#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
const int maxn=5e5+7;
vector<int>adj[maxn];
int max_depth[maxn];
int max_count[maxn];
int max_hardness=0;
int paths_counts=1;
int n;
void dfs(int u,int par)
{
for(int v:adj[u])if(v!=par){
dfs(v,u);
if(max_depth[u]<max_depth[v]+1){
max_depth[u]=max_depth[v]+1;
max_count[u]=max_count[v];
}
else if(max_depth[u]==max_depth[v]+1){
max_count[u]+=max_count[v];
}
}
}
void dfs2(int u,int par,int parent_dist,int parent_count)
{
// cout<<u<<" "<<max_hardness<<" "<<paths_counts<<" ";
vector<pair<int,int>>tmp;
if(u>1||adj[u].size()==1){
tmp.push_back(make_pair(parent_dist,parent_count));
}
for(int v:adj[u])if(v!=par){
tmp.push_back({max_depth[v]+1,max_count[v]});
}
sort(tmp.begin(),tmp.end(),greater<ii>());
if(adj[u].size()>=3){
int pre=0;
int curr=0,tinh=0;
int a=tmp[0].fi;
int b=tmp[1].fi;
int c=tmp[2].fi;
for(ii p:tmp)
{
int len=p.fi;
int num=p.se;
if(len==c)pre+=num;
}
curr=1ll*a*(b+c);
if(a!=b&&b!=c)
{
tinh=tmp[1].se*pre;
} else if(a==b&&b==c)
{
tinh=pre*pre;
for(ii p:tmp) {
int len=p.fi;
int num=p.se;
if(len==a)tinh-=num*num;
}
tinh=tinh/2;
} else if(a==b)
{
tinh=(tmp[0].se+tmp[1].se)*pre;
} else
{
tinh=pre*pre;
for(ii p:tmp) {
int len=p.fi;
int num=p.se;
if(len==b)tinh-=num*num;
}
tinh=tinh/2;
}
if(curr>max_hardness)
{
max_hardness=curr;
paths_counts=tinh;
} else if(curr==max_hardness)paths_counts+=tinh;
}
int longest1=0;
int count1=0;
int longest2=0;
int count2=0;
for(ii p:tmp){
int v=p.fi;
int w=p.se;
if(longest1<v+1){
longest2=longest1;
count2=count1;
longest1=v+1;
count1=w;
}
else if(longest1==v+1){
count1+=w;
}
else if(longest2<v+1){
longest2=v+1;
count2=w;
}
else if(longest2==v+1){
count2+=w;
}
}
// cout<<max_hardness<<" "<<paths_counts<<"\n";
for(int v:adj[u])if(v!=par){
if(max_depth[v]+2==longest1){
if(max_count[v]==count1){
dfs2(v,u,longest2,count2);
}
else{
dfs2(v,u,longest1,count1-max_count[v]);
}
}
else dfs2(v,u,longest1,count1);
}
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i=1;i<=n;i++){
max_depth[i]=0;
max_count[i]=1;
}
dfs(1,0);
// for(int i=1;i<=n;i++){
// cout<<max_depth[i]<<" "<<max_count[i]<<"\n";
// }
dfs2(1,0,0,1);
cout<<max_hardness<<" "<<paths_counts;
return 0;
}
Compilation message
road.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
124 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15548 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
2 ms |
15708 KB |
Output is correct |
6 |
Correct |
2 ms |
15708 KB |
Output is correct |
7 |
Correct |
2 ms |
15708 KB |
Output is correct |
8 |
Correct |
3 ms |
15708 KB |
Output is correct |
9 |
Correct |
3 ms |
15704 KB |
Output is correct |
10 |
Correct |
2 ms |
15708 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
3 ms |
15708 KB |
Output is correct |
13 |
Correct |
2 ms |
15708 KB |
Output is correct |
14 |
Correct |
2 ms |
15708 KB |
Output is correct |
15 |
Correct |
3 ms |
15708 KB |
Output is correct |
16 |
Correct |
2 ms |
15708 KB |
Output is correct |
17 |
Correct |
2 ms |
15708 KB |
Output is correct |
18 |
Correct |
2 ms |
15708 KB |
Output is correct |
19 |
Correct |
2 ms |
15708 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
3 ms |
15704 KB |
Output is correct |
22 |
Correct |
3 ms |
15708 KB |
Output is correct |
23 |
Correct |
2 ms |
15708 KB |
Output is correct |
24 |
Correct |
3 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15548 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
2 ms |
15708 KB |
Output is correct |
6 |
Correct |
2 ms |
15708 KB |
Output is correct |
7 |
Correct |
2 ms |
15708 KB |
Output is correct |
8 |
Correct |
3 ms |
15708 KB |
Output is correct |
9 |
Correct |
3 ms |
15704 KB |
Output is correct |
10 |
Correct |
2 ms |
15708 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
3 ms |
15708 KB |
Output is correct |
13 |
Correct |
2 ms |
15708 KB |
Output is correct |
14 |
Correct |
2 ms |
15708 KB |
Output is correct |
15 |
Correct |
3 ms |
15708 KB |
Output is correct |
16 |
Correct |
2 ms |
15708 KB |
Output is correct |
17 |
Correct |
2 ms |
15708 KB |
Output is correct |
18 |
Correct |
2 ms |
15708 KB |
Output is correct |
19 |
Correct |
2 ms |
15708 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
3 ms |
15704 KB |
Output is correct |
22 |
Correct |
3 ms |
15708 KB |
Output is correct |
23 |
Correct |
2 ms |
15708 KB |
Output is correct |
24 |
Correct |
3 ms |
15708 KB |
Output is correct |
25 |
Correct |
4 ms |
16220 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16476 KB |
Output is correct |
28 |
Correct |
4 ms |
16476 KB |
Output is correct |
29 |
Correct |
4 ms |
16372 KB |
Output is correct |
30 |
Correct |
5 ms |
16732 KB |
Output is correct |
31 |
Correct |
4 ms |
16476 KB |
Output is correct |
32 |
Correct |
3 ms |
16220 KB |
Output is correct |
33 |
Correct |
4 ms |
16392 KB |
Output is correct |
34 |
Correct |
4 ms |
16476 KB |
Output is correct |
35 |
Correct |
4 ms |
16360 KB |
Output is correct |
36 |
Correct |
4 ms |
16220 KB |
Output is correct |
37 |
Correct |
4 ms |
16480 KB |
Output is correct |
38 |
Correct |
5 ms |
16728 KB |
Output is correct |
39 |
Correct |
4 ms |
16220 KB |
Output is correct |
40 |
Correct |
4 ms |
15960 KB |
Output is correct |
41 |
Correct |
4 ms |
15964 KB |
Output is correct |
42 |
Correct |
4 ms |
16016 KB |
Output is correct |
43 |
Correct |
4 ms |
15964 KB |
Output is correct |
44 |
Correct |
4 ms |
15964 KB |
Output is correct |
45 |
Correct |
4 ms |
15876 KB |
Output is correct |
46 |
Correct |
5 ms |
15932 KB |
Output is correct |
47 |
Correct |
4 ms |
16212 KB |
Output is correct |
48 |
Correct |
4 ms |
16220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15708 KB |
Output is correct |
2 |
Correct |
2 ms |
15548 KB |
Output is correct |
3 |
Correct |
2 ms |
15708 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
2 ms |
15708 KB |
Output is correct |
6 |
Correct |
2 ms |
15708 KB |
Output is correct |
7 |
Correct |
2 ms |
15708 KB |
Output is correct |
8 |
Correct |
3 ms |
15708 KB |
Output is correct |
9 |
Correct |
3 ms |
15704 KB |
Output is correct |
10 |
Correct |
2 ms |
15708 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
3 ms |
15708 KB |
Output is correct |
13 |
Correct |
2 ms |
15708 KB |
Output is correct |
14 |
Correct |
2 ms |
15708 KB |
Output is correct |
15 |
Correct |
3 ms |
15708 KB |
Output is correct |
16 |
Correct |
2 ms |
15708 KB |
Output is correct |
17 |
Correct |
2 ms |
15708 KB |
Output is correct |
18 |
Correct |
2 ms |
15708 KB |
Output is correct |
19 |
Correct |
2 ms |
15708 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
3 ms |
15704 KB |
Output is correct |
22 |
Correct |
3 ms |
15708 KB |
Output is correct |
23 |
Correct |
2 ms |
15708 KB |
Output is correct |
24 |
Correct |
3 ms |
15708 KB |
Output is correct |
25 |
Correct |
4 ms |
16220 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16476 KB |
Output is correct |
28 |
Correct |
4 ms |
16476 KB |
Output is correct |
29 |
Correct |
4 ms |
16372 KB |
Output is correct |
30 |
Correct |
5 ms |
16732 KB |
Output is correct |
31 |
Correct |
4 ms |
16476 KB |
Output is correct |
32 |
Correct |
3 ms |
16220 KB |
Output is correct |
33 |
Correct |
4 ms |
16392 KB |
Output is correct |
34 |
Correct |
4 ms |
16476 KB |
Output is correct |
35 |
Correct |
4 ms |
16360 KB |
Output is correct |
36 |
Correct |
4 ms |
16220 KB |
Output is correct |
37 |
Correct |
4 ms |
16480 KB |
Output is correct |
38 |
Correct |
5 ms |
16728 KB |
Output is correct |
39 |
Correct |
4 ms |
16220 KB |
Output is correct |
40 |
Correct |
4 ms |
15960 KB |
Output is correct |
41 |
Correct |
4 ms |
15964 KB |
Output is correct |
42 |
Correct |
4 ms |
16016 KB |
Output is correct |
43 |
Correct |
4 ms |
15964 KB |
Output is correct |
44 |
Correct |
4 ms |
15964 KB |
Output is correct |
45 |
Correct |
4 ms |
15876 KB |
Output is correct |
46 |
Correct |
5 ms |
15932 KB |
Output is correct |
47 |
Correct |
4 ms |
16212 KB |
Output is correct |
48 |
Correct |
4 ms |
16220 KB |
Output is correct |
49 |
Correct |
253 ms |
78868 KB |
Output is correct |
50 |
Correct |
244 ms |
85552 KB |
Output is correct |
51 |
Correct |
260 ms |
91216 KB |
Output is correct |
52 |
Correct |
234 ms |
71300 KB |
Output is correct |
53 |
Correct |
237 ms |
90964 KB |
Output is correct |
54 |
Correct |
218 ms |
98132 KB |
Output is correct |
55 |
Correct |
205 ms |
80472 KB |
Output is correct |
56 |
Correct |
234 ms |
88236 KB |
Output is correct |
57 |
Correct |
229 ms |
93936 KB |
Output is correct |
58 |
Correct |
236 ms |
86608 KB |
Output is correct |
59 |
Correct |
259 ms |
86356 KB |
Output is correct |
60 |
Correct |
242 ms |
82948 KB |
Output is correct |
61 |
Correct |
411 ms |
136256 KB |
Output is correct |
62 |
Correct |
408 ms |
119000 KB |
Output is correct |
63 |
Correct |
414 ms |
69456 KB |
Output is correct |
64 |
Correct |
352 ms |
57860 KB |
Output is correct |
65 |
Correct |
341 ms |
50256 KB |
Output is correct |
66 |
Correct |
286 ms |
46160 KB |
Output is correct |
67 |
Correct |
307 ms |
44208 KB |
Output is correct |
68 |
Correct |
292 ms |
43108 KB |
Output is correct |
69 |
Correct |
286 ms |
43092 KB |
Output is correct |
70 |
Correct |
270 ms |
42788 KB |
Output is correct |
71 |
Correct |
294 ms |
42644 KB |
Output is correct |
72 |
Correct |
295 ms |
42836 KB |
Output is correct |
73 |
Correct |
294 ms |
43152 KB |
Output is correct |
74 |
Correct |
280 ms |
43480 KB |
Output is correct |
75 |
Correct |
276 ms |
44112 KB |
Output is correct |
76 |
Correct |
246 ms |
45640 KB |
Output is correct |
77 |
Correct |
247 ms |
49588 KB |
Output is correct |
78 |
Correct |
151 ms |
55352 KB |
Output is correct |