#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define pii pair<int,int>
#define x first
#define y second
#define N 500005
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,fa[N];
long long ans=-1,cnt=0,a=0,b=0;
pii dp[N],dp1[N];
vector<int>g[N];
map<int,pii>have[N];
void dfs(int p,int lp){
fa[p]=lp; dp[p]={0,0};
if (g[p].size()==1){
dp[p].y=1;
return;
}
for (auto i:g[p]){
if (i!=lp){
dfs(i,p);
if (dp[p].x==dp[i].x+1)
dp[p].y+=dp[i].y;
else dp[p]=max(dp[p],pii(dp[i].x+1,dp[i].y));
}
}
}
void dfs1(int p,int lp,pii val){
dp1[p]={val.x+1,val.y};
pii mx1=val,mx2={-1,0};
for (auto i:g[p]){
if (i!=lp){
if (mx1.x==dp[i].x) mx1.y+=dp[i].y;
else if (mx2.x==dp[i].x) mx2.y+=dp[i].y;
else mx2=max(mx2,dp[i]);
if (mx1<mx2) swap(mx1,mx2);
}
}
mx1.x++; mx2.x++;
for (auto i:g[p]){
if (i!=lp){
if (dp[i].x==mx1.x-1&&dp[i].y==mx1.y)
dfs1(i,p,mx2);
else if (dp[i].x==mx1.x-1)
dfs1(i,p,{mx1.x,mx1.y-dp[i].y});
else dfs1(i,p,mx1);
}
}
}
void relax(int x,int y){
swap(x,y);
if ((long long)x*y>ans){
ans=x*y;
a=x; b=y;
}
}
void solve(){
for (int j=1;j<=n;j++){
vector<int>dep;
for (auto i:g[j]){
if (i!=fa[j])
dep.push_back(dp[i].x+1);
}
if (dp1[j].y) dep.push_back(dp1[j].x);
sort(dep.begin(),dep.end());
if (dep.size()>2){
int b1=dep.back(),b2=dep[dep.size()-2],b3=dep[dep.size()-3];
for (int i=0;i<(int)(dep.size())-2;i++)
relax(dep[i],(b2+b1));
relax(b2,(b1+b3)); relax(b1,(b2+b3));
}
}
}
void dfs2(int p,int lp){
if (g[p].size()==1) return;
for (auto i:g[p]){
have[p][dp[i].x+1].x+=dp[i].y;
have[p][dp[i].x+1].y++;
}
for (auto i:g[p]){
auto [d,c]=dp[i]; d++;
pii tmp=dp[p];
have[p][d].x-=c; have[p][d].y--;
int have_find=0;
if (have[p].count(a-d)&&have[p][a-d].x){
have[p][a-d].y--;
if (have[p].count(b)&&have[p][b].y)
cnt=(cnt+(long long)c*have[p][a-d].x),have_find=1;
have[p][a-d].y++;
}
if (have[p].count(b-d)&&have[p][b-d].x){
have[p][b-d].y--;
if (have[p].count(a)&&have[p][a].y)
cnt=(cnt+(long long)c*have[p][b-d].x),have_find=1;
have[p][b-d].y++;
}
if (!have_find){
if (have[p].count(dp1[i].x-1))
dp[p]={dp1[i].x-1,have[p][dp1[i].x-1].x};
else dp[p]={dp1[i].x-1,0};
}
if (i!=lp)
dfs2(i,p);
dp[p]=tmp;
have[p][d].x+=c; have[p][d].y++;
}
map<int,pii>().swap(have[p]);
}
signed main(){
fast
cin>>n;
for (int i=1;i<n;i++){
int a,b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
int root=0;
for (int i=1;i<=n;i++){
if (g[i].size()>1){
root=i;
break;
}
}
dfs(root,0);
dfs1(root,0,pii(-1,0));
solve();
dfs2(root,0);
if (a==b) cnt/=2;
if (ans<=0) cout<<0<<' '<<1<<'\n';
else cout<<ans<<' '<<cnt/2<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35576 KB |
Output is correct |
2 |
Correct |
20 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35504 KB |
Output is correct |
4 |
Correct |
21 ms |
35532 KB |
Output is correct |
5 |
Correct |
20 ms |
35540 KB |
Output is correct |
6 |
Correct |
19 ms |
35540 KB |
Output is correct |
7 |
Correct |
20 ms |
35532 KB |
Output is correct |
8 |
Correct |
18 ms |
35540 KB |
Output is correct |
9 |
Correct |
18 ms |
35580 KB |
Output is correct |
10 |
Correct |
18 ms |
35504 KB |
Output is correct |
11 |
Correct |
18 ms |
35600 KB |
Output is correct |
12 |
Correct |
19 ms |
35528 KB |
Output is correct |
13 |
Correct |
23 ms |
35540 KB |
Output is correct |
14 |
Correct |
23 ms |
35552 KB |
Output is correct |
15 |
Correct |
19 ms |
35512 KB |
Output is correct |
16 |
Correct |
19 ms |
35572 KB |
Output is correct |
17 |
Correct |
19 ms |
35596 KB |
Output is correct |
18 |
Correct |
19 ms |
35540 KB |
Output is correct |
19 |
Correct |
20 ms |
35604 KB |
Output is correct |
20 |
Correct |
19 ms |
35540 KB |
Output is correct |
21 |
Correct |
20 ms |
35540 KB |
Output is correct |
22 |
Correct |
19 ms |
35568 KB |
Output is correct |
23 |
Correct |
18 ms |
35520 KB |
Output is correct |
24 |
Correct |
21 ms |
35576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35576 KB |
Output is correct |
2 |
Correct |
20 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35504 KB |
Output is correct |
4 |
Correct |
21 ms |
35532 KB |
Output is correct |
5 |
Correct |
20 ms |
35540 KB |
Output is correct |
6 |
Correct |
19 ms |
35540 KB |
Output is correct |
7 |
Correct |
20 ms |
35532 KB |
Output is correct |
8 |
Correct |
18 ms |
35540 KB |
Output is correct |
9 |
Correct |
18 ms |
35580 KB |
Output is correct |
10 |
Correct |
18 ms |
35504 KB |
Output is correct |
11 |
Correct |
18 ms |
35600 KB |
Output is correct |
12 |
Correct |
19 ms |
35528 KB |
Output is correct |
13 |
Correct |
23 ms |
35540 KB |
Output is correct |
14 |
Correct |
23 ms |
35552 KB |
Output is correct |
15 |
Correct |
19 ms |
35512 KB |
Output is correct |
16 |
Correct |
19 ms |
35572 KB |
Output is correct |
17 |
Correct |
19 ms |
35596 KB |
Output is correct |
18 |
Correct |
19 ms |
35540 KB |
Output is correct |
19 |
Correct |
20 ms |
35604 KB |
Output is correct |
20 |
Correct |
19 ms |
35540 KB |
Output is correct |
21 |
Correct |
20 ms |
35540 KB |
Output is correct |
22 |
Correct |
19 ms |
35568 KB |
Output is correct |
23 |
Correct |
18 ms |
35520 KB |
Output is correct |
24 |
Correct |
21 ms |
35576 KB |
Output is correct |
25 |
Correct |
21 ms |
36192 KB |
Output is correct |
26 |
Correct |
20 ms |
36564 KB |
Output is correct |
27 |
Correct |
21 ms |
36180 KB |
Output is correct |
28 |
Correct |
21 ms |
36652 KB |
Output is correct |
29 |
Correct |
21 ms |
36564 KB |
Output is correct |
30 |
Correct |
22 ms |
36656 KB |
Output is correct |
31 |
Correct |
25 ms |
36688 KB |
Output is correct |
32 |
Correct |
21 ms |
36436 KB |
Output is correct |
33 |
Correct |
30 ms |
36540 KB |
Output is correct |
34 |
Correct |
24 ms |
36740 KB |
Output is correct |
35 |
Correct |
22 ms |
36640 KB |
Output is correct |
36 |
Correct |
22 ms |
36588 KB |
Output is correct |
37 |
Correct |
22 ms |
36792 KB |
Output is correct |
38 |
Correct |
22 ms |
37460 KB |
Output is correct |
39 |
Correct |
21 ms |
36396 KB |
Output is correct |
40 |
Correct |
22 ms |
36024 KB |
Output is correct |
41 |
Correct |
22 ms |
35940 KB |
Output is correct |
42 |
Correct |
21 ms |
35832 KB |
Output is correct |
43 |
Correct |
21 ms |
35796 KB |
Output is correct |
44 |
Correct |
24 ms |
35836 KB |
Output is correct |
45 |
Correct |
22 ms |
35756 KB |
Output is correct |
46 |
Correct |
20 ms |
35716 KB |
Output is correct |
47 |
Correct |
21 ms |
35796 KB |
Output is correct |
48 |
Correct |
20 ms |
35904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35576 KB |
Output is correct |
2 |
Correct |
20 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35504 KB |
Output is correct |
4 |
Correct |
21 ms |
35532 KB |
Output is correct |
5 |
Correct |
20 ms |
35540 KB |
Output is correct |
6 |
Correct |
19 ms |
35540 KB |
Output is correct |
7 |
Correct |
20 ms |
35532 KB |
Output is correct |
8 |
Correct |
18 ms |
35540 KB |
Output is correct |
9 |
Correct |
18 ms |
35580 KB |
Output is correct |
10 |
Correct |
18 ms |
35504 KB |
Output is correct |
11 |
Correct |
18 ms |
35600 KB |
Output is correct |
12 |
Correct |
19 ms |
35528 KB |
Output is correct |
13 |
Correct |
23 ms |
35540 KB |
Output is correct |
14 |
Correct |
23 ms |
35552 KB |
Output is correct |
15 |
Correct |
19 ms |
35512 KB |
Output is correct |
16 |
Correct |
19 ms |
35572 KB |
Output is correct |
17 |
Correct |
19 ms |
35596 KB |
Output is correct |
18 |
Correct |
19 ms |
35540 KB |
Output is correct |
19 |
Correct |
20 ms |
35604 KB |
Output is correct |
20 |
Correct |
19 ms |
35540 KB |
Output is correct |
21 |
Correct |
20 ms |
35540 KB |
Output is correct |
22 |
Correct |
19 ms |
35568 KB |
Output is correct |
23 |
Correct |
18 ms |
35520 KB |
Output is correct |
24 |
Correct |
21 ms |
35576 KB |
Output is correct |
25 |
Correct |
21 ms |
36192 KB |
Output is correct |
26 |
Correct |
20 ms |
36564 KB |
Output is correct |
27 |
Correct |
21 ms |
36180 KB |
Output is correct |
28 |
Correct |
21 ms |
36652 KB |
Output is correct |
29 |
Correct |
21 ms |
36564 KB |
Output is correct |
30 |
Correct |
22 ms |
36656 KB |
Output is correct |
31 |
Correct |
25 ms |
36688 KB |
Output is correct |
32 |
Correct |
21 ms |
36436 KB |
Output is correct |
33 |
Correct |
30 ms |
36540 KB |
Output is correct |
34 |
Correct |
24 ms |
36740 KB |
Output is correct |
35 |
Correct |
22 ms |
36640 KB |
Output is correct |
36 |
Correct |
22 ms |
36588 KB |
Output is correct |
37 |
Correct |
22 ms |
36792 KB |
Output is correct |
38 |
Correct |
22 ms |
37460 KB |
Output is correct |
39 |
Correct |
21 ms |
36396 KB |
Output is correct |
40 |
Correct |
22 ms |
36024 KB |
Output is correct |
41 |
Correct |
22 ms |
35940 KB |
Output is correct |
42 |
Correct |
21 ms |
35832 KB |
Output is correct |
43 |
Correct |
21 ms |
35796 KB |
Output is correct |
44 |
Correct |
24 ms |
35836 KB |
Output is correct |
45 |
Correct |
22 ms |
35756 KB |
Output is correct |
46 |
Correct |
20 ms |
35716 KB |
Output is correct |
47 |
Correct |
21 ms |
35796 KB |
Output is correct |
48 |
Correct |
20 ms |
35904 KB |
Output is correct |
49 |
Correct |
721 ms |
124960 KB |
Output is correct |
50 |
Correct |
692 ms |
137004 KB |
Output is correct |
51 |
Correct |
760 ms |
147308 KB |
Output is correct |
52 |
Correct |
720 ms |
110884 KB |
Output is correct |
53 |
Correct |
658 ms |
162156 KB |
Output is correct |
54 |
Correct |
679 ms |
154228 KB |
Output is correct |
55 |
Correct |
632 ms |
131736 KB |
Output is correct |
56 |
Correct |
616 ms |
112104 KB |
Output is correct |
57 |
Correct |
714 ms |
155060 KB |
Output is correct |
58 |
Correct |
726 ms |
141464 KB |
Output is correct |
59 |
Correct |
705 ms |
141404 KB |
Output is correct |
60 |
Correct |
745 ms |
134944 KB |
Output is correct |
61 |
Correct |
1066 ms |
239524 KB |
Output is correct |
62 |
Correct |
1106 ms |
208296 KB |
Output is correct |
63 |
Correct |
1091 ms |
117496 KB |
Output is correct |
64 |
Correct |
1074 ms |
95912 KB |
Output is correct |
65 |
Correct |
999 ms |
81868 KB |
Output is correct |
66 |
Correct |
1022 ms |
74772 KB |
Output is correct |
67 |
Correct |
974 ms |
70720 KB |
Output is correct |
68 |
Correct |
915 ms |
69264 KB |
Output is correct |
69 |
Correct |
908 ms |
68708 KB |
Output is correct |
70 |
Correct |
893 ms |
68232 KB |
Output is correct |
71 |
Correct |
909 ms |
68192 KB |
Output is correct |
72 |
Correct |
862 ms |
68200 KB |
Output is correct |
73 |
Correct |
906 ms |
68356 KB |
Output is correct |
74 |
Correct |
861 ms |
68448 KB |
Output is correct |
75 |
Correct |
867 ms |
68764 KB |
Output is correct |
76 |
Correct |
821 ms |
69196 KB |
Output is correct |
77 |
Correct |
616 ms |
70472 KB |
Output is correct |
78 |
Correct |
387 ms |
73344 KB |
Output is correct |