-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20040.cpp
More file actions
52 lines (45 loc) · 774 Bytes
/
20040.cpp
File metadata and controls
52 lines (45 loc) · 774 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
vector<int> parent;
// Find 연산
int find(int x) {
if (parent[x] < 0) {
return x;
}
return parent[x] = find(parent[x]);
}
// Union 연산
bool unionInput(int x, int y) {
int xp = find(x);
int yp = find(y);
if (xp == yp) {
return false;
}
if (parent[xp] < parent[yp]) {
parent[xp] += parent[yp];
parent[yp] = xp;
}
else {
parent[yp] += parent[xp];
parent[xp] = yp;
}
return true;
}
int main()
{
int n, m, p1, p2, end = 0;
cin >> n >> m;
// 입력
parent.assign(n, -1);
for (int i = 0; i < m; i++) {
cin >> p1 >> p2;
// 연산 & 출력
if (!unionInput(p1, p2)) { // 사이클 생성
end = i + 1;
break;
}
}
cout << end << '\n'; // 사이클 생성 X
return 0;
}