cgy12306
[백준 BoJ] 2660 - 회장뽑기 본문
// 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 << " ";
}
- 플로이드-와샬
'Algorithm > C++' 카테고리의 다른 글
[백준 BoJ] 1753 - 최단경로 (0) | 2021.12.31 |
---|---|
[백준 BoJ] 1916 - 최소비용 구하기 (0) | 2021.12.31 |
[백준 BoJ] 9205 - 맥주 마시면서 걸어가기 (0) | 2021.12.27 |
[백준 BoJ] 11404 - 플로이드 (0) | 2021.12.27 |
[백준 BoJ] 11403 - 경로 찾기 (0) | 2021.12.27 |
Comments