-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaekjoon_10775.cpp
57 lines (48 loc) · 1.07 KB
/
baekjoon_10775.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int G, P;
struct DisjointSet{
vector<int> parent;
// Constructor
DisjointSet(int N) : parent(N){
for(int i = 0 ; i < N; i++){
parent[i] = i;
}
}
// U가 속한 트리의 루트를 반환
int find(int u){
if(parent[u] == u) return u;
else{
return parent[u] = find(parent[u]);
}
}
// U와 V가 속한 트리를 병합
void merge(int u, int v){
u = find(u);
v = find(v);
if(u == v) return;
// u가 무조건 더 작게끔
if(u > v) swap(u, v);
parent[v] = u;
return;
}
};
int main(void){
cin >> G;
cin >> P;
DisjointSet Disjoint(G + 1);
for(int i = 0; i < P; i++){
int temp;
cin >> temp;
int check_num = Disjoint.find(temp);
if(check_num == 0){
cout << i << "\n";
return 0;
}
Disjoint.merge(check_num, check_num -1);
}
cout << P << "\n";
return 0;
}