#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int M = 1e9 + 7;
const int N = 1e6 + 1;
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
vector<int> g[n+1];
vector<int> answer(n+n,0);
int max_distance = 0;
for(int i = 0;i < n; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
queue<int> q;
for(int i = 1;i <= n; i++){
vector<int> dis(n+1,-1), cnt(n+1,0);
dis[i] = 0; q.push(i); cnt[i] = 1;
while(!q.empty()){
int v = q.front(); q.pop();
for(int to : g[v]){
if(dis[to] == -1){
dis[to] = dis[v]+1;
cnt[to] = cnt[v];
answer[dis[to]]+= cnt[v];
q.push(to);
}else if(dis[to] == dis[v]+1){
cnt[to]+= cnt[v];
answer[dis[to]]+= cnt[v];
}
max_distance = max(max_distance, dis[to]);
}
}
}
// cout << max_distance << '\n';
cout << answer[max_distance]/2;
return 0;
}
Compilation message
shymbulak.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
11 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
5 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
388 KB |
Output is correct |
2 |
Correct |
10 ms |
416 KB |
Output is correct |
3 |
Correct |
19 ms |
420 KB |
Output is correct |
4 |
Correct |
21 ms |
416 KB |
Output is correct |
5 |
Correct |
513 ms |
796 KB |
Output is correct |
6 |
Correct |
386 ms |
864 KB |
Output is correct |
7 |
Correct |
631 ms |
860 KB |
Output is correct |
8 |
Correct |
554 ms |
976 KB |
Output is correct |
9 |
Correct |
526 ms |
844 KB |
Output is correct |
10 |
Correct |
547 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1595 ms |
10972 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |