알고리즘 문제/BOJ
2606번 바이러스
parkit
2019. 9. 22. 23:54
728x90
반응형
https://www.acmicpc.net/problem/2606
유니온 파인드(Union Find) 문제이다.
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 <bits/stdc++.h> using namespace std; #define Max 101 int parent[Max], virus[Max], N, C; 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) { if (a < b) parent[b] = a; else parent[a] = b; virus[min(a, b)] += virus[max(a, b)]; } } int main(void) { //cin.tie(0); for (int i = 1; i <= 100; i++) { parent[i] = i, virus[i] = 1; } int f, t; scanf("%d %d", &N, &C); for (int i = 0; i < C; i++) { scanf("%d %d", &f, &t); Union(f, t); } printf("%d\n", virus[1] - 1); return 0; } | cs |
728x90
반응형