페이지

6118번: Hide and Seek

https://www.acmicpc.net/problem/6118


#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX_N = 20000;
int n, m;
int q[MAX_N], h, t, dist[MAX_N + 1];
vector<int> adj[MAX_N + 1];
int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    fill(dist + 1, dist + 1 + n, -1);
    dist[1] = 0;
    q[t++] = 1;
    while (h != t) {
        for (auto it : adj[q[h]]) {
            if (dist[it] != -1) continue;
            dist[it] = dist[q[h]] + 1;
            q[t++] = it;
        }
        h++;
    }
    int maxi = 0, cnt = 0, barn;
    for (int i = 2; i <= n; i++) {
        if (dist[i] > maxi) {
            maxi = dist[i];
            cnt = 1;
            barn = i;
        }
        else if (dist[i] == maxi) {
            cnt++;
            barn = min(barn, i);
        }
    }
    printf("%d %d %d", barn, maxi, cnt);
    return 0;
}

댓글 없음 :

댓글 쓰기