반응형
출처
2004 전국 본선 초등3
힌트
단순 탐색
풀이
양방향 그래프를 만들어 준 뒤, 연결 되어 있는 컴퓨터들을 찾아가면 됩니다.
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
|
#include<iostream>
using namespace std;
int com[101][101];
int check[101];
int ans=-1;
int v,e;
void dfs(int n){
check[n]=1;
ans++;
for(int i=1;i<=v;i++){
if(com[n][i]==1&&!check[i]){
dfs(i);
}
}
}
int main(){
cin>>v>>e;
for(int i=0;i<e;i++){
int a,b;
cin>>a>>b;
com[a][b]=1;
com[b][a]=1;
}
int n;
dfs(1);
cout<<ans;
}
|


반응형
'Problem Solve > DFS & BFS' 카테고리의 다른 글
[코드업] 4039 : 놀이공원 (C) (0) | 2020.03.10 |
---|---|
[백준] 2667번 단지 번호 붙이기 (C) (0) | 2020.03.10 |
[코드업] 3212 : 위상 정렬(topological sort) (C) (0) | 2020.03.09 |
[코드업] 2610 : 그림판 채우기 (C) (0) | 2020.03.09 |
[코드업] 2605 : 캔디팡 (C) (0) | 2020.03.09 |