cgy12306

[백준 BoJ] 2660 - 회장뽑기 본문

Algorithm/C++

[백준 BoJ] 2660 - 회장뽑기

cgy12306 2021. 12. 28. 15:29
// https://www.acmicpc.net/problem/2660
// 회장뽑기
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int N, arr[52][52], INF = 999999999;
void floyd_warshall() {
	for (int k = 1; k <= N; k++) {
		for (int i = 1; i <= N; i++) {
			for (int j = 1; j <= N; j++) {
				if (arr[i][k] && arr[k][j]) {
					if (i == j || i == k || j == k) continue;
					arr[i][j] = min(arr[i][k] + arr[k][j], arr[i][j]);
				}
			}
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	vector<int> ans;
	cin >> N;
	fill(arr[0], arr[52], INF);
	while (1) {
		int from, to;
		cin >> from >> to;
		if (from == -1 && to == -1) break;

		arr[from][to] = 1;
		arr[to][from] = 1;
	}

	floyd_warshall();

	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			if (arr[i][j] == INF) arr[i][j] = 0;
		}
	}
	int Max = 0, Min = INF;
	
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			Max = max(Max, arr[i][j]);
		}
		Min = min(Max, Min);
		Max = 0;
	}
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			Max = max(Max, arr[i][j]);
		}
		if (Max == Min) ans.push_back(i);
		Max = 0;
	}
	sort(ans.begin(), ans.end());

	cout << Min << " " << ans.size() << "\n";
	for (auto a : ans) cout << a << " ";

}
  • 플로이드-와샬
Comments