본문 바로가기
백준 알고리즘/BFS

백준 1325번 C++

by paysmile 2019. 8. 20.

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;
const int MAX = 10001;
int n, m;
vector<int> trust[MAX];
vector<int> answer;
int visited[MAX];

int dfs(int i) {
	visited[i] = 0;

	for (int k = 0; k < trust[i].size(); k++) {
		int move = trust[i][k];
		if (visited[move] == -1)
			visited[i] = visited[i] + dfs(move) + 1;
	}
	return visited[i];
}

int main(void) {
	int maxvalue = -1;
	ios_base::sync_with_stdio(0);
	cin.tie(0); 
	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		int computer1,computer2;
		cin >> computer1 >> computer2;
		trust[computer2].push_back(computer1);
	}
	for (int i = 1; i <= n; i++) {
		memset(visited, -1, sizeof(visited));
		dfs(i);
		if (maxvalue == visited[i])
			answer.push_back(i);
		else if (maxvalue < visited[i]) {
			answer.clear();
			answer.push_back(i);
			maxvalue = visited[i];
		}
	}
	sort(answer.begin(),answer.end());
	for (int i = 0; i < answer.size(); i++) 
		cout << answer[i] << " ";
	return 0;
}

'백준 알고리즘 > BFS' 카테고리의 다른 글

백준 5427번 C++  (0) 2019.08.21
백준 2251번 C++  (0) 2019.08.20
백준 1967번 C++  (0) 2019.08.19
백준 9019번 C++  (0) 2019.08.18
백준 2146번 C++  (0) 2019.08.17