#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
int n, ans, mx, d[N], ct[N], mxi, rs;
vector<int> g[N];
bool used[N];
void bfs(int v) {
queue<int> q;
q.push(v);
for (int i = 1; i <= n; i++) {
used[i] = 0;
ct[i] = 0;
}
d[v] = 0;
used[v] = 1;
ct[v] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < g[u].size(); i++) {
int to = g[u][i];
if (!used[to]) {
used[to] = 1;
ct[to] = ct[u];
d[to] = d[u] + 1;
q.push(to);
} else if (d[to] == d[u] + 1) {
ct[to] += ct[u];
}
}
}
}
int main() {
int rs = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
bfs(1);
for (int i = 1; i <= n; i++) {
if (mx < d[i]) {
mx = d[i];
mxi = i;
}
}
bfs(mxi);
mx = 0;
int lst = mxi;
for (int i = 1; i <= n; i++) {
if (mx < d[i]) {
mx = d[i];
mxi = i;
rs = 0;
}
if (mx == d[i]) {
rs += ct[i];
}
}
mx = 0;
bfs(mxi);
for (int i = 1; i <= n; i++) {
if (mx < d[i]) {
mx = d[i];
ans = 0;
}
if (mx == d[i]) {
ans += ct[i];
}
}
cout << ans + rs - ct[lst];
}
Compilation message
shymbulak.cpp: In function 'void bfs(int)':
shymbulak.cpp:30:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[u].size(); i++) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
5240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5240 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
136 ms |
8416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |