Submission #50060

# Submission time Handle Problem Language Result Execution time Memory
50060 2018-06-07T08:20:31 Z Diuven Race (IOI11_race) C++11
100 / 100
714 ms 33520 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int KMX=1000010, MX=200010, inf=2e9;
typedef pair<int, int> pii;

vector<pii> G[MX];
bool done[MX];

int sub[MX];

void init(int v, int par=-1){
	sub[v]=1;
	for(pii &p:G[v]){
		int x=p.first;
		if(done[x] || x==par) continue;
		init(x,v); sub[v]+=sub[x];
	}
}

int findcen(int v, int sz){
	for(pii &p:G[v]){
		int x=p.first;
		if(done[x] || sub[x]>sub[v]) continue;
		if(sub[x]*2>=sz) return findcen(x,sz);
	}
	return v;
}

int dist[KMX];
vector<pii> V, W;
int n, k;

void dfs(int v, int par=-1, int dep=0, int cnt=0){
	for(pii &p:G[v]){
		int x,c; tie(x,c)=p;
		if(x==par || done[x]) continue;
		dfs(x,v,dep+c,cnt+1);
	}
	if(0<dep && dep<=k){
		V.push_back({dep, cnt});
	}
}

int solve(int v){
	init(v); v=findcen(v, sub[v]); done[v]=true;
	int ans=inf;
	for(pii &p:G[v]){
		int x,c; tie(x,c)=p;
		if(done[x]) continue;
		dfs(x,v,c,1);

		// cutting?
		for(pii &q:V){
			int dep, cnt; tie(dep, cnt)=q;
			ans=min(ans, cnt+dist[k-dep]);
		}
		for(pii &q:V){
			int dep, cnt; tie(dep, cnt)=q;
			dist[dep]=min(dist[dep], cnt);
			W.push_back(q);
		}
		V.clear();
	}
	for(pii &q:W){
		int dep=q.first;
		dist[dep]=inf;
	}
	W.clear();
	for(pii &p:G[v]){
		int x=p.first;
		if(done[x]) continue;
		ans=min(solve(x), ans);
	}
	return ans;
}

int best_path(int N, int K, int H[][2], int L[]){
	n=N, k=K;
	for(int i=0; i<n-1; i++){
		int u=H[i][0], v=H[i][1], c=L[i];
		G[u].push_back({v,c});
		G[v].push_back({u,c});
	}
	fill(dist+1, dist+k+1, inf);
	int ans=solve(0);
	return ans>=inf/2 ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 5 ms 5216 KB Output is correct
3 Correct 5 ms 5216 KB Output is correct
4 Correct 5 ms 5228 KB Output is correct
5 Correct 6 ms 5284 KB Output is correct
6 Correct 6 ms 5396 KB Output is correct
7 Correct 5 ms 5396 KB Output is correct
8 Correct 5 ms 5396 KB Output is correct
9 Correct 5 ms 5396 KB Output is correct
10 Correct 8 ms 5396 KB Output is correct
11 Correct 6 ms 5396 KB Output is correct
12 Correct 6 ms 5396 KB Output is correct
13 Correct 6 ms 5396 KB Output is correct
14 Correct 6 ms 5396 KB Output is correct
15 Correct 7 ms 5396 KB Output is correct
16 Correct 7 ms 5396 KB Output is correct
17 Correct 6 ms 5396 KB Output is correct
18 Correct 9 ms 5396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 5 ms 5216 KB Output is correct
3 Correct 5 ms 5216 KB Output is correct
4 Correct 5 ms 5228 KB Output is correct
5 Correct 6 ms 5284 KB Output is correct
6 Correct 6 ms 5396 KB Output is correct
7 Correct 5 ms 5396 KB Output is correct
8 Correct 5 ms 5396 KB Output is correct
9 Correct 5 ms 5396 KB Output is correct
10 Correct 8 ms 5396 KB Output is correct
11 Correct 6 ms 5396 KB Output is correct
12 Correct 6 ms 5396 KB Output is correct
13 Correct 6 ms 5396 KB Output is correct
14 Correct 6 ms 5396 KB Output is correct
15 Correct 7 ms 5396 KB Output is correct
16 Correct 7 ms 5396 KB Output is correct
17 Correct 6 ms 5396 KB Output is correct
18 Correct 9 ms 5396 KB Output is correct
19 Correct 6 ms 5396 KB Output is correct
20 Correct 6 ms 5396 KB Output is correct
21 Correct 7 ms 5396 KB Output is correct
22 Correct 10 ms 8940 KB Output is correct
23 Correct 11 ms 8940 KB Output is correct
24 Correct 10 ms 8940 KB Output is correct
25 Correct 10 ms 8940 KB Output is correct
26 Correct 9 ms 8940 KB Output is correct
27 Correct 10 ms 8940 KB Output is correct
28 Correct 9 ms 8940 KB Output is correct
29 Correct 10 ms 8940 KB Output is correct
30 Correct 8 ms 8940 KB Output is correct
31 Correct 9 ms 8940 KB Output is correct
32 Correct 10 ms 8940 KB Output is correct
33 Correct 10 ms 8940 KB Output is correct
34 Correct 10 ms 8940 KB Output is correct
35 Correct 9 ms 8940 KB Output is correct
36 Correct 9 ms 8956 KB Output is correct
37 Correct 10 ms 8956 KB Output is correct
38 Correct 11 ms 8956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 5 ms 5216 KB Output is correct
3 Correct 5 ms 5216 KB Output is correct
4 Correct 5 ms 5228 KB Output is correct
5 Correct 6 ms 5284 KB Output is correct
6 Correct 6 ms 5396 KB Output is correct
7 Correct 5 ms 5396 KB Output is correct
8 Correct 5 ms 5396 KB Output is correct
9 Correct 5 ms 5396 KB Output is correct
10 Correct 8 ms 5396 KB Output is correct
11 Correct 6 ms 5396 KB Output is correct
12 Correct 6 ms 5396 KB Output is correct
13 Correct 6 ms 5396 KB Output is correct
14 Correct 6 ms 5396 KB Output is correct
15 Correct 7 ms 5396 KB Output is correct
16 Correct 7 ms 5396 KB Output is correct
17 Correct 6 ms 5396 KB Output is correct
18 Correct 9 ms 5396 KB Output is correct
19 Correct 167 ms 10920 KB Output is correct
20 Correct 180 ms 10920 KB Output is correct
21 Correct 210 ms 11384 KB Output is correct
22 Correct 184 ms 12024 KB Output is correct
23 Correct 135 ms 12024 KB Output is correct
24 Correct 100 ms 12024 KB Output is correct
25 Correct 259 ms 14968 KB Output is correct
26 Correct 102 ms 17780 KB Output is correct
27 Correct 290 ms 17780 KB Output is correct
28 Correct 618 ms 27284 KB Output is correct
29 Correct 575 ms 27284 KB Output is correct
30 Correct 222 ms 27284 KB Output is correct
31 Correct 261 ms 27284 KB Output is correct
32 Correct 357 ms 27284 KB Output is correct
33 Correct 518 ms 27284 KB Output is correct
34 Correct 513 ms 27284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5112 KB Output is correct
2 Correct 5 ms 5216 KB Output is correct
3 Correct 5 ms 5216 KB Output is correct
4 Correct 5 ms 5228 KB Output is correct
5 Correct 6 ms 5284 KB Output is correct
6 Correct 6 ms 5396 KB Output is correct
7 Correct 5 ms 5396 KB Output is correct
8 Correct 5 ms 5396 KB Output is correct
9 Correct 5 ms 5396 KB Output is correct
10 Correct 8 ms 5396 KB Output is correct
11 Correct 6 ms 5396 KB Output is correct
12 Correct 6 ms 5396 KB Output is correct
13 Correct 6 ms 5396 KB Output is correct
14 Correct 6 ms 5396 KB Output is correct
15 Correct 7 ms 5396 KB Output is correct
16 Correct 7 ms 5396 KB Output is correct
17 Correct 6 ms 5396 KB Output is correct
18 Correct 9 ms 5396 KB Output is correct
19 Correct 6 ms 5396 KB Output is correct
20 Correct 6 ms 5396 KB Output is correct
21 Correct 7 ms 5396 KB Output is correct
22 Correct 10 ms 8940 KB Output is correct
23 Correct 11 ms 8940 KB Output is correct
24 Correct 10 ms 8940 KB Output is correct
25 Correct 10 ms 8940 KB Output is correct
26 Correct 9 ms 8940 KB Output is correct
27 Correct 10 ms 8940 KB Output is correct
28 Correct 9 ms 8940 KB Output is correct
29 Correct 10 ms 8940 KB Output is correct
30 Correct 8 ms 8940 KB Output is correct
31 Correct 9 ms 8940 KB Output is correct
32 Correct 10 ms 8940 KB Output is correct
33 Correct 10 ms 8940 KB Output is correct
34 Correct 10 ms 8940 KB Output is correct
35 Correct 9 ms 8940 KB Output is correct
36 Correct 9 ms 8956 KB Output is correct
37 Correct 10 ms 8956 KB Output is correct
38 Correct 11 ms 8956 KB Output is correct
39 Correct 167 ms 10920 KB Output is correct
40 Correct 180 ms 10920 KB Output is correct
41 Correct 210 ms 11384 KB Output is correct
42 Correct 184 ms 12024 KB Output is correct
43 Correct 135 ms 12024 KB Output is correct
44 Correct 100 ms 12024 KB Output is correct
45 Correct 259 ms 14968 KB Output is correct
46 Correct 102 ms 17780 KB Output is correct
47 Correct 290 ms 17780 KB Output is correct
48 Correct 618 ms 27284 KB Output is correct
49 Correct 575 ms 27284 KB Output is correct
50 Correct 222 ms 27284 KB Output is correct
51 Correct 261 ms 27284 KB Output is correct
52 Correct 357 ms 27284 KB Output is correct
53 Correct 518 ms 27284 KB Output is correct
54 Correct 513 ms 27284 KB Output is correct
55 Correct 18 ms 27284 KB Output is correct
56 Correct 22 ms 27284 KB Output is correct
57 Correct 90 ms 27284 KB Output is correct
58 Correct 53 ms 27284 KB Output is correct
59 Correct 134 ms 27284 KB Output is correct
60 Correct 563 ms 33520 KB Output is correct
61 Correct 230 ms 33520 KB Output is correct
62 Correct 288 ms 33520 KB Output is correct
63 Correct 307 ms 33520 KB Output is correct
64 Correct 604 ms 33520 KB Output is correct
65 Correct 441 ms 33520 KB Output is correct
66 Correct 714 ms 33520 KB Output is correct
67 Correct 178 ms 33520 KB Output is correct
68 Correct 323 ms 33520 KB Output is correct
69 Correct 330 ms 33520 KB Output is correct
70 Correct 299 ms 33520 KB Output is correct