문제 접근
입력 받은 것들을 인접 행렬로 구현하고, 1번에 대해 DFS 한 뒤 탐색하는 것들의 수를 cnt 변수에 기록해 반환하여 풀었다. 간단한 문제!
문제 코드
#include <iostream>
using namespace std;
int v,e;
bool adj[101][101] = {false};
bool visited[101] = {false};
int dfs(int n)
{
int cnt=0;
visited[n] = true;
for(int i=1; i<=v; ++i)
if(!visited[i] && adj[n][i])
cnt=cnt+1+dfs(i);
return cnt;
}
int main(void)
{
int in, out;
cin>>v>>e;
for(int i=0; i<e; ++i)
{
cin>>in>>out;
adj[in][out]=adj[out][in]=true;
}
cout<<dfs(1)<<'\n';
}
'개발 > 알고리즘' 카테고리의 다른 글
백준 11723번 : 집합 (0) | 2020.08.24 |
---|---|
백준 14502 : 연구소 (1) | 2020.08.23 |
백준 7662번 : 이중 우선순위 큐 (1) | 2020.08.17 |
백준 11279번 : 최대 힙, 백준 1917번 : 최소 힙 (0) | 2020.08.17 |
백준 11399번 : ATM (0) | 2020.08.16 |