From 90b0f86e1d87bcba4519fb4f1dc48f7bc30f01da Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Sat, 11 Sep 2021 20:36:15 +0800 Subject: [PATCH] [USACO09OPEN]Hide and Seek S R57850314 --- Luogu/problem/P2951/P2951.cpp | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Luogu/problem/P2951/P2951.cpp diff --git a/Luogu/problem/P2951/P2951.cpp b/Luogu/problem/P2951/P2951.cpp new file mode 100644 index 00000000..cd48cefe --- /dev/null +++ b/Luogu/problem/P2951/P2951.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; + +int n, m, u, v, dist[200005], ans1, ans2, ans3; +vector g[200005]; + +void spfa() { + memset(dist, 0x3f, sizeof(dist)); + dist[1] = 0; + queue q; + q.push(1); + while (!q.empty()) { + auto t = q.front(); + q.pop(); + for (auto i : g[t]) { + if (dist[i] > dist[t] + 1) { + dist[i] = dist[t] + 1; + q.push(i); + } + } + } +} + +int main() { + cin >> n >> m; + for (int i = 0; i < m; i++) { + cin >> u >> v; + g[u].push_back(v); + g[v].push_back(u); + } + spfa(); + for (int i = 1; i <= n; i++) { + if (ans2 < dist[i]) { + ans2 = dist[i]; + ans1 = i; + } + } + for (int i = 1; i <= n; i++) { + ans3 += dist[i] == ans2; + } + cout << ans1 << ' ' << ans2 << ' ' << ans3 << endl; + return 0; +}