-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20955.h
97 lines (76 loc) · 1.39 KB
/
20955.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
int N, M;
vector<vector<int>> E;
bool visited[100000];
int groupCount = 0;
int root[100000];
int depth[100000];
int cycleEdgeCount = 0;
void DFS(int n, bool isFirstAccess) {
if (visited[n])
return;
visited[n] = true;
int s = E[n].size();
for (int i = 0; i < s; i++)
DFS(E[n][i], false);
if (isFirstAccess)
groupCount++;
return;
}
int unionSet() {
for (int i = 0; i < N; i++) {
root[i] = i;
depth[i] = 1;
}
return 0;
}
int unionFind(int n) {
if (root[n] == n)
return n;
else
return root[n] = unionFind(root[n]);
}
int unionOperation(int a, int b) {
int x = unionFind(a);
int y = unionFind(b);
if (x == y)
return 0;
if (depth[x] > depth[y])
root[y] = x;
else if (depth[x] < depth[y])
root[x] = y;
else {
depth[y]++;
root[x] = y;
}
return 0;
}
int main_20955() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
scanf("%d", &N);
fill_n(visited, N, false);
scanf("%d", &M);
E = vector<vector<int>>(N, vector<int>());
unionSet();
int a, b;
for (int i = 0; i < M; i++) {
scanf("%d", &a);
a--;
scanf("%d", &b);
b--;
if (unionFind(a) == unionFind(b))
cycleEdgeCount++;
else
unionOperation(a, b);
E[a].push_back(b);
E[b].push_back(a);
}
for (int i = 0; i < N; i++)
DFS(i, true);
printf("%d", groupCount - 1 + cycleEdgeCount);
return 0;
}