#include "bits/stdc++.h"
using namespace std;
#define int long long
vector<int> adj[500001];
int mx[500001];
int cnt[500001];
pair<int,int> pref[500001];
long long overMA = 0;
long long overCNT = 1;
void dfs(int i,int pr){
mx[i] = 0 , cnt[i] = 1;
for(auto j:adj[i]){
if(j==pr)continue;
dfs(j,i);
if(mx[j]+1>mx[i]){
mx[i] = mx[j]+1;
cnt[i] = 0;
}
if(mx[j]+1==mx[i]){
cnt[i]+=cnt[j];
}
}
}
void reroot(int i,int pr,int MA,int CNT){
vector<pair<int,int>> v;
if(MA)v.push_back({MA,CNT});
for(auto j:adj[i]){
if(j==pr)continue;
v.push_back({mx[j]+1,cnt[j]});
}
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
if(v.size()>=3){
int z = v[0].first , y = v[1].first , x = v[2].first;
long long cost = 0;
if(x<y&&y<z){
int A = v[0].second , B = v[1].second , C = v[2].second;
for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
cost = B*C;
}else if(x<y&&y==z){
int A = v[0].second , B = v[1].second , C = v[2].second;
for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
cost = (A+B)*C;
}else if(x==y&&y<z){
int A = v[0].second , B = v[1].second , C = v[2].second;
cost = B*C;
long long sum = B+C;
for(int i = 3;i<v.size();i++){
if(v[i].first==x){
cost+=sum*v[i].second;
sum+=v[i].second;
}
}
}else if(x==y&&y==z){
int A = v[0].second , B = v[1].second , C = v[2].second;
cost = A*B+(A+B)*C;
long long sum = A+B+C;
for(int i = 3;i<v.size();i++){
if(v[i].first==x){
cost+=sum*v[i].second;
sum+=v[i].second;
}
}
}
if(overMA<(x+y)*z){
overMA = (x+y)*z;
overCNT = 0;
}
if(overMA==(x+y)*z){
overCNT+=cost;
}
}
pair<int,int> cur = {MA,CNT};
for(auto j:adj[i]){
if(j==pr)continue;
pref[j] = cur;
if(mx[j]+1>cur.first){
cur.first = mx[j]+1;
cur.second = 0;
}
if(mx[j]+1==cur.first){
cur.second+=cnt[j];
}
}
reverse(adj[i].begin(),adj[i].end());
cur = {-1e9,0};
for(auto j:adj[i]){
if(j==pr)continue;
if(cur.first>pref[j].first)pref[j] = cur;
else if(cur.first==pref[j].first)pref[j].second+=cur.second;
if(mx[j]+1>cur.first){
cur.first = mx[j]+1;
cur.second = 0;
}
if(mx[j]+1==cur.first){
cur.second+=cnt[j];
}
}
reverse(adj[i].begin(),adj[i].end());
for(auto j:adj[i]){
if(j==pr)continue;
reroot(j,i,pref[j].first+1,pref[j].second);
}
}
signed main(){
int n;
cin>>n;
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,0);
reroot(1,0,0,1);
cout<<overMA<<" "<<overCNT<<endl;
}
Compilation message
road.cpp: In function 'void reroot(long long int, long long int, long long int, long long int)':
road.cpp:39:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
| ~^~~~~~~~~
road.cpp:38:17: warning: unused variable 'A' [-Wunused-variable]
38 | int A = v[0].second , B = v[1].second , C = v[2].second;
| ^
road.cpp:43:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 3;i<v.size();i++)if(v[i].first==x)C+=v[i].second;
| ~^~~~~~~~~
road.cpp:49:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i = 3;i<v.size();i++){
| ~^~~~~~~~~
road.cpp:46:17: warning: unused variable 'A' [-Wunused-variable]
46 | int A = v[0].second , B = v[1].second , C = v[2].second;
| ^
road.cpp:59:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 3;i<v.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
17240 KB |
Output is correct |
2 |
Correct |
2 ms |
17244 KB |
Output is correct |
3 |
Correct |
2 ms |
17244 KB |
Output is correct |
4 |
Correct |
2 ms |
17244 KB |
Output is correct |
5 |
Correct |
2 ms |
17244 KB |
Output is correct |
6 |
Correct |
2 ms |
17244 KB |
Output is correct |
7 |
Correct |
2 ms |
17244 KB |
Output is correct |
8 |
Correct |
3 ms |
17244 KB |
Output is correct |
9 |
Correct |
2 ms |
17244 KB |
Output is correct |
10 |
Correct |
2 ms |
17244 KB |
Output is correct |
11 |
Correct |
2 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
17244 KB |
Output is correct |
13 |
Correct |
2 ms |
17244 KB |
Output is correct |
14 |
Correct |
2 ms |
17244 KB |
Output is correct |
15 |
Correct |
2 ms |
17244 KB |
Output is correct |
16 |
Correct |
2 ms |
17244 KB |
Output is correct |
17 |
Correct |
2 ms |
17244 KB |
Output is correct |
18 |
Correct |
2 ms |
17244 KB |
Output is correct |
19 |
Correct |
2 ms |
17244 KB |
Output is correct |
20 |
Correct |
2 ms |
17244 KB |
Output is correct |
21 |
Correct |
2 ms |
17244 KB |
Output is correct |
22 |
Correct |
3 ms |
17244 KB |
Output is correct |
23 |
Correct |
2 ms |
17244 KB |
Output is correct |
24 |
Correct |
2 ms |
17244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
17240 KB |
Output is correct |
2 |
Correct |
2 ms |
17244 KB |
Output is correct |
3 |
Correct |
2 ms |
17244 KB |
Output is correct |
4 |
Correct |
2 ms |
17244 KB |
Output is correct |
5 |
Correct |
2 ms |
17244 KB |
Output is correct |
6 |
Correct |
2 ms |
17244 KB |
Output is correct |
7 |
Correct |
2 ms |
17244 KB |
Output is correct |
8 |
Correct |
3 ms |
17244 KB |
Output is correct |
9 |
Correct |
2 ms |
17244 KB |
Output is correct |
10 |
Correct |
2 ms |
17244 KB |
Output is correct |
11 |
Correct |
2 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
17244 KB |
Output is correct |
13 |
Correct |
2 ms |
17244 KB |
Output is correct |
14 |
Correct |
2 ms |
17244 KB |
Output is correct |
15 |
Correct |
2 ms |
17244 KB |
Output is correct |
16 |
Correct |
2 ms |
17244 KB |
Output is correct |
17 |
Correct |
2 ms |
17244 KB |
Output is correct |
18 |
Correct |
2 ms |
17244 KB |
Output is correct |
19 |
Correct |
2 ms |
17244 KB |
Output is correct |
20 |
Correct |
2 ms |
17244 KB |
Output is correct |
21 |
Correct |
2 ms |
17244 KB |
Output is correct |
22 |
Correct |
3 ms |
17244 KB |
Output is correct |
23 |
Correct |
2 ms |
17244 KB |
Output is correct |
24 |
Correct |
2 ms |
17244 KB |
Output is correct |
25 |
Correct |
4 ms |
17796 KB |
Output is correct |
26 |
Correct |
4 ms |
18012 KB |
Output is correct |
27 |
Correct |
4 ms |
18012 KB |
Output is correct |
28 |
Correct |
4 ms |
18012 KB |
Output is correct |
29 |
Correct |
4 ms |
18012 KB |
Output is correct |
30 |
Correct |
4 ms |
18012 KB |
Output is correct |
31 |
Correct |
4 ms |
18008 KB |
Output is correct |
32 |
Correct |
4 ms |
18012 KB |
Output is correct |
33 |
Correct |
4 ms |
17844 KB |
Output is correct |
34 |
Correct |
4 ms |
18012 KB |
Output is correct |
35 |
Correct |
4 ms |
18108 KB |
Output is correct |
36 |
Correct |
4 ms |
18012 KB |
Output is correct |
37 |
Correct |
4 ms |
18012 KB |
Output is correct |
38 |
Correct |
4 ms |
18524 KB |
Output is correct |
39 |
Correct |
4 ms |
17756 KB |
Output is correct |
40 |
Correct |
4 ms |
17756 KB |
Output is correct |
41 |
Correct |
4 ms |
17500 KB |
Output is correct |
42 |
Correct |
4 ms |
17500 KB |
Output is correct |
43 |
Correct |
4 ms |
17540 KB |
Output is correct |
44 |
Correct |
4 ms |
17500 KB |
Output is correct |
45 |
Correct |
4 ms |
17500 KB |
Output is correct |
46 |
Correct |
3 ms |
17500 KB |
Output is correct |
47 |
Correct |
4 ms |
17500 KB |
Output is correct |
48 |
Correct |
4 ms |
17796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
17240 KB |
Output is correct |
2 |
Correct |
2 ms |
17244 KB |
Output is correct |
3 |
Correct |
2 ms |
17244 KB |
Output is correct |
4 |
Correct |
2 ms |
17244 KB |
Output is correct |
5 |
Correct |
2 ms |
17244 KB |
Output is correct |
6 |
Correct |
2 ms |
17244 KB |
Output is correct |
7 |
Correct |
2 ms |
17244 KB |
Output is correct |
8 |
Correct |
3 ms |
17244 KB |
Output is correct |
9 |
Correct |
2 ms |
17244 KB |
Output is correct |
10 |
Correct |
2 ms |
17244 KB |
Output is correct |
11 |
Correct |
2 ms |
17244 KB |
Output is correct |
12 |
Correct |
3 ms |
17244 KB |
Output is correct |
13 |
Correct |
2 ms |
17244 KB |
Output is correct |
14 |
Correct |
2 ms |
17244 KB |
Output is correct |
15 |
Correct |
2 ms |
17244 KB |
Output is correct |
16 |
Correct |
2 ms |
17244 KB |
Output is correct |
17 |
Correct |
2 ms |
17244 KB |
Output is correct |
18 |
Correct |
2 ms |
17244 KB |
Output is correct |
19 |
Correct |
2 ms |
17244 KB |
Output is correct |
20 |
Correct |
2 ms |
17244 KB |
Output is correct |
21 |
Correct |
2 ms |
17244 KB |
Output is correct |
22 |
Correct |
3 ms |
17244 KB |
Output is correct |
23 |
Correct |
2 ms |
17244 KB |
Output is correct |
24 |
Correct |
2 ms |
17244 KB |
Output is correct |
25 |
Correct |
4 ms |
17796 KB |
Output is correct |
26 |
Correct |
4 ms |
18012 KB |
Output is correct |
27 |
Correct |
4 ms |
18012 KB |
Output is correct |
28 |
Correct |
4 ms |
18012 KB |
Output is correct |
29 |
Correct |
4 ms |
18012 KB |
Output is correct |
30 |
Correct |
4 ms |
18012 KB |
Output is correct |
31 |
Correct |
4 ms |
18008 KB |
Output is correct |
32 |
Correct |
4 ms |
18012 KB |
Output is correct |
33 |
Correct |
4 ms |
17844 KB |
Output is correct |
34 |
Correct |
4 ms |
18012 KB |
Output is correct |
35 |
Correct |
4 ms |
18108 KB |
Output is correct |
36 |
Correct |
4 ms |
18012 KB |
Output is correct |
37 |
Correct |
4 ms |
18012 KB |
Output is correct |
38 |
Correct |
4 ms |
18524 KB |
Output is correct |
39 |
Correct |
4 ms |
17756 KB |
Output is correct |
40 |
Correct |
4 ms |
17756 KB |
Output is correct |
41 |
Correct |
4 ms |
17500 KB |
Output is correct |
42 |
Correct |
4 ms |
17500 KB |
Output is correct |
43 |
Correct |
4 ms |
17540 KB |
Output is correct |
44 |
Correct |
4 ms |
17500 KB |
Output is correct |
45 |
Correct |
4 ms |
17500 KB |
Output is correct |
46 |
Correct |
3 ms |
17500 KB |
Output is correct |
47 |
Correct |
4 ms |
17500 KB |
Output is correct |
48 |
Correct |
4 ms |
17796 KB |
Output is correct |
49 |
Correct |
353 ms |
82472 KB |
Output is correct |
50 |
Correct |
333 ms |
89668 KB |
Output is correct |
51 |
Correct |
345 ms |
95832 KB |
Output is correct |
52 |
Correct |
329 ms |
74168 KB |
Output is correct |
53 |
Correct |
300 ms |
95060 KB |
Output is correct |
54 |
Correct |
313 ms |
109744 KB |
Output is correct |
55 |
Correct |
283 ms |
90992 KB |
Output is correct |
56 |
Correct |
305 ms |
99036 KB |
Output is correct |
57 |
Correct |
335 ms |
106232 KB |
Output is correct |
58 |
Correct |
320 ms |
98128 KB |
Output is correct |
59 |
Correct |
335 ms |
97876 KB |
Output is correct |
60 |
Correct |
323 ms |
94036 KB |
Output is correct |
61 |
Correct |
473 ms |
151696 KB |
Output is correct |
62 |
Correct |
474 ms |
133312 KB |
Output is correct |
63 |
Correct |
443 ms |
79700 KB |
Output is correct |
64 |
Correct |
446 ms |
66904 KB |
Output is correct |
65 |
Correct |
407 ms |
58888 KB |
Output is correct |
66 |
Correct |
393 ms |
54352 KB |
Output is correct |
67 |
Correct |
382 ms |
52052 KB |
Output is correct |
68 |
Correct |
366 ms |
50964 KB |
Output is correct |
69 |
Correct |
380 ms |
50768 KB |
Output is correct |
70 |
Correct |
364 ms |
50492 KB |
Output is correct |
71 |
Correct |
348 ms |
50512 KB |
Output is correct |
72 |
Correct |
383 ms |
50512 KB |
Output is correct |
73 |
Correct |
368 ms |
50840 KB |
Output is correct |
74 |
Correct |
361 ms |
51024 KB |
Output is correct |
75 |
Correct |
352 ms |
51792 KB |
Output is correct |
76 |
Correct |
353 ms |
53120 KB |
Output is correct |
77 |
Correct |
320 ms |
56368 KB |
Output is correct |
78 |
Correct |
256 ms |
62396 KB |
Output is correct |