Coding/acmicpc
백준 2606:바이러스
이끼대백과
2018. 2. 23. 18:40
백준 2606번 문제입니다. 이번문제 또한 http://tiger1710.tistory.com/8 이때 풀었던 문제와 다를건 별로 없는거 같아요!
11724번 문제는 그래프 한 뭉탱이?의 갯수를 세 주었다면, 이번문제는 한 뭉탱이에 1을 제외한 정점의 갯수만 세어주면 될거같아요!
밑에는 제 코드 입니다.
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 | #include <stdio.h> #include <stdbool.h> #define MAX 101 int COM[MAX][MAX]; bool visited[MAX]; int N, M; int cnt; void dfs(int k) { visited[k] = true; for (int i = 1; i <= N; i++) { if (visited[i] != false || COM[k][i] == 0) continue; cnt++; dfs(i); } } int main(void) { scanf("%d%d", &N, &M); for (int i = 0; i < M; i++) { int u, v; scanf("%d%d", &u, &v); COM[u][v] = 1; COM[v][u] = 1; } dfs(1); printf("%d\n", cnt); return 0; } | cs |
15:새로운 정점이 발견될때마다 cnt++
30:1에서 출발하는 것만 세어주면 됩니다.