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];
}
ll res=dp[pos][0][0]*(dp[pos][1][0]+dp[pos][2][0]);
if(res>ans){
ans=res;
say=art;
}
else if(res==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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12888 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 |
13140 KB |
Output is correct |
5 |
Correct |
3 ms |
13144 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 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 |
12888 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 |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12888 KB |
Output is correct |
17 |
Correct |
3 ms |
12888 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
13144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12888 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 |
13140 KB |
Output is correct |
5 |
Correct |
3 ms |
13144 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 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 |
12888 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 |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12888 KB |
Output is correct |
17 |
Correct |
3 ms |
12888 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
13144 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 |
13148 KB |
Output is correct |
28 |
Correct |
4 ms |
13148 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 |
4 ms |
13148 KB |
Output is correct |
33 |
Correct |
5 ms |
13148 KB |
Output is correct |
34 |
Correct |
4 ms |
13148 KB |
Output is correct |
35 |
Correct |
4 ms |
13148 KB |
Output is correct |
36 |
Correct |
4 ms |
13148 KB |
Output is correct |
37 |
Correct |
5 ms |
13148 KB |
Output is correct |
38 |
Correct |
5 ms |
13404 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 |
5 ms |
13112 KB |
Output is correct |
43 |
Correct |
4 ms |
12892 KB |
Output is correct |
44 |
Correct |
4 ms |
13108 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 |
13072 KB |
Output is correct |
48 |
Correct |
4 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12888 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 |
13140 KB |
Output is correct |
5 |
Correct |
3 ms |
13144 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
3 ms |
12888 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 |
12888 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 |
12888 KB |
Output is correct |
16 |
Correct |
3 ms |
12888 KB |
Output is correct |
17 |
Correct |
3 ms |
12888 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
3 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12892 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
3 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
13144 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 |
13148 KB |
Output is correct |
28 |
Correct |
4 ms |
13148 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 |
4 ms |
13148 KB |
Output is correct |
33 |
Correct |
5 ms |
13148 KB |
Output is correct |
34 |
Correct |
4 ms |
13148 KB |
Output is correct |
35 |
Correct |
4 ms |
13148 KB |
Output is correct |
36 |
Correct |
4 ms |
13148 KB |
Output is correct |
37 |
Correct |
5 ms |
13148 KB |
Output is correct |
38 |
Correct |
5 ms |
13404 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 |
5 ms |
13112 KB |
Output is correct |
43 |
Correct |
4 ms |
12892 KB |
Output is correct |
44 |
Correct |
4 ms |
13108 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 |
13072 KB |
Output is correct |
48 |
Correct |
4 ms |
12892 KB |
Output is correct |
49 |
Correct |
351 ms |
74840 KB |
Output is correct |
50 |
Correct |
353 ms |
77112 KB |
Output is correct |
51 |
Correct |
327 ms |
78984 KB |
Output is correct |
52 |
Correct |
340 ms |
72532 KB |
Output is correct |
53 |
Correct |
274 ms |
75856 KB |
Output is correct |
54 |
Correct |
277 ms |
77652 KB |
Output is correct |
55 |
Correct |
283 ms |
72724 KB |
Output is correct |
56 |
Correct |
299 ms |
74960 KB |
Output is correct |
57 |
Correct |
316 ms |
80152 KB |
Output is correct |
58 |
Correct |
310 ms |
77724 KB |
Output is correct |
59 |
Correct |
303 ms |
77536 KB |
Output is correct |
60 |
Correct |
300 ms |
76448 KB |
Output is correct |
61 |
Correct |
517 ms |
94208 KB |
Output is correct |
62 |
Correct |
511 ms |
88544 KB |
Output is correct |
63 |
Correct |
525 ms |
72268 KB |
Output is correct |
64 |
Correct |
530 ms |
68028 KB |
Output is correct |
65 |
Correct |
488 ms |
65560 KB |
Output is correct |
66 |
Correct |
519 ms |
64336 KB |
Output is correct |
67 |
Correct |
512 ms |
63524 KB |
Output is correct |
68 |
Correct |
471 ms |
63288 KB |
Output is correct |
69 |
Correct |
488 ms |
63120 KB |
Output is correct |
70 |
Correct |
459 ms |
62804 KB |
Output is correct |
71 |
Correct |
461 ms |
62860 KB |
Output is correct |
72 |
Correct |
471 ms |
63060 KB |
Output is correct |
73 |
Correct |
482 ms |
63096 KB |
Output is correct |
74 |
Correct |
444 ms |
63224 KB |
Output is correct |
75 |
Correct |
434 ms |
63064 KB |
Output is correct |
76 |
Correct |
457 ms |
63432 KB |
Output is correct |
77 |
Correct |
372 ms |
63936 KB |
Output is correct |
78 |
Correct |
227 ms |
64868 KB |
Output is correct |