Recent Posts
Notice
No Rules Rules
집합의 표현 (feat. 백준, 1717번) 본문
728x90
반응형
집합의 표현
https://www.acmicpc.net/problem/1717
1717번: 집합의 표현
첫째 줄에 n(1 ≤ n ≤ 1,000,000), m(1 ≤ m ≤ 100,000)이 주어진다. m은 입력으로 주어지는 연산의 개수이다. 다음 m개의 줄에는 각각의 연산이 주어진다. 합집합은 0 a b의 형태로 입력이 주어진다. 이는
www.acmicpc.net
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <vector>
using namespace std;
int N, M;
int arr[1000001];
int find(register int num) {
if (arr[num] < 0)
return num;
return arr[num] = find(arr[num]);
}
void merge(register int a, register int b) {
a = find(a);
b = find(b);
if (a != b)
arr[a] = b;
}
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
cin >> N >> M;
for (register int n = 0; n <= N; ++n)
arr[n] = -1;
for (register int m = 0, q, a, b; m < M; ++m) {
cin >> q >> a >> b;
if (q == 0) {
merge(a, b);
}
else {
if (find(a) == find(b))
cout << "YES" << "\n";
else
cout << "NO" << "\n";
}
}
return 0;
}
// *&)*@*
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
미네랄 (feat. 백준, 2933번) (0) | 2022.08.31 |
---|---|
백조의 호수 (feat. 백준, 3197번) (0) | 2022.08.31 |
이진 검색 트리 (feat. 백준, 5639번) (0) | 2022.08.30 |
트리의 순회 (feat. 백준, 2263번) (0) | 2022.08.30 |
미확인 도착지 (feat. 백준, 9370번) (0) | 2022.08.30 |
Comments