//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo n;
vector<llo> adj[500001];
pair<llo,llo> ma[5000001];
pair<llo,llo> ma3[5000001];
llo par[5000001];
pair<llo,llo> remax(pair<llo,llo> aa,pair<llo,llo> bb){
if(bb.a>aa.a){
return bb;
}
if(aa.a==bb.a){
aa.b+=bb.b;
return aa;
}
return aa;
}
void dfs(llo no,llo par2=-1){
par[no]=par2;
ma[no]={0,1};
for(auto j:adj[no]){
if(j!=par2){
dfs(j,no);
ma[no]=remax(ma[no],{ma[j].a+1,ma[j].b});
}
}
}
/*void dfs2(llo no,llo par2=-1,pair<llo,llo> ma2={0,0}){
ma3[no]=ma2;
for(auto j:adj[no]){
}
}*/
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(llo i=0;i<n-1;i++){
llo aa,bb;
cin>>aa>>bb;
aa--;
bb--;
adj[aa].pb(bb);
adj[bb].pb(aa);
}
llo st=1;
for(llo i=0;i<n;i++){
if(adj[i].size()>2){
st=0;
}
}
if(st){
cout<<0<<" "<<1<<endl;
return 0;
}
pair<llo,llo> cot={0,0};
for(llo i=0;i<n;i++){
dfs(i);
vector<pair<llo,llo>> tt;
for(auto j:adj[i]){
tt.pb({ma[j].a+1,ma[j].b});
}
sort(tt.begin(),tt.end());
reverse(tt.begin(),tt.end());
if(tt.size()>2){
if(tt[0].a==tt[1].a and tt[1].a==tt[2].a){
llo cur=0;
for(auto j:tt){
if(j.a==tt[0].a){
cot=remax(cot,{(2*tt[0].a)*tt[0].a,cur*j.b});
cur+=j.b;
}
}
continue;
}
/*for(auto j:tt){
cout<<j.a<<","<<j.b<<endl;
}
cout<<endl;*/
if(tt[0].a==tt[1].a){
llo su=tt[0].a+tt[1].a;
llo su2=0;
for(auto j:tt){
if(j.a==tt[2].a){
su2+=j.b;
}
}
cot=remax(cot,{tt[0].a*(tt[0].a+tt[2].a),su*su2});
continue;
}
if(tt[1].a==tt[2].a){
llo cur=0;
for(auto j:tt){
if(j.a==tt[1].a){
cot=remax(cot,{(tt[1].a*2)*tt[0].a,cur*j.b});
cur+=j.b;
}
}
}
else{
llo cur=0;
for(auto j:tt){
if(j.a==tt[2].a){
cot=remax(cot,{(tt[1].a+tt[2].a)*tt[0].a,j.b*tt[1].b});
//cur+=j.b;
}
}
}
}
}
cout<<cot.a<<" "<<cot.b<<endl;
//dfs(0);
return 0;
}
Compilation message
road.cpp: In function 'int main()':
road.cpp:116:9: warning: unused variable 'cur' [-Wunused-variable]
116 | llo cur=0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12288 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
9 ms |
12140 KB |
Output is correct |
12 |
Correct |
9 ms |
12140 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12140 KB |
Output is correct |
16 |
Correct |
9 ms |
12140 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
10 ms |
12140 KB |
Output is correct |
19 |
Correct |
9 ms |
12140 KB |
Output is correct |
20 |
Correct |
9 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
9 ms |
12032 KB |
Output is correct |
23 |
Correct |
9 ms |
12140 KB |
Output is correct |
24 |
Correct |
9 ms |
12140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12288 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
9 ms |
12140 KB |
Output is correct |
12 |
Correct |
9 ms |
12140 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12140 KB |
Output is correct |
16 |
Correct |
9 ms |
12140 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
10 ms |
12140 KB |
Output is correct |
19 |
Correct |
9 ms |
12140 KB |
Output is correct |
20 |
Correct |
9 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
9 ms |
12032 KB |
Output is correct |
23 |
Correct |
9 ms |
12140 KB |
Output is correct |
24 |
Correct |
9 ms |
12140 KB |
Output is correct |
25 |
Correct |
469 ms |
12652 KB |
Output is correct |
26 |
Correct |
518 ms |
12652 KB |
Output is correct |
27 |
Correct |
467 ms |
12780 KB |
Output is correct |
28 |
Correct |
484 ms |
12780 KB |
Output is correct |
29 |
Correct |
389 ms |
12652 KB |
Output is correct |
30 |
Correct |
356 ms |
12652 KB |
Output is correct |
31 |
Correct |
350 ms |
12780 KB |
Output is correct |
32 |
Correct |
355 ms |
12652 KB |
Output is correct |
33 |
Correct |
413 ms |
12652 KB |
Output is correct |
34 |
Correct |
436 ms |
12852 KB |
Output is correct |
35 |
Correct |
414 ms |
12780 KB |
Output is correct |
36 |
Correct |
428 ms |
12796 KB |
Output is correct |
37 |
Correct |
10 ms |
12268 KB |
Output is correct |
38 |
Correct |
12 ms |
12268 KB |
Output is correct |
39 |
Correct |
831 ms |
12628 KB |
Output is correct |
40 |
Correct |
763 ms |
12548 KB |
Output is correct |
41 |
Correct |
733 ms |
12524 KB |
Output is correct |
42 |
Correct |
720 ms |
12556 KB |
Output is correct |
43 |
Correct |
660 ms |
12652 KB |
Output is correct |
44 |
Correct |
659 ms |
12396 KB |
Output is correct |
45 |
Correct |
477 ms |
12396 KB |
Output is correct |
46 |
Correct |
386 ms |
12452 KB |
Output is correct |
47 |
Correct |
364 ms |
12524 KB |
Output is correct |
48 |
Correct |
294 ms |
12652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12140 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
9 ms |
12140 KB |
Output is correct |
4 |
Correct |
9 ms |
12140 KB |
Output is correct |
5 |
Correct |
9 ms |
12140 KB |
Output is correct |
6 |
Correct |
9 ms |
12288 KB |
Output is correct |
7 |
Correct |
9 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12140 KB |
Output is correct |
9 |
Correct |
9 ms |
12140 KB |
Output is correct |
10 |
Correct |
9 ms |
12140 KB |
Output is correct |
11 |
Correct |
9 ms |
12140 KB |
Output is correct |
12 |
Correct |
9 ms |
12140 KB |
Output is correct |
13 |
Correct |
9 ms |
12140 KB |
Output is correct |
14 |
Correct |
9 ms |
12140 KB |
Output is correct |
15 |
Correct |
9 ms |
12140 KB |
Output is correct |
16 |
Correct |
9 ms |
12140 KB |
Output is correct |
17 |
Correct |
9 ms |
12140 KB |
Output is correct |
18 |
Correct |
10 ms |
12140 KB |
Output is correct |
19 |
Correct |
9 ms |
12140 KB |
Output is correct |
20 |
Correct |
9 ms |
12140 KB |
Output is correct |
21 |
Correct |
9 ms |
12140 KB |
Output is correct |
22 |
Correct |
9 ms |
12032 KB |
Output is correct |
23 |
Correct |
9 ms |
12140 KB |
Output is correct |
24 |
Correct |
9 ms |
12140 KB |
Output is correct |
25 |
Correct |
469 ms |
12652 KB |
Output is correct |
26 |
Correct |
518 ms |
12652 KB |
Output is correct |
27 |
Correct |
467 ms |
12780 KB |
Output is correct |
28 |
Correct |
484 ms |
12780 KB |
Output is correct |
29 |
Correct |
389 ms |
12652 KB |
Output is correct |
30 |
Correct |
356 ms |
12652 KB |
Output is correct |
31 |
Correct |
350 ms |
12780 KB |
Output is correct |
32 |
Correct |
355 ms |
12652 KB |
Output is correct |
33 |
Correct |
413 ms |
12652 KB |
Output is correct |
34 |
Correct |
436 ms |
12852 KB |
Output is correct |
35 |
Correct |
414 ms |
12780 KB |
Output is correct |
36 |
Correct |
428 ms |
12796 KB |
Output is correct |
37 |
Correct |
10 ms |
12268 KB |
Output is correct |
38 |
Correct |
12 ms |
12268 KB |
Output is correct |
39 |
Correct |
831 ms |
12628 KB |
Output is correct |
40 |
Correct |
763 ms |
12548 KB |
Output is correct |
41 |
Correct |
733 ms |
12524 KB |
Output is correct |
42 |
Correct |
720 ms |
12556 KB |
Output is correct |
43 |
Correct |
660 ms |
12652 KB |
Output is correct |
44 |
Correct |
659 ms |
12396 KB |
Output is correct |
45 |
Correct |
477 ms |
12396 KB |
Output is correct |
46 |
Correct |
386 ms |
12452 KB |
Output is correct |
47 |
Correct |
364 ms |
12524 KB |
Output is correct |
48 |
Correct |
294 ms |
12652 KB |
Output is correct |
49 |
Execution timed out |
2077 ms |
67740 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |