[BOJ | 백준] 1325번: 효율적인 해킹
2021. 1. 6. 17:13ㆍIT/BOJ
반응형
출처: www.acmicpc.net/problem/1325
1325번: 효율적인 해킹
첫째 줄에, N과 M이 들어온다. N은 10,000보다 작거나 같은 자연수, M은 100,000보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에 신뢰하는 관계가 A B와 같은 형식으로 들어오며, "A가 B를 신뢰한
www.acmicpc.net
각 노드에서 탐색을 수행해 접근가능한 노드의 수를 계산해 해결할 수 있다.
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int cnt_children[10001];
vector<int> graph[10001];
queue<int> dfs_queue;
vector<bool> visited;
int N,M,parent,child,max_value,current,cnt;
void bfs(int n){
visited.resize(N+1);
dfs_queue.push(n);
visited[n]=true;
cnt=0;
while(!dfs_queue.empty()){
current=dfs_queue.front();
dfs_queue.pop();
cnt++;
for(int i=0;i<graph[current].size();i++){
if(visited[graph[current][i]]) continue;
dfs_queue.push(graph[current][i]);
visited[graph[current][i]]=true;
}
}
cnt_children[n]=cnt;
visited.clear();
}
int main(){
max_value=0;
cin>>N>>M;
while(M--){
cin>>child>>parent;
graph[parent].push_back(child);
}
for(int n=1;n<=N;n++){
bfs(n);
if(max_value<cnt_children[n]) max_value=cnt_children[n];
}
for(int n=1;n<=N;n++){
if(cnt_children[n]==max_value) cout<<n<<" ";
}
}
반응형
'IT > BOJ' 카테고리의 다른 글
[BOJ | 백준] 1436번: 영화감독 숌 (0) | 2021.01.07 |
---|---|
[BOJ | 백준] 2213번: 트리의 독립집합 (0) | 2021.01.07 |
[BOJ | 백준] 1914번: 하노이 탑 (0) | 2021.01.06 |
[BOJ | 백준] 1197번: 최소 스패닝 트리 (0) | 2021.01.05 |
[BOJ | 백준] 10868번: 최솟값 (0) | 2021.01.05 |