typedef long long ll;
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int n;
ll dp[500001][3][3],ans=0,say=1;
vector<int>komsu[500001];
void ekle(int loc,int x,ll cnt){
if(x>dp[loc][0][0]){
swap(dp[loc][1],dp[loc][2]);
swap(dp[loc][0],dp[loc][1]);
dp[loc][0][0]=x;
dp[loc][0][1]=0;
dp[loc][0][2]=0;
}
else if(x>dp[loc][1][0]){
swap(dp[loc][1],dp[loc][2]);
dp[loc][1][0]=x;
dp[loc][1][1]=0;
dp[loc][1][2]=0;
if(x==dp[loc][0][0]){
dp[loc][1][1]=dp[loc][0][1];
dp[loc][1][2]=dp[loc][0][2];
}
}
else if(x>dp[loc][2][0]){
dp[loc][2][0]=x;
dp[loc][2][1]=0;
dp[loc][2][2]=0;
if(x==dp[loc][1][0]){
dp[loc][2][1]=dp[loc][1][1];
dp[loc][2][2]=dp[loc][1][2];
}
}
for(int i=0;i<3;i++){
if(x==dp[loc][i][0]){
dp[loc][i][2]+=dp[loc][i][1]*cnt;
dp[loc][i][1]+=cnt;
}
}
}
void dfs1(int pos,int root){
dp[pos][0][1]=1;
for(int x:komsu[pos]){
if(x==root)continue;
dfs1(x,pos);
ekle(pos,dp[x][0][0]+1,dp[x][0][1]);
}
}
void dfs2(int pos,int root){
if(root){
if(dp[root][0][0]-1==dp[pos][0][0]){
ll y=dp[root][1][1];
if(dp[root][1][0]-1==dp[pos][0][0]){
y-=dp[pos][0][1];
}
ekle(pos,dp[root][1][0]+1,y);
}
else{
ekle(pos,dp[root][0][0]+1,dp[root][0][1]);
}
}
if(dp[pos][0][0]&&dp[pos][1][0]&&dp[pos][2][0]){
ll art=0;
if(dp[pos][1][0]==dp[pos][2][0]){
art=dp[pos][1][2];
}
else{
art=dp[pos][1][1]*dp[pos][2][1];
}
if(dp[pos][0][0]*(dp[pos][1][0]+dp[pos][2][0])>ans){
ans=dp[pos][0][0]*(dp[pos][1][0]+dp[pos][2][0]);
say=art;
}
else if(dp[pos][0][0]*(dp[pos][1][0]+dp[pos][2][0])==ans){
say+=art;
}
}
for(int x:komsu[pos]){
if(x==root)continue;
dfs2(x,pos);
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin>>n;
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
komsu[a].pb(b);
komsu[b].pb(a);
}
dfs1(1,0);
dfs2(1,0);
cout<<ans<<" "<<say;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12920 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12912 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
4 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12888 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12920 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12912 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
4 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12888 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12904 KB |
Output is correct |
25 |
Correct |
4 ms |
13148 KB |
Output is correct |
26 |
Correct |
4 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13080 KB |
Output is correct |
28 |
Correct |
4 ms |
13144 KB |
Output is correct |
29 |
Correct |
4 ms |
13144 KB |
Output is correct |
30 |
Correct |
4 ms |
13148 KB |
Output is correct |
31 |
Correct |
4 ms |
13148 KB |
Output is correct |
32 |
Correct |
6 ms |
13180 KB |
Output is correct |
33 |
Correct |
4 ms |
13148 KB |
Output is correct |
34 |
Correct |
6 ms |
13100 KB |
Output is correct |
35 |
Correct |
5 ms |
13088 KB |
Output is correct |
36 |
Correct |
6 ms |
13400 KB |
Output is correct |
37 |
Correct |
4 ms |
13144 KB |
Output is correct |
38 |
Correct |
6 ms |
13400 KB |
Output is correct |
39 |
Correct |
4 ms |
13148 KB |
Output is correct |
40 |
Correct |
5 ms |
13148 KB |
Output is correct |
41 |
Correct |
4 ms |
12892 KB |
Output is correct |
42 |
Correct |
4 ms |
12892 KB |
Output is correct |
43 |
Correct |
4 ms |
13144 KB |
Output is correct |
44 |
Correct |
4 ms |
12892 KB |
Output is correct |
45 |
Correct |
4 ms |
12892 KB |
Output is correct |
46 |
Correct |
4 ms |
12892 KB |
Output is correct |
47 |
Correct |
4 ms |
12888 KB |
Output is correct |
48 |
Correct |
5 ms |
12900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12920 KB |
Output is correct |
7 |
Correct |
3 ms |
12892 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12912 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
4 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12888 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12904 KB |
Output is correct |
25 |
Correct |
4 ms |
13148 KB |
Output is correct |
26 |
Correct |
4 ms |
13148 KB |
Output is correct |
27 |
Correct |
4 ms |
13080 KB |
Output is correct |
28 |
Correct |
4 ms |
13144 KB |
Output is correct |
29 |
Correct |
4 ms |
13144 KB |
Output is correct |
30 |
Correct |
4 ms |
13148 KB |
Output is correct |
31 |
Correct |
4 ms |
13148 KB |
Output is correct |
32 |
Correct |
6 ms |
13180 KB |
Output is correct |
33 |
Correct |
4 ms |
13148 KB |
Output is correct |
34 |
Correct |
6 ms |
13100 KB |
Output is correct |
35 |
Correct |
5 ms |
13088 KB |
Output is correct |
36 |
Correct |
6 ms |
13400 KB |
Output is correct |
37 |
Correct |
4 ms |
13144 KB |
Output is correct |
38 |
Correct |
6 ms |
13400 KB |
Output is correct |
39 |
Correct |
4 ms |
13148 KB |
Output is correct |
40 |
Correct |
5 ms |
13148 KB |
Output is correct |
41 |
Correct |
4 ms |
12892 KB |
Output is correct |
42 |
Correct |
4 ms |
12892 KB |
Output is correct |
43 |
Correct |
4 ms |
13144 KB |
Output is correct |
44 |
Correct |
4 ms |
12892 KB |
Output is correct |
45 |
Correct |
4 ms |
12892 KB |
Output is correct |
46 |
Correct |
4 ms |
12892 KB |
Output is correct |
47 |
Correct |
4 ms |
12888 KB |
Output is correct |
48 |
Correct |
5 ms |
12900 KB |
Output is correct |
49 |
Correct |
379 ms |
74928 KB |
Output is correct |
50 |
Correct |
391 ms |
77112 KB |
Output is correct |
51 |
Correct |
390 ms |
78932 KB |
Output is correct |
52 |
Correct |
376 ms |
72524 KB |
Output is correct |
53 |
Correct |
263 ms |
75592 KB |
Output is correct |
54 |
Correct |
333 ms |
77812 KB |
Output is correct |
55 |
Correct |
284 ms |
72528 KB |
Output is correct |
56 |
Correct |
355 ms |
74836 KB |
Output is correct |
57 |
Correct |
306 ms |
79952 KB |
Output is correct |
58 |
Correct |
330 ms |
77640 KB |
Output is correct |
59 |
Correct |
302 ms |
77652 KB |
Output is correct |
60 |
Correct |
287 ms |
76368 KB |
Output is correct |
61 |
Correct |
574 ms |
94036 KB |
Output is correct |
62 |
Correct |
548 ms |
88444 KB |
Output is correct |
63 |
Correct |
495 ms |
72016 KB |
Output is correct |
64 |
Correct |
513 ms |
67856 KB |
Output is correct |
65 |
Correct |
502 ms |
65620 KB |
Output is correct |
66 |
Correct |
464 ms |
64084 KB |
Output is correct |
67 |
Correct |
491 ms |
63528 KB |
Output is correct |
68 |
Correct |
514 ms |
63316 KB |
Output is correct |
69 |
Correct |
476 ms |
63124 KB |
Output is correct |
70 |
Correct |
480 ms |
62992 KB |
Output is correct |
71 |
Correct |
466 ms |
62860 KB |
Output is correct |
72 |
Correct |
517 ms |
63076 KB |
Output is correct |
73 |
Correct |
501 ms |
63348 KB |
Output is correct |
74 |
Correct |
489 ms |
63060 KB |
Output is correct |
75 |
Correct |
435 ms |
63260 KB |
Output is correct |
76 |
Correct |
454 ms |
63692 KB |
Output is correct |
77 |
Correct |
377 ms |
63940 KB |
Output is correct |
78 |
Correct |
233 ms |
64956 KB |
Output is correct |