Submission #234675

# Submission time Handle Problem Language Result Execution time Memory
234675 2020-05-25T06:26:17 Z kshitij_sodani Race (IOI11_race) C++17
100 / 100
751 ms 63316 KB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#include "race.h"
#define a first 
#define pb push_back
#define b second
pair<int,int> aa[1000001];
pair<int,int> bb[1000001];
int ss[200001];
set<pair<int,int>> adj[200001];
int cur;
vector<int> del;
int k;
int ans=-1;
void dfs(int no,int par=-1){
	ss[no]=1;
	for(auto j:adj[no]){
		if(j.a!=par){
			dfs(j.a,no);
			ss[no]+=ss[j.a];
		}
	}
}
int dfs2(int no,int par=-1){
	for(auto j:adj[no]){
		if(j.a==par){
			continue;
		}
		if(ss[j.a]>ss[cur]/2){
			return dfs2(j.a,no);
		}
	}
	return no;
}
void dfs3(int no,int par2,int par=-1,int lev=0,int ww=0){
	if(lev==1){
		par2=no;
	}
	if(ww<=k){

		del.pb(ww);
		if(aa[ww].a==-1){
			aa[ww]={par2,lev};
		}
		else if(aa[ww].a==par2){
			aa[ww].b=min(aa[ww].b,lev);
		}
		else if(aa[ww].b>lev){
			bb[ww]=aa[ww];
			aa[ww]={par2,lev};
		}
		else if(bb[ww].a==-1){
			bb[ww]={par2,lev};
		}
		else if(bb[ww].a==par2){
			bb[ww].b=min(bb[ww].b,lev);
		}
		else if(lev<bb[ww].b){
			bb[ww]={par2,lev};
		}
		if(aa[ww].a!=-1 and aa[k-ww].a!=-1){
			if(aa[ww].a!=aa[k-ww].a){
				if(ans==-1){
					ans=aa[ww].b+aa[k-ww].b;
				}
				ans=min(ans,aa[ww].b+aa[k-ww].b);
			}
		}
		if(aa[ww].a!=-1 and bb[k-ww].a!=-1){
			if(aa[ww].a!=bb[k-ww].a){
				if(ans==-1){
					ans=aa[ww].b+bb[k-ww].b;
				}
				ans=min(ans,aa[ww].b+bb[k-ww].b);
			}
		}
		if(bb[ww].a!=-1 and aa[k-ww].a!=-1){
			if(bb[ww].a!=aa[k-ww].a){
				if(ans==-1){
					ans=bb[ww].b+aa[k-ww].b;
				}
				ans=min(ans,bb[ww].b+aa[k-ww].b);
			}
		}
		if(bb[ww].a!=-1 and bb[k-ww].a!=-1){
			if(bb[ww].a!=bb[k-ww].a){
				if(ans==-1){
					ans=bb[ww].b+bb[k-ww].b;
				}
				ans=min(ans,bb[ww].b+bb[k-ww].b);
			}
		}
	}

	for(auto j:adj[no]){
		if(j.a==par){
			continue;
		}
		dfs3(j.a,par2,no,lev+1,ww+j.b);

	}
}
void dec(int no){
	cur=no;
	dfs(no);
	//return;
	int cen=dfs2(no);

	dfs3(cen,cen);
	for(auto j:del){
		aa[j]={-1,-1};
		bb[j]={-1,-1};
	}
	del.clear();
	for(auto j:adj[cen]){
		adj[j.a].erase({cen,j.b});
	}
	for(auto j:adj[cen]){
		dec(j.a);
	}
	return;
}

int best_path(int n,int kk,int h[][2],int l[]){
	k=kk;
	for(int i=0;i<=k;i++){
		aa[i]={-1,-1};
		bb[i]={-1,-1};
	}
	for(int i=0;i<n-1;i++){
		adj[h[i][0]].insert({h[i][1],l[i]});
		adj[h[i][1]].insert({h[i][0],l[i]});
	}
	dec(0);
	return ans;
}

/*int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n,ko;
	cin>>n>>ko;
	int hh[n-1][2];
	int lol[n-1];
	for(int i=0;i<n-1;i++){
		cin>>hh[i][0]>>hh[i][1]>>lol[i];
	}
	cout<<best_path(n,ko,hh,lol)<<endl;
	return 0;
}
*/
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 10 ms 9704 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 13 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 12 ms 9728 KB Output is correct
9 Correct 11 ms 9780 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 11 ms 9832 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 10 ms 9704 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 13 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 12 ms 9728 KB Output is correct
9 Correct 11 ms 9780 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 11 ms 9832 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 11 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 20 ms 24320 KB Output is correct
23 Correct 20 ms 21632 KB Output is correct
24 Correct 21 ms 23424 KB Output is correct
25 Correct 21 ms 23040 KB Output is correct
26 Correct 14 ms 15232 KB Output is correct
27 Correct 20 ms 22400 KB Output is correct
28 Correct 13 ms 13056 KB Output is correct
29 Correct 15 ms 14976 KB Output is correct
30 Correct 14 ms 15616 KB Output is correct
31 Correct 17 ms 20096 KB Output is correct
32 Correct 17 ms 20992 KB Output is correct
33 Correct 18 ms 22144 KB Output is correct
34 Correct 16 ms 19072 KB Output is correct
35 Correct 21 ms 22656 KB Output is correct
36 Correct 23 ms 24448 KB Output is correct
37 Correct 19 ms 22364 KB Output is correct
38 Correct 16 ms 17920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 10 ms 9704 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 13 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 12 ms 9728 KB Output is correct
9 Correct 11 ms 9780 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 11 ms 9832 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 267 ms 22136 KB Output is correct
20 Correct 272 ms 22136 KB Output is correct
21 Correct 262 ms 22520 KB Output is correct
22 Correct 251 ms 22772 KB Output is correct
23 Correct 197 ms 22136 KB Output is correct
24 Correct 141 ms 22196 KB Output is correct
25 Correct 261 ms 26080 KB Output is correct
26 Correct 129 ms 28916 KB Output is correct
27 Correct 410 ms 34628 KB Output is correct
28 Correct 699 ms 47264 KB Output is correct
29 Correct 669 ms 46364 KB Output is correct
30 Correct 368 ms 34636 KB Output is correct
31 Correct 367 ms 34552 KB Output is correct
32 Correct 466 ms 34636 KB Output is correct
33 Correct 496 ms 34784 KB Output is correct
34 Correct 479 ms 35576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9728 KB Output is correct
2 Correct 10 ms 9704 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 11 ms 9728 KB Output is correct
5 Correct 13 ms 9728 KB Output is correct
6 Correct 11 ms 9856 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 12 ms 9728 KB Output is correct
9 Correct 11 ms 9780 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 10 ms 9728 KB Output is correct
12 Correct 10 ms 9728 KB Output is correct
13 Correct 10 ms 9728 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 10 ms 9728 KB Output is correct
16 Correct 10 ms 9728 KB Output is correct
17 Correct 11 ms 9832 KB Output is correct
18 Correct 11 ms 9728 KB Output is correct
19 Correct 11 ms 9728 KB Output is correct
20 Correct 10 ms 9728 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 20 ms 24320 KB Output is correct
23 Correct 20 ms 21632 KB Output is correct
24 Correct 21 ms 23424 KB Output is correct
25 Correct 21 ms 23040 KB Output is correct
26 Correct 14 ms 15232 KB Output is correct
27 Correct 20 ms 22400 KB Output is correct
28 Correct 13 ms 13056 KB Output is correct
29 Correct 15 ms 14976 KB Output is correct
30 Correct 14 ms 15616 KB Output is correct
31 Correct 17 ms 20096 KB Output is correct
32 Correct 17 ms 20992 KB Output is correct
33 Correct 18 ms 22144 KB Output is correct
34 Correct 16 ms 19072 KB Output is correct
35 Correct 21 ms 22656 KB Output is correct
36 Correct 23 ms 24448 KB Output is correct
37 Correct 19 ms 22364 KB Output is correct
38 Correct 16 ms 17920 KB Output is correct
39 Correct 267 ms 22136 KB Output is correct
40 Correct 272 ms 22136 KB Output is correct
41 Correct 262 ms 22520 KB Output is correct
42 Correct 251 ms 22772 KB Output is correct
43 Correct 197 ms 22136 KB Output is correct
44 Correct 141 ms 22196 KB Output is correct
45 Correct 261 ms 26080 KB Output is correct
46 Correct 129 ms 28916 KB Output is correct
47 Correct 410 ms 34628 KB Output is correct
48 Correct 699 ms 47264 KB Output is correct
49 Correct 669 ms 46364 KB Output is correct
50 Correct 368 ms 34636 KB Output is correct
51 Correct 367 ms 34552 KB Output is correct
52 Correct 466 ms 34636 KB Output is correct
53 Correct 496 ms 34784 KB Output is correct
54 Correct 479 ms 35576 KB Output is correct
55 Correct 24 ms 11136 KB Output is correct
56 Correct 21 ms 11136 KB Output is correct
57 Correct 139 ms 22772 KB Output is correct
58 Correct 102 ms 22388 KB Output is correct
59 Correct 136 ms 31988 KB Output is correct
60 Correct 701 ms 63316 KB Output is correct
61 Correct 358 ms 35064 KB Output is correct
62 Correct 368 ms 51380 KB Output is correct
63 Correct 480 ms 51360 KB Output is correct
64 Correct 722 ms 44372 KB Output is correct
65 Correct 547 ms 35548 KB Output is correct
66 Correct 751 ms 60108 KB Output is correct
67 Correct 430 ms 51880 KB Output is correct
68 Correct 527 ms 51068 KB Output is correct
69 Correct 542 ms 51224 KB Output is correct
70 Correct 516 ms 49912 KB Output is correct