File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ++
2+ #include <string>
3+ #include <vector>
4+ #include <queue>
5+
6+ using namespace std;
7+
8+ int cnt;
9+ vector<int> info[101];
10+
11+ void bfs(int v1, int v2) {
12+ queue<int> q;
13+ vector<bool> visited(101);
14+
15+ q.push(v1);
16+ visited[v1] = true;
17+ visited[v2] = true;
18+
19+ while (!q.empty()) {
20+ int node = q.front();
21+ q.pop();
22+
23+ for (int i = 0; i < info[node].size(); i++) {
24+ int node2 = info[node][i];
25+
26+ if (visited[node2])
27+ continue;
28+
29+ cnt++;
30+ q.push(node2);
31+ visited[node2] = true;
32+ }
33+ }
34+ }
35+
36+ int solution(int n, vector<vector<int>> wires) {
37+ int answer = 101;
38+
39+ for (auto wire : wires) {
40+ info[wire[0]].push_back(wire[1]);
41+ info[wire[1]].push_back(wire[0]);
42+ }
43+
44+ for (auto wire : wires) {
45+ cnt = 1;
46+ int v1 = wire[0];
47+ int v2 = wire[1];
48+
49+ bfs(v1, v2);
50+ answer = min(answer, abs(2 * cnt - n));
51+ }
52+
53+ return answer;
54+ }
You can’t perform that action at this time.
0 commit comments