#include <bits/stdc++.h>
using namespace std;
const int N = 500*1000+5;
vector<int> g[N];
int dist[N];
void dfs(int v,int p = -1){
for(int i = 0;i<g[v].size();i++){
int to = g[v][i];
if(to == p) continue;
dfs(to,v);
dist[v] = max(dist[v],dist[to]+1);
}
}
int H[5005*5005];
long long mx = 0;
int n;
void calc(int v,int hard = 0,int d = 0,int p = -1){
if((g[v].size() == 1 && d+1 == n)){
hard = min(hard,dist[v]);
}
if(g[v].size() == 1 && p!=-1){
long long now = (long long)hard*(long long)d;
H[now]++;
mx = max(mx,now);
}
int mx1,mx2;
mx1 = mx2 = 0;
for(int i = 0;i<g[v].size();i++){
int to = g[v][i];
if(to == p) continue;
if(dist[to]+1>mx1){
mx1 = dist[to]+1;
}
else if(dist[to]+1>mx2){
mx2 = dist[to]+1;
}
}
for(int i = 0;i<g[v].size();i++){
int to = g[v][i];
if(to == p) continue;
int h = -1;
if(dist[to]+1 == mx1) h = mx2;
else h = mx1;/*
if(dist[v]>hard){
for(int j = 0;j<g[v].size();j++){
int too = g[v][j];
if(too!=to && too!=p){
if(h == -1 || dist[too]+1>h) h = dist[too]+1;
}
}
}*/
int hardd = hard;
if(h!=-1){
hardd = max(hardd,h);
}
calc(to,hardd,d+1,v);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin>>n;
for(int i = 1;i<=n-1;i++){
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i = 1;i<=n;i++){
if(g[i].size() == 1){
memset(dist,0,sizeof dist);
dfs(i);
calc(i);
}
}
//cout<<H[mx]<<endl;
cout<<mx<<" "<<H[mx]/2<<endl;
return 0;
}
Compilation message
road.cpp: In function 'void dfs(int, int)':
road.cpp:7:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[v].size();i++){
~^~~~~~~~~~~~
road.cpp: In function 'void calc(int, int, int, int)':
road.cpp:28:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[v].size();i++){
~^~~~~~~~~~~~
road.cpp:38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0;i<g[v].size();i++){
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14072 KB |
Output is correct |
2 |
Correct |
14 ms |
14072 KB |
Output is correct |
3 |
Correct |
11 ms |
14072 KB |
Output is correct |
4 |
Correct |
11 ms |
14072 KB |
Output is correct |
5 |
Correct |
11 ms |
14072 KB |
Output is correct |
6 |
Correct |
14 ms |
14072 KB |
Output is correct |
7 |
Correct |
14 ms |
14072 KB |
Output is correct |
8 |
Correct |
13 ms |
14072 KB |
Output is correct |
9 |
Correct |
14 ms |
14072 KB |
Output is correct |
10 |
Correct |
16 ms |
14072 KB |
Output is correct |
11 |
Correct |
19 ms |
14072 KB |
Output is correct |
12 |
Correct |
19 ms |
14072 KB |
Output is correct |
13 |
Correct |
16 ms |
14076 KB |
Output is correct |
14 |
Correct |
18 ms |
14072 KB |
Output is correct |
15 |
Correct |
14 ms |
14076 KB |
Output is correct |
16 |
Correct |
14 ms |
14072 KB |
Output is correct |
17 |
Correct |
14 ms |
14072 KB |
Output is correct |
18 |
Correct |
14 ms |
14012 KB |
Output is correct |
19 |
Correct |
14 ms |
14072 KB |
Output is correct |
20 |
Correct |
13 ms |
14072 KB |
Output is correct |
21 |
Correct |
14 ms |
14072 KB |
Output is correct |
22 |
Correct |
13 ms |
14072 KB |
Output is correct |
23 |
Correct |
15 ms |
14072 KB |
Output is correct |
24 |
Correct |
22 ms |
14088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14072 KB |
Output is correct |
2 |
Correct |
14 ms |
14072 KB |
Output is correct |
3 |
Correct |
11 ms |
14072 KB |
Output is correct |
4 |
Correct |
11 ms |
14072 KB |
Output is correct |
5 |
Correct |
11 ms |
14072 KB |
Output is correct |
6 |
Correct |
14 ms |
14072 KB |
Output is correct |
7 |
Correct |
14 ms |
14072 KB |
Output is correct |
8 |
Correct |
13 ms |
14072 KB |
Output is correct |
9 |
Correct |
14 ms |
14072 KB |
Output is correct |
10 |
Correct |
16 ms |
14072 KB |
Output is correct |
11 |
Correct |
19 ms |
14072 KB |
Output is correct |
12 |
Correct |
19 ms |
14072 KB |
Output is correct |
13 |
Correct |
16 ms |
14076 KB |
Output is correct |
14 |
Correct |
18 ms |
14072 KB |
Output is correct |
15 |
Correct |
14 ms |
14076 KB |
Output is correct |
16 |
Correct |
14 ms |
14072 KB |
Output is correct |
17 |
Correct |
14 ms |
14072 KB |
Output is correct |
18 |
Correct |
14 ms |
14012 KB |
Output is correct |
19 |
Correct |
14 ms |
14072 KB |
Output is correct |
20 |
Correct |
13 ms |
14072 KB |
Output is correct |
21 |
Correct |
14 ms |
14072 KB |
Output is correct |
22 |
Correct |
13 ms |
14072 KB |
Output is correct |
23 |
Correct |
15 ms |
14072 KB |
Output is correct |
24 |
Correct |
22 ms |
14088 KB |
Output is correct |
25 |
Correct |
400 ms |
14684 KB |
Output is correct |
26 |
Correct |
418 ms |
14584 KB |
Output is correct |
27 |
Correct |
405 ms |
14584 KB |
Output is correct |
28 |
Correct |
402 ms |
14584 KB |
Output is correct |
29 |
Correct |
949 ms |
20600 KB |
Output is correct |
30 |
Correct |
903 ms |
20600 KB |
Output is correct |
31 |
Correct |
895 ms |
20600 KB |
Output is correct |
32 |
Correct |
913 ms |
20600 KB |
Output is correct |
33 |
Correct |
20 ms |
14840 KB |
Output is correct |
34 |
Correct |
19 ms |
14840 KB |
Output is correct |
35 |
Correct |
19 ms |
14840 KB |
Output is correct |
36 |
Correct |
19 ms |
14844 KB |
Output is correct |
37 |
Correct |
16 ms |
14584 KB |
Output is correct |
38 |
Correct |
14 ms |
14584 KB |
Output is correct |
39 |
Correct |
18 ms |
14328 KB |
Output is correct |
40 |
Correct |
21 ms |
14328 KB |
Output is correct |
41 |
Correct |
26 ms |
14328 KB |
Output is correct |
42 |
Correct |
39 ms |
14328 KB |
Output is correct |
43 |
Correct |
59 ms |
14328 KB |
Output is correct |
44 |
Correct |
94 ms |
14200 KB |
Output is correct |
45 |
Correct |
190 ms |
14200 KB |
Output is correct |
46 |
Correct |
242 ms |
14204 KB |
Output is correct |
47 |
Correct |
606 ms |
14332 KB |
Output is correct |
48 |
Correct |
977 ms |
14328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14072 KB |
Output is correct |
2 |
Correct |
14 ms |
14072 KB |
Output is correct |
3 |
Correct |
11 ms |
14072 KB |
Output is correct |
4 |
Correct |
11 ms |
14072 KB |
Output is correct |
5 |
Correct |
11 ms |
14072 KB |
Output is correct |
6 |
Correct |
14 ms |
14072 KB |
Output is correct |
7 |
Correct |
14 ms |
14072 KB |
Output is correct |
8 |
Correct |
13 ms |
14072 KB |
Output is correct |
9 |
Correct |
14 ms |
14072 KB |
Output is correct |
10 |
Correct |
16 ms |
14072 KB |
Output is correct |
11 |
Correct |
19 ms |
14072 KB |
Output is correct |
12 |
Correct |
19 ms |
14072 KB |
Output is correct |
13 |
Correct |
16 ms |
14076 KB |
Output is correct |
14 |
Correct |
18 ms |
14072 KB |
Output is correct |
15 |
Correct |
14 ms |
14076 KB |
Output is correct |
16 |
Correct |
14 ms |
14072 KB |
Output is correct |
17 |
Correct |
14 ms |
14072 KB |
Output is correct |
18 |
Correct |
14 ms |
14012 KB |
Output is correct |
19 |
Correct |
14 ms |
14072 KB |
Output is correct |
20 |
Correct |
13 ms |
14072 KB |
Output is correct |
21 |
Correct |
14 ms |
14072 KB |
Output is correct |
22 |
Correct |
13 ms |
14072 KB |
Output is correct |
23 |
Correct |
15 ms |
14072 KB |
Output is correct |
24 |
Correct |
22 ms |
14088 KB |
Output is correct |
25 |
Correct |
400 ms |
14684 KB |
Output is correct |
26 |
Correct |
418 ms |
14584 KB |
Output is correct |
27 |
Correct |
405 ms |
14584 KB |
Output is correct |
28 |
Correct |
402 ms |
14584 KB |
Output is correct |
29 |
Correct |
949 ms |
20600 KB |
Output is correct |
30 |
Correct |
903 ms |
20600 KB |
Output is correct |
31 |
Correct |
895 ms |
20600 KB |
Output is correct |
32 |
Correct |
913 ms |
20600 KB |
Output is correct |
33 |
Correct |
20 ms |
14840 KB |
Output is correct |
34 |
Correct |
19 ms |
14840 KB |
Output is correct |
35 |
Correct |
19 ms |
14840 KB |
Output is correct |
36 |
Correct |
19 ms |
14844 KB |
Output is correct |
37 |
Correct |
16 ms |
14584 KB |
Output is correct |
38 |
Correct |
14 ms |
14584 KB |
Output is correct |
39 |
Correct |
18 ms |
14328 KB |
Output is correct |
40 |
Correct |
21 ms |
14328 KB |
Output is correct |
41 |
Correct |
26 ms |
14328 KB |
Output is correct |
42 |
Correct |
39 ms |
14328 KB |
Output is correct |
43 |
Correct |
59 ms |
14328 KB |
Output is correct |
44 |
Correct |
94 ms |
14200 KB |
Output is correct |
45 |
Correct |
190 ms |
14200 KB |
Output is correct |
46 |
Correct |
242 ms |
14204 KB |
Output is correct |
47 |
Correct |
606 ms |
14332 KB |
Output is correct |
48 |
Correct |
977 ms |
14328 KB |
Output is correct |
49 |
Execution timed out |
2062 ms |
56368 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |