[문제 링크] : https://www.acmicpc.net/problem/10775
#include <iostream>
using namespace std;
int G, P;
int Arr[100001];
int Ans;
int find(int n){
if(n == Arr[n]){
return n;
}
return Arr[n] = find(Arr[n]);
}
void solve(int a, int b){
a = find(a);
b = find(b);
if(a > b){
swap(a, b);
}
Arr[b] = a;
return;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> G >> P;
for(int i=0; i<=G; i++){
Arr[i] = i;
}
int inp;
for(int i=1; i<=P; i++){
cin >> inp;
inp = find(inp);
if(inp == 0){
break;
}
solve(inp, inp - 1);
Ans++;
}
cout << Ans << "\n";
return 0;
}
'알고리즘' 카테고리의 다른 글
[알고리즘] 백준 6321 - IBM 빼기 1 (C++) (0) | 2025.02.02 |
---|---|
[알고리즘] 백준 16431 - 베시와 데이지 (C++) (0) | 2025.02.01 |
[알고리즘] 백준 7453 - 합이 0인 네 정수 (C++) (0) | 2025.01.30 |
[알고리즘] 백준 22864 - 피로도 (C++) (0) | 2025.01.29 |
[알고리즘] 백준 26307 - Correct (C++) (0) | 2025.01.28 |