Submission #576257

# Submission time Handle Problem Language Result Execution time Memory
576257 2022-06-12T19:05:20 Z itnes Race (IOI11_race) C++14
100 / 100
730 ms 50320 KB
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef pair<int,int> pii;
const ll INFLL=1e18+7;
const int INF=1e9+7;
#define pb push_back
const int MAXN=2e5+7,MAXM=1e6+7;
int n,k;
list<pii> G[MAXN];
pair<list<pii>::iterator,list<pii>::iterator> edges[MAXN];
int cost[MAXN];
int res=INF;
int amount[MAXN],depth[MAXN];
void set_dp(int x,int pre){
	depth[x]=depth[pre]+1;
	for(pii v:G[x]) if(v.first!=pre) set_dp(v.first,x);
	amount[x]=1;
	for(pii v:G[x]) if(v.first!=pre) amount[x]+=amount[v.first];
}
int centroid(int x,int pre,int N){
	int maxi=0,pos=0;
	for(pii v:G[x]){
		if(v.first!=pre){
			if(amount[v.first]>maxi){
				maxi=amount[v.first];
				pos=v.first;
			}
		}
	}
	if(maxi<=N/2) return x;
	return centroid(pos,x,N);
}
vector<int> to_reverse,to_temp;
int dist[MAXN];
int best[MAXM];
int temp[MAXM];
void prepare(int x,int pre){
	if(dist[x]>k) return;
	depth[x]=depth[pre]+1;
	to_reverse.pb(dist[x]);
	to_temp.pb(dist[x]);
	temp[dist[x]]=min(temp[dist[x]],depth[x]);
	res=min(res,temp[dist[x]]+best[k-dist[x]]);
	//~ cout<<dist[x]<<" & "<<k-dist[x]<<" | ";
	//~ cout<<temp[dist[x]]<<" + "<<best[k-dist[x]]<<"\n";
	for(pii v:G[x]){
		if(v.first!=pre){
			dist[v.first]=dist[x]+cost[v.second];
			if(dist[v.first]>k) continue;
			prepare(v.first,x);
		}
	}
}
void clean(int x,int pre){
	dist[x]=depth[x]=0;
	for(pii v:G[x]) if(v.first!=pre) clean(v.first,x);
}
void decompose(int x,int pre){
	set_dp(x,pre);
	int vert=centroid(x,x,amount[x]);
	//actual calculations
	depth[vert]=0;
	for(pii v:G[vert]){
		dist[v.first]=cost[v.second];
		prepare(v.first,vert);
		for(int w:to_temp) best[w]=min(best[w],temp[w]);
		for(int w:to_temp) temp[w]=INF;
		to_temp.clear();
	}
	for(int v:to_reverse) best[v]=INF;
	to_reverse.clear();
	clean(vert,vert);
	vector<pii> neigs;
	for(pii v:G[vert]) neigs.pb(v);
	for(pii v:neigs){
		if(x>v.first) swap(edges[v.second].first,edges[v.second].second);
		G[x].erase(edges[v.second].first);
		G[v.first].erase(edges[v.second].second); 
	}
	for(pii v:neigs) decompose(v.first,vert);
}
int best_path(int N,int K,int H[][2],int* L){
	n=N; k=K;
	for(int i=0;i<n-1;++i){
		if(H[i][0]>H[i][1]) swap(H[i][0],H[i][1]);
		G[H[i][0]].push_front({H[i][1],i});
		G[H[i][1]].push_front({H[i][0],i});
		edges[i]={G[H[i][0]].begin(),G[H[i][1]].begin()};
	}
	for(int i=0;i<n-1;++i) cost[i]=L[i];
	for(int i=1;i<MAXM;++i) best[i]=temp[i]=INF;
	decompose(0,0);
	return (res==INF?-1:res);
}
//~ int main()
//~ {
	//~ ios_base::sync_with_stdio(0);
	//~ int arr[10][2]={{0,1},{0,2},{2,3},{3,4},{4,5},{0,6},{6,7},{6,8},{8,9},{8,10}};
	//~ int arr2[10]={3,4,5,4,6,3,2,5,6,7};
	//~ cout<<best_path(11,12,arr,arr2)<<"\n";
//~ }
# Verdict Execution time Memory Grader output
1 Correct 10 ms 15956 KB Output is correct
2 Correct 10 ms 15956 KB Output is correct
3 Correct 9 ms 15912 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Correct 10 ms 15956 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 9 ms 15944 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 10 ms 15956 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 8 ms 15948 KB Output is correct
15 Correct 9 ms 15952 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 8 ms 15956 KB Output is correct
18 Correct 8 ms 15952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 15956 KB Output is correct
2 Correct 10 ms 15956 KB Output is correct
3 Correct 9 ms 15912 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Correct 10 ms 15956 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 9 ms 15944 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 10 ms 15956 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 8 ms 15948 KB Output is correct
15 Correct 9 ms 15952 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 8 ms 15956 KB Output is correct
18 Correct 8 ms 15952 KB Output is correct
19 Correct 10 ms 15956 KB Output is correct
20 Correct 12 ms 15916 KB Output is correct
21 Correct 12 ms 16068 KB Output is correct
22 Correct 10 ms 16028 KB Output is correct
23 Correct 9 ms 16084 KB Output is correct
24 Correct 9 ms 16044 KB Output is correct
25 Correct 9 ms 16092 KB Output is correct
26 Correct 10 ms 16092 KB Output is correct
27 Correct 9 ms 16084 KB Output is correct
28 Correct 12 ms 16096 KB Output is correct
29 Correct 10 ms 15992 KB Output is correct
30 Correct 9 ms 16084 KB Output is correct
31 Correct 9 ms 16084 KB Output is correct
32 Correct 10 ms 16096 KB Output is correct
33 Correct 10 ms 16092 KB Output is correct
34 Correct 9 ms 16084 KB Output is correct
35 Correct 9 ms 16088 KB Output is correct
36 Correct 9 ms 16084 KB Output is correct
37 Correct 12 ms 16088 KB Output is correct
38 Correct 9 ms 16104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 15956 KB Output is correct
2 Correct 10 ms 15956 KB Output is correct
3 Correct 9 ms 15912 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Correct 10 ms 15956 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 9 ms 15944 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 10 ms 15956 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 8 ms 15948 KB Output is correct
15 Correct 9 ms 15952 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 8 ms 15956 KB Output is correct
18 Correct 8 ms 15952 KB Output is correct
19 Correct 208 ms 26468 KB Output is correct
20 Correct 225 ms 26316 KB Output is correct
21 Correct 212 ms 26708 KB Output is correct
22 Correct 262 ms 27176 KB Output is correct
23 Correct 104 ms 26436 KB Output is correct
24 Correct 88 ms 26444 KB Output is correct
25 Correct 180 ms 29652 KB Output is correct
26 Correct 122 ms 33396 KB Output is correct
27 Correct 265 ms 36864 KB Output is correct
28 Correct 426 ms 49612 KB Output is correct
29 Correct 434 ms 48608 KB Output is correct
30 Correct 260 ms 36864 KB Output is correct
31 Correct 270 ms 36900 KB Output is correct
32 Correct 368 ms 36812 KB Output is correct
33 Correct 319 ms 36972 KB Output is correct
34 Correct 327 ms 37704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 15956 KB Output is correct
2 Correct 10 ms 15956 KB Output is correct
3 Correct 9 ms 15912 KB Output is correct
4 Correct 9 ms 15956 KB Output is correct
5 Correct 10 ms 15956 KB Output is correct
6 Correct 8 ms 15956 KB Output is correct
7 Correct 9 ms 15944 KB Output is correct
8 Correct 8 ms 15956 KB Output is correct
9 Correct 8 ms 15956 KB Output is correct
10 Correct 8 ms 15956 KB Output is correct
11 Correct 8 ms 15956 KB Output is correct
12 Correct 10 ms 15956 KB Output is correct
13 Correct 8 ms 15956 KB Output is correct
14 Correct 8 ms 15948 KB Output is correct
15 Correct 9 ms 15952 KB Output is correct
16 Correct 8 ms 15956 KB Output is correct
17 Correct 8 ms 15956 KB Output is correct
18 Correct 8 ms 15952 KB Output is correct
19 Correct 10 ms 15956 KB Output is correct
20 Correct 12 ms 15916 KB Output is correct
21 Correct 12 ms 16068 KB Output is correct
22 Correct 10 ms 16028 KB Output is correct
23 Correct 9 ms 16084 KB Output is correct
24 Correct 9 ms 16044 KB Output is correct
25 Correct 9 ms 16092 KB Output is correct
26 Correct 10 ms 16092 KB Output is correct
27 Correct 9 ms 16084 KB Output is correct
28 Correct 12 ms 16096 KB Output is correct
29 Correct 10 ms 15992 KB Output is correct
30 Correct 9 ms 16084 KB Output is correct
31 Correct 9 ms 16084 KB Output is correct
32 Correct 10 ms 16096 KB Output is correct
33 Correct 10 ms 16092 KB Output is correct
34 Correct 9 ms 16084 KB Output is correct
35 Correct 9 ms 16088 KB Output is correct
36 Correct 9 ms 16084 KB Output is correct
37 Correct 12 ms 16088 KB Output is correct
38 Correct 9 ms 16104 KB Output is correct
39 Correct 208 ms 26468 KB Output is correct
40 Correct 225 ms 26316 KB Output is correct
41 Correct 212 ms 26708 KB Output is correct
42 Correct 262 ms 27176 KB Output is correct
43 Correct 104 ms 26436 KB Output is correct
44 Correct 88 ms 26444 KB Output is correct
45 Correct 180 ms 29652 KB Output is correct
46 Correct 122 ms 33396 KB Output is correct
47 Correct 265 ms 36864 KB Output is correct
48 Correct 426 ms 49612 KB Output is correct
49 Correct 434 ms 48608 KB Output is correct
50 Correct 260 ms 36864 KB Output is correct
51 Correct 270 ms 36900 KB Output is correct
52 Correct 368 ms 36812 KB Output is correct
53 Correct 319 ms 36972 KB Output is correct
54 Correct 327 ms 37704 KB Output is correct
55 Correct 18 ms 16980 KB Output is correct
56 Correct 19 ms 17048 KB Output is correct
57 Correct 104 ms 26880 KB Output is correct
58 Correct 51 ms 27568 KB Output is correct
59 Correct 127 ms 33324 KB Output is correct
60 Correct 594 ms 50320 KB Output is correct
61 Correct 314 ms 37280 KB Output is correct
62 Correct 295 ms 37848 KB Output is correct
63 Correct 409 ms 38008 KB Output is correct
64 Correct 730 ms 38912 KB Output is correct
65 Correct 433 ms 37920 KB Output is correct
66 Correct 508 ms 46732 KB Output is correct
67 Correct 141 ms 40228 KB Output is correct
68 Correct 395 ms 37888 KB Output is correct
69 Correct 417 ms 38124 KB Output is correct
70 Correct 396 ms 36928 KB Output is correct