티스토리 뷰
https://www.acmicpc.net/problem/11724
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 | #include <cstdio> #include <vector> using namespace std; int n,m,u,v,c[1001],ans; vector<int> a[1001]; void dfs(int x) { c[x]=1; for(auto i : a[x]) if(!c[i]) dfs(i); } int main() { scanf("%d %d",&n,&m); while(m--) { scanf("%d %d",&u,&v); a[u].push_back(v); a[v].push_back(u); } for(int i=1;i<=n;i++) if(!c[i]) dfs(i),ans++; printf("%d\n",ans); return 0; } | cs |
'Algorithm > BOJ' 카테고리의 다른 글
6603 로또 (0) | 2018.07.09 |
---|---|
10871 X보다 작은 수 (0) | 2018.07.09 |
9498 시험 성적 (0) | 2018.07.09 |
2583 영역 구하기 (0) | 2018.07.09 |
1924 2007년 (0) | 2018.07.09 |