[BOJ] 1717 집합의 표현

Time Lapse :28min 51sec

1717.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <algorithm>
using namespace std;
int g[1000001];
int n, m, c, a, b;
int getG(int n) {
return g[n] == n ? g[n] : (g[n] = getG(g[n]));
}

void makeG(int a, int b) {
g[getG(a)] = g[getG(b)] = min(getG(a),getG(b));
}

int main() {
scanf("%d %d",&n, &m);
for(int i = 0; i <= n; ++i)
g[i] = i;
for(int i = 0; i < m; ++i) {
scanf("%d %d %d",&c,&a,&b);
if(c) printf("%s\n", getG(a) ^ getG(b) ? "NO" : "YES");
else makeG(a,b);
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/30/PS/BOJ/1717/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.