-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path공항.cc
46 lines (39 loc) · 854 Bytes
/
공항.cc
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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> parent;
int p, g;
int Find(int x) {
if (parent[x] == x) return x;
return parent[x] = Find(parent[x]);
}
void Union(int a, int b) {
a = Find(a);
b = Find(b);
if (a < b) parent[b] = a;
else parent[a] = b;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("input.txt", "r", stdin);
cin >> g >> p;
vector<int> arr(p, 0);
for (int i = 0; i < p; i++) {
cin >> arr[i];
}
for (int i = 0; i <= g; i++) {
parent.push_back(i);
}
int cnt = 0;
for (int i = 0; i < p; i++) {
int gate = Find(arr[i]);
if (gate == 0) break;
Union(gate, gate-1);
cnt++;
}
cout << cnt;
return 0;
}