이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const int INF = 2'000'000'000;
int n;
vector<int> g[MAXN];
int cur = 0, cnt = 0;
void bfs(int a) {
int dist[MAXN];
fill (dist, dist + MAXN, INF);
queue < int > q;
q.push(a);
dist[a] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto i : g[v]) {
if (dist[i] >= dist[v] + 1) {
q.push(i);
dist[i] = dist[v] + 1;
if (dist[i] == cur) {
cnt++;
} else if (dist[i] > cur) {
cur = dist[i];
cnt = 1;
}
}
}
}
return;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int ansD = 0, cntWay = 0;
for (int i = 1; i <= n; i++) {
cur = 0;
cnt = 0;
bfs(i);
if (cur == ansD) {
cntWay += cnt;
} else if (cur > ansD) {
ansD = cur;
cntWay = cnt;
}
}
cout << cntWay / 2 << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |