Submission #376946

# Submission time Handle Problem Language Result Execution time Memory
376946 2021-03-12T09:07:36 Z kshitij_sodani Hard route (IZhO17_road) C++14
100 / 100
1035 ms 106476 KB
//#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,1}){

	ma3[no]=ma2;
	//cout<<no<<","<<ma2.a<<","<<ma2.b<<endl;
	vector<pair<llo,llo>> xx;
	xx.pb({ma2.a+1,ma2.b});
	for(auto j:adj[no]){
		if(j!=par2){
			xx.pb({ma[j].a+2,ma[j].b});
		}	
	}
	if(par2!=-1){
		if(adj[no].size()==1){
			return;
		}
	}
	sort(xx.begin(),xx.end());
	reverse(xx.begin(),xx.end());
	pair<llo,llo> rr=xx[0];

	for(llo i=1;i<xx.size();i++){
		rr=remax(rr,xx[i]);
	}
	pair<llo,llo> rr2;
	if(xx[0].a!=xx[1].a){
		rr2.a=xx[1].a;
		rr2.b=0;
		for(llo i=1;i<xx.size();i++){
			if(rr2.a==xx[i].a){
				rr2.b+=xx[i].b;
			}
		}
	}

	for(auto j:adj[no]){
		if(j!=par2){
			if(ma[j].a+2==xx[0].a){
				if(ma[j].b==rr.b){
					dfs2(j,no,rr2);
				}
				else{
					dfs2(j,no,{rr.a,rr.b-ma[j].b});
				}
			}
			else{
				dfs2(j,no,rr);
			}
		}
	}

}
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};
	dfs(0);
	dfs2(0);
	for(llo i=0;i<n;i++){
	//	dfs(i);
		vector<pair<llo,llo>> tt;
		for(auto j:adj[i]){
			if(j!=par[i]){
				tt.pb({ma[j].a+1,ma[j].b});
			}
			else{
				tt.pb(ma3[i]);
			}
		}	
		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 'void dfs2(llo, llo, std::pair<long long int, long long int>)':
road.cpp:56:15: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |  for(llo i=1;i<xx.size();i++){
      |              ~^~~~~~~~~~
road.cpp:63:16: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |   for(llo i=1;i<xx.size();i++){
      |               ~^~~~~~~~~~
road.cpp: In function 'int main()':
road.cpp:163:9: warning: unused variable 'cur' [-Wunused-variable]
  163 |     llo cur=0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12140 KB Output is correct
2 Correct 10 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 12140 KB Output is correct
7 Correct 8 ms 12140 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 10 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 9 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 10 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12140 KB Output is correct
2 Correct 10 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 12140 KB Output is correct
7 Correct 8 ms 12140 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 10 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 9 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 10 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
25 Correct 12 ms 12780 KB Output is correct
26 Correct 12 ms 12908 KB Output is correct
27 Correct 13 ms 12908 KB Output is correct
28 Correct 12 ms 12908 KB Output is correct
29 Correct 12 ms 12952 KB Output is correct
30 Correct 12 ms 13036 KB Output is correct
31 Correct 12 ms 13036 KB Output is correct
32 Correct 12 ms 12908 KB Output is correct
33 Correct 12 ms 12908 KB Output is correct
34 Correct 12 ms 12908 KB Output is correct
35 Correct 13 ms 12908 KB Output is correct
36 Correct 12 ms 12908 KB Output is correct
37 Correct 11 ms 12268 KB Output is correct
38 Correct 10 ms 12268 KB Output is correct
39 Correct 12 ms 12780 KB Output is correct
40 Correct 12 ms 12652 KB Output is correct
41 Correct 12 ms 12524 KB Output is correct
42 Correct 12 ms 12524 KB Output is correct
43 Correct 14 ms 12524 KB Output is correct
44 Correct 12 ms 12524 KB Output is correct
45 Correct 12 ms 12524 KB Output is correct
46 Correct 11 ms 12396 KB Output is correct
47 Correct 11 ms 12652 KB Output is correct
48 Correct 11 ms 12780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12140 KB Output is correct
2 Correct 10 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 12140 KB Output is correct
7 Correct 8 ms 12140 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 10 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 9 ms 12140 KB Output is correct
19 Correct 8 ms 12140 KB Output is correct
20 Correct 9 ms 12140 KB Output is correct
21 Correct 10 ms 12140 KB Output is correct
22 Correct 9 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
25 Correct 12 ms 12780 KB Output is correct
26 Correct 12 ms 12908 KB Output is correct
27 Correct 13 ms 12908 KB Output is correct
28 Correct 12 ms 12908 KB Output is correct
29 Correct 12 ms 12952 KB Output is correct
30 Correct 12 ms 13036 KB Output is correct
31 Correct 12 ms 13036 KB Output is correct
32 Correct 12 ms 12908 KB Output is correct
33 Correct 12 ms 12908 KB Output is correct
34 Correct 12 ms 12908 KB Output is correct
35 Correct 13 ms 12908 KB Output is correct
36 Correct 12 ms 12908 KB Output is correct
37 Correct 11 ms 12268 KB Output is correct
38 Correct 10 ms 12268 KB Output is correct
39 Correct 12 ms 12780 KB Output is correct
40 Correct 12 ms 12652 KB Output is correct
41 Correct 12 ms 12524 KB Output is correct
42 Correct 12 ms 12524 KB Output is correct
43 Correct 14 ms 12524 KB Output is correct
44 Correct 12 ms 12524 KB Output is correct
45 Correct 12 ms 12524 KB Output is correct
46 Correct 11 ms 12396 KB Output is correct
47 Correct 11 ms 12652 KB Output is correct
48 Correct 11 ms 12780 KB Output is correct
49 Correct 832 ms 82028 KB Output is correct
50 Correct 829 ms 93848 KB Output is correct
51 Correct 845 ms 99052 KB Output is correct
52 Correct 834 ms 80864 KB Output is correct
53 Correct 731 ms 99836 KB Output is correct
54 Correct 751 ms 106476 KB Output is correct
55 Correct 743 ms 90124 KB Output is correct
56 Correct 731 ms 97004 KB Output is correct
57 Correct 759 ms 101584 KB Output is correct
58 Correct 751 ms 94792 KB Output is correct
59 Correct 769 ms 94572 KB Output is correct
60 Correct 777 ms 91584 KB Output is correct
61 Correct 388 ms 35052 KB Output is correct
62 Correct 390 ms 34892 KB Output is correct
63 Correct 1035 ms 79176 KB Output is correct
64 Correct 1028 ms 68484 KB Output is correct
65 Correct 1012 ms 61420 KB Output is correct
66 Correct 1012 ms 57836 KB Output is correct
67 Correct 978 ms 55788 KB Output is correct
68 Correct 976 ms 55020 KB Output is correct
69 Correct 988 ms 54892 KB Output is correct
70 Correct 968 ms 54640 KB Output is correct
71 Correct 939 ms 54508 KB Output is correct
72 Correct 1010 ms 54632 KB Output is correct
73 Correct 945 ms 54940 KB Output is correct
74 Correct 944 ms 55524 KB Output is correct
75 Correct 940 ms 56780 KB Output is correct
76 Correct 912 ms 58816 KB Output is correct
77 Correct 770 ms 64276 KB Output is correct
78 Correct 587 ms 74672 KB Output is correct