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 | #include <stdio.h> int adj[1005][1005] = { 0 }; int check[1005] = { 0 }; int n; void DFS(int v); int main() { int m; scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%d %d", &a, &b); adj[a][b] = 1; adj[b][a] = 1; } int rst = 0; for (int i = 1; i <= n; i++) { if (check[i] == 0) { DFS(i); rst++; } } printf("%d", rst); } void DFS(int v) { for (int i = 1; i <= n; i++) { if (adj[v][i] == 1 && check[i] == 0) { check[i] = 1; DFS(i); } } } | cs |
'알고리즘 > DFS' 카테고리의 다른 글
| 2583 영역 구하기 (0) | 2019.07.30 |
|---|---|
| 10026 적록색약 (0) | 2019.07.30 |
| 11403 경로찾기 (0) | 2019.07.30 |
| 1012 유기농 배추 (0) | 2019.07.29 |
| 2667 단지번호 붙이기 (0) | 2019.07.29 |