#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,tot;
int mx[500005],cnt[500005];
vector<int> g[500005];
ll res=0,c=1;
void addedge(int x,int y){ g[x].pb(y); g[y].pb(x); }
void dfs(int x,int px,int d){
mx[x]=d;
cnt[x]=1;
for(auto&v:g[x]){
if(v==px) continue;
dfs(v,x,d+1);
if(mx[v]==mx[x]) cnt[x]+=cnt[v];
else if(mx[x]<mx[v]) cnt[x]=cnt[v];
mx[x]=max(mx[x],mx[v]);
}
}
void reroot(int x,int px,int dep){
for(auto&v:g[x]){
if(v!=px) mx[v]-=dep;
}
sort(g[x].begin(),g[x].end(),[&](int i,int j){return mx[i]>mx[j];});
if((int)g[x].size()>=3){
ll cur=0,cc=0;
cur=mx[g[x][0]]*1ll*(mx[g[x][1]]+mx[g[x][2]]);
ll s=0;
for(int j=0;j<(int)g[x].size();j++){
if(mx[g[x][j]]==mx[g[x][2]]) cc+=s*cnt[g[x][j]];
if(mx[g[x][j]]==mx[g[x][1]]) s+=cnt[g[x][j]];
}
if(res<cur) res=cur,c=cc;
else if(res==cur) c+=cc;
}
int mx_bef=mx[x],cnt_bef=cnt[x];
int a=0,b=1,c=-1,d=0;
for(auto&v:g[x]){
if(mx[v]>a) c=a,d=b,a=mx[v],b=cnt[v];
else if(mx[v]==a) b+=cnt[v];
else if(mx[v]>c) c=mx[v],d=cnt[v];
else if(mx[v]==c) d+=cnt[v];
}
for(auto&v:g[x]){
if(v==px) continue;
mx[x]=(mx[v]==a&&cnt[v]==b?c:a)+1;
cnt[x]=(mx[v]==a&&cnt[v]==b?d:b-(mx[v]==a?cnt[v]:0));
reroot(v,x,dep+1);
}
mx[x]=mx_bef;
cnt[x]=cnt_bef;
}
int main(){
n=readint();
for(int i=1;i<n;i++) addedge(readint(),readint());
dfs(1,1,0);
reroot(1,1,0);
printf("%lld %lld\n",res,c);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14960 KB |
Output is correct |
5 |
Correct |
5 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
6 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14936 KB |
Output is correct |
10 |
Correct |
4 ms |
14956 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
7 ms |
14940 KB |
Output is correct |
14 |
Correct |
5 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
7 ms |
14936 KB |
Output is correct |
19 |
Correct |
5 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
5 ms |
14956 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14960 KB |
Output is correct |
5 |
Correct |
5 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
6 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14936 KB |
Output is correct |
10 |
Correct |
4 ms |
14956 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
7 ms |
14940 KB |
Output is correct |
14 |
Correct |
5 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
7 ms |
14936 KB |
Output is correct |
19 |
Correct |
5 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
5 ms |
14956 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
5 ms |
15196 KB |
Output is correct |
26 |
Correct |
5 ms |
15196 KB |
Output is correct |
27 |
Correct |
5 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
15196 KB |
Output is correct |
29 |
Correct |
6 ms |
15196 KB |
Output is correct |
30 |
Correct |
5 ms |
15196 KB |
Output is correct |
31 |
Correct |
5 ms |
15196 KB |
Output is correct |
32 |
Correct |
6 ms |
15192 KB |
Output is correct |
33 |
Correct |
5 ms |
15196 KB |
Output is correct |
34 |
Correct |
6 ms |
15192 KB |
Output is correct |
35 |
Correct |
5 ms |
15196 KB |
Output is correct |
36 |
Correct |
5 ms |
15196 KB |
Output is correct |
37 |
Correct |
5 ms |
15452 KB |
Output is correct |
38 |
Correct |
5 ms |
15452 KB |
Output is correct |
39 |
Correct |
5 ms |
15196 KB |
Output is correct |
40 |
Correct |
5 ms |
15196 KB |
Output is correct |
41 |
Correct |
6 ms |
14940 KB |
Output is correct |
42 |
Correct |
5 ms |
14936 KB |
Output is correct |
43 |
Correct |
6 ms |
14940 KB |
Output is correct |
44 |
Correct |
4 ms |
14940 KB |
Output is correct |
45 |
Correct |
5 ms |
14940 KB |
Output is correct |
46 |
Correct |
6 ms |
15192 KB |
Output is correct |
47 |
Correct |
5 ms |
15140 KB |
Output is correct |
48 |
Correct |
5 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
14940 KB |
Output is correct |
2 |
Correct |
4 ms |
14940 KB |
Output is correct |
3 |
Correct |
4 ms |
14940 KB |
Output is correct |
4 |
Correct |
4 ms |
14960 KB |
Output is correct |
5 |
Correct |
5 ms |
14940 KB |
Output is correct |
6 |
Correct |
4 ms |
14940 KB |
Output is correct |
7 |
Correct |
4 ms |
14940 KB |
Output is correct |
8 |
Correct |
6 ms |
14940 KB |
Output is correct |
9 |
Correct |
5 ms |
14936 KB |
Output is correct |
10 |
Correct |
4 ms |
14956 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
3 ms |
14940 KB |
Output is correct |
13 |
Correct |
7 ms |
14940 KB |
Output is correct |
14 |
Correct |
5 ms |
14940 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
4 ms |
14940 KB |
Output is correct |
17 |
Correct |
4 ms |
14940 KB |
Output is correct |
18 |
Correct |
7 ms |
14936 KB |
Output is correct |
19 |
Correct |
5 ms |
14940 KB |
Output is correct |
20 |
Correct |
4 ms |
14940 KB |
Output is correct |
21 |
Correct |
4 ms |
14940 KB |
Output is correct |
22 |
Correct |
5 ms |
14956 KB |
Output is correct |
23 |
Correct |
4 ms |
14936 KB |
Output is correct |
24 |
Correct |
4 ms |
14936 KB |
Output is correct |
25 |
Correct |
5 ms |
15196 KB |
Output is correct |
26 |
Correct |
5 ms |
15196 KB |
Output is correct |
27 |
Correct |
5 ms |
15196 KB |
Output is correct |
28 |
Correct |
5 ms |
15196 KB |
Output is correct |
29 |
Correct |
6 ms |
15196 KB |
Output is correct |
30 |
Correct |
5 ms |
15196 KB |
Output is correct |
31 |
Correct |
5 ms |
15196 KB |
Output is correct |
32 |
Correct |
6 ms |
15192 KB |
Output is correct |
33 |
Correct |
5 ms |
15196 KB |
Output is correct |
34 |
Correct |
6 ms |
15192 KB |
Output is correct |
35 |
Correct |
5 ms |
15196 KB |
Output is correct |
36 |
Correct |
5 ms |
15196 KB |
Output is correct |
37 |
Correct |
5 ms |
15452 KB |
Output is correct |
38 |
Correct |
5 ms |
15452 KB |
Output is correct |
39 |
Correct |
5 ms |
15196 KB |
Output is correct |
40 |
Correct |
5 ms |
15196 KB |
Output is correct |
41 |
Correct |
6 ms |
14940 KB |
Output is correct |
42 |
Correct |
5 ms |
14936 KB |
Output is correct |
43 |
Correct |
6 ms |
14940 KB |
Output is correct |
44 |
Correct |
4 ms |
14940 KB |
Output is correct |
45 |
Correct |
5 ms |
14940 KB |
Output is correct |
46 |
Correct |
6 ms |
15192 KB |
Output is correct |
47 |
Correct |
5 ms |
15140 KB |
Output is correct |
48 |
Correct |
5 ms |
14940 KB |
Output is correct |
49 |
Correct |
421 ms |
52052 KB |
Output is correct |
50 |
Correct |
419 ms |
56144 KB |
Output is correct |
51 |
Correct |
352 ms |
59396 KB |
Output is correct |
52 |
Correct |
400 ms |
47928 KB |
Output is correct |
53 |
Correct |
276 ms |
53800 KB |
Output is correct |
54 |
Correct |
349 ms |
57560 KB |
Output is correct |
55 |
Correct |
274 ms |
48560 KB |
Output is correct |
56 |
Correct |
284 ms |
52412 KB |
Output is correct |
57 |
Correct |
329 ms |
61456 KB |
Output is correct |
58 |
Correct |
355 ms |
57168 KB |
Output is correct |
59 |
Correct |
378 ms |
57304 KB |
Output is correct |
60 |
Correct |
327 ms |
55040 KB |
Output is correct |
61 |
Correct |
627 ms |
93160 KB |
Output is correct |
62 |
Correct |
653 ms |
83232 KB |
Output is correct |
63 |
Correct |
688 ms |
54488 KB |
Output is correct |
64 |
Correct |
645 ms |
47684 KB |
Output is correct |
65 |
Correct |
614 ms |
43092 KB |
Output is correct |
66 |
Correct |
548 ms |
40956 KB |
Output is correct |
67 |
Correct |
631 ms |
39688 KB |
Output is correct |
68 |
Correct |
579 ms |
39260 KB |
Output is correct |
69 |
Correct |
534 ms |
38988 KB |
Output is correct |
70 |
Correct |
584 ms |
38860 KB |
Output is correct |
71 |
Correct |
566 ms |
38888 KB |
Output is correct |
72 |
Correct |
554 ms |
38648 KB |
Output is correct |
73 |
Correct |
599 ms |
38744 KB |
Output is correct |
74 |
Correct |
595 ms |
38796 KB |
Output is correct |
75 |
Correct |
590 ms |
39120 KB |
Output is correct |
76 |
Correct |
492 ms |
39116 KB |
Output is correct |
77 |
Correct |
370 ms |
39460 KB |
Output is correct |
78 |
Correct |
245 ms |
41176 KB |
Output is correct |