#pragma GCC optimize("O3,fast-math,unroll-all-loops")
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(komsu[pos].size()>2){
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
12888 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
13144 KB |
Output is correct |
5 |
Correct |
4 ms |
12892 KB |
Output is correct |
6 |
Correct |
5 ms |
12888 KB |
Output is correct |
7 |
Correct |
4 ms |
12892 KB |
Output is correct |
8 |
Correct |
4 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
12892 KB |
Output is correct |
10 |
Correct |
4 ms |
12892 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12892 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
12912 KB |
Output is correct |
20 |
Correct |
4 ms |
12892 KB |
Output is correct |
21 |
Correct |
4 ms |
12888 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
4 ms |
12952 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
12888 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
13144 KB |
Output is correct |
5 |
Correct |
4 ms |
12892 KB |
Output is correct |
6 |
Correct |
5 ms |
12888 KB |
Output is correct |
7 |
Correct |
4 ms |
12892 KB |
Output is correct |
8 |
Correct |
4 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
12892 KB |
Output is correct |
10 |
Correct |
4 ms |
12892 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12892 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
12912 KB |
Output is correct |
20 |
Correct |
4 ms |
12892 KB |
Output is correct |
21 |
Correct |
4 ms |
12888 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
4 ms |
12952 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
5 ms |
13176 KB |
Output is correct |
26 |
Correct |
5 ms |
13148 KB |
Output is correct |
27 |
Correct |
5 ms |
13148 KB |
Output is correct |
28 |
Correct |
5 ms |
13148 KB |
Output is correct |
29 |
Correct |
5 ms |
13148 KB |
Output is correct |
30 |
Correct |
6 ms |
13400 KB |
Output is correct |
31 |
Correct |
5 ms |
13148 KB |
Output is correct |
32 |
Correct |
5 ms |
13148 KB |
Output is correct |
33 |
Correct |
5 ms |
13148 KB |
Output is correct |
34 |
Correct |
5 ms |
13148 KB |
Output is correct |
35 |
Correct |
6 ms |
13148 KB |
Output is correct |
36 |
Correct |
5 ms |
13148 KB |
Output is correct |
37 |
Correct |
5 ms |
13148 KB |
Output is correct |
38 |
Correct |
5 ms |
13144 KB |
Output is correct |
39 |
Correct |
5 ms |
13148 KB |
Output is correct |
40 |
Correct |
5 ms |
12888 KB |
Output is correct |
41 |
Correct |
6 ms |
12892 KB |
Output is correct |
42 |
Correct |
6 ms |
12892 KB |
Output is correct |
43 |
Correct |
5 ms |
12888 KB |
Output is correct |
44 |
Correct |
5 ms |
12892 KB |
Output is correct |
45 |
Correct |
5 ms |
12888 KB |
Output is correct |
46 |
Correct |
5 ms |
12892 KB |
Output is correct |
47 |
Correct |
5 ms |
12892 KB |
Output is correct |
48 |
Correct |
5 ms |
12900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
12888 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
13144 KB |
Output is correct |
5 |
Correct |
4 ms |
12892 KB |
Output is correct |
6 |
Correct |
5 ms |
12888 KB |
Output is correct |
7 |
Correct |
4 ms |
12892 KB |
Output is correct |
8 |
Correct |
4 ms |
12892 KB |
Output is correct |
9 |
Correct |
4 ms |
12892 KB |
Output is correct |
10 |
Correct |
4 ms |
12892 KB |
Output is correct |
11 |
Correct |
4 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12892 KB |
Output is correct |
13 |
Correct |
4 ms |
12892 KB |
Output is correct |
14 |
Correct |
4 ms |
12892 KB |
Output is correct |
15 |
Correct |
4 ms |
12892 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
4 ms |
12892 KB |
Output is correct |
18 |
Correct |
4 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
12912 KB |
Output is correct |
20 |
Correct |
4 ms |
12892 KB |
Output is correct |
21 |
Correct |
4 ms |
12888 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
4 ms |
12952 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
5 ms |
13176 KB |
Output is correct |
26 |
Correct |
5 ms |
13148 KB |
Output is correct |
27 |
Correct |
5 ms |
13148 KB |
Output is correct |
28 |
Correct |
5 ms |
13148 KB |
Output is correct |
29 |
Correct |
5 ms |
13148 KB |
Output is correct |
30 |
Correct |
6 ms |
13400 KB |
Output is correct |
31 |
Correct |
5 ms |
13148 KB |
Output is correct |
32 |
Correct |
5 ms |
13148 KB |
Output is correct |
33 |
Correct |
5 ms |
13148 KB |
Output is correct |
34 |
Correct |
5 ms |
13148 KB |
Output is correct |
35 |
Correct |
6 ms |
13148 KB |
Output is correct |
36 |
Correct |
5 ms |
13148 KB |
Output is correct |
37 |
Correct |
5 ms |
13148 KB |
Output is correct |
38 |
Correct |
5 ms |
13144 KB |
Output is correct |
39 |
Correct |
5 ms |
13148 KB |
Output is correct |
40 |
Correct |
5 ms |
12888 KB |
Output is correct |
41 |
Correct |
6 ms |
12892 KB |
Output is correct |
42 |
Correct |
6 ms |
12892 KB |
Output is correct |
43 |
Correct |
5 ms |
12888 KB |
Output is correct |
44 |
Correct |
5 ms |
12892 KB |
Output is correct |
45 |
Correct |
5 ms |
12888 KB |
Output is correct |
46 |
Correct |
5 ms |
12892 KB |
Output is correct |
47 |
Correct |
5 ms |
12892 KB |
Output is correct |
48 |
Correct |
5 ms |
12900 KB |
Output is correct |
49 |
Correct |
325 ms |
72012 KB |
Output is correct |
50 |
Correct |
379 ms |
73412 KB |
Output is correct |
51 |
Correct |
314 ms |
74836 KB |
Output is correct |
52 |
Correct |
353 ms |
70128 KB |
Output is correct |
53 |
Correct |
261 ms |
72528 KB |
Output is correct |
54 |
Correct |
316 ms |
74276 KB |
Output is correct |
55 |
Correct |
289 ms |
70156 KB |
Output is correct |
56 |
Correct |
256 ms |
71760 KB |
Output is correct |
57 |
Correct |
313 ms |
75860 KB |
Output is correct |
58 |
Correct |
301 ms |
73808 KB |
Output is correct |
59 |
Correct |
298 ms |
73884 KB |
Output is correct |
60 |
Correct |
283 ms |
73040 KB |
Output is correct |
61 |
Correct |
498 ms |
86356 KB |
Output is correct |
62 |
Correct |
511 ms |
82084 KB |
Output is correct |
63 |
Correct |
494 ms |
69744 KB |
Output is correct |
64 |
Correct |
490 ms |
66896 KB |
Output is correct |
65 |
Correct |
481 ms |
64852 KB |
Output is correct |
66 |
Correct |
467 ms |
64080 KB |
Output is correct |
67 |
Correct |
460 ms |
63568 KB |
Output is correct |
68 |
Correct |
440 ms |
63060 KB |
Output is correct |
69 |
Correct |
476 ms |
63340 KB |
Output is correct |
70 |
Correct |
479 ms |
62920 KB |
Output is correct |
71 |
Correct |
497 ms |
62844 KB |
Output is correct |
72 |
Correct |
481 ms |
63228 KB |
Output is correct |
73 |
Correct |
468 ms |
63096 KB |
Output is correct |
74 |
Correct |
462 ms |
63220 KB |
Output is correct |
75 |
Correct |
458 ms |
63180 KB |
Output is correct |
76 |
Correct |
428 ms |
63624 KB |
Output is correct |
77 |
Correct |
331 ms |
63936 KB |
Output is correct |
78 |
Correct |
211 ms |
64956 KB |
Output is correct |