Submission #287353

# Submission time Handle Problem Language Result Execution time Memory
287353 2020-08-31T16:20:46 Z Namnamseo Race (IOI11_race) C++17
100 / 100
2936 ms 50936 KB
#include <bits/stdc++.h>
using namespace std;
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define coord_comp(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define v_index(v, x) (lower_bound(all(v),x)-(v).begin())
typedef pair<int,int> pp;
typedef long long ll;

int n,k;

vector<pp> edge[200001];

bool dead[200010];

int nt;
int pre_time[200010];
int pre_par [200010];
int pre_size[200010];
int pre_mcsz[200010];

void pre_dfs(int x){
	pre_size[x]=1;
	pre_mcsz[x]=0;
	pre_time[x]=nt;
	for(pp ep : edge[x]){
		int y = ep.first;
		if(dead[y] || (pre_time[y]==nt && pre_par[x]==y)) continue;
		pre_par[y]=x;
		pre_dfs(y);
		pre_mcsz[x] = max(pre_mcsz[x], pre_size[y]);
		pre_size[x] += pre_size[y];
	}
}

int find_cent(int root){
	++nt;
	pre_par[root] = -1;
	pre_dfs(root);
	int sz=pre_size[root];
	while(true){
		if(pre_mcsz[root] <= sz/2) break;
		for(pp ep:edge[root]){
			int y=ep.first;
			if(dead[y] || pre_par[root]==y) continue;
			if(pre_mcsz[root] == pre_size[y]){
				root=y; break;
			}
		}
	}
	return root;
}

int ans=2e9;

int par[200010];

void dfs_child(int x, int depth, int dc, set<pp>& s){
	s.insert({depth,dc});
	for(pp ep:edge[x]){
		int y=ep.first;
		if(dead[y] || par[x]==y) continue;
		par[y]=x;
		dfs_child(y, depth+ep.second, dc+1, s);
	}
}

inline void find_pairs(pp& t,multiset<pp>& s){
	if(t.first > k) return;
	auto it=s.lower_bound({k-t.first,0});
	if(it!=s.end() && it -> first == k-t.first){
		ans = min(ans, t.second + it->second);
	}
}

void treat_root(int x){
	vector<set<pp>> sets;
	for(pp ep:edge[x]){
		int y=ep.first;
		if(dead[y]) continue;
		par[y]=x;
		set<pp> ns;
		dfs_child(y, ep.second, 1, ns);
		sets.pb(ns);
	}
	multiset<pp> s;
	for(auto& ss:sets){
		for(pp t:ss) s.insert(t);
	}
	s.insert({0,0});
	for(auto& v:sets){
		for(auto t:v) s.erase(s.find(t));
		for(auto t:v) find_pairs(t, s);
		for(auto t:v) s.insert(t);
	}
}

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 a=H[i][0],b=H[i][1],c=L[i];
		edge[a].pb({b,c});
		edge[b].pb({a,c});
	}

	queue<int> q;
	q.push(1);
	while(q.size()){
		int t = q.front(); q.pop();
		int cent = find_cent(t);
		treat_root(cent);
		dead[cent] = true;
		for(pp ep:edge[cent]){
			int y = ep.first;
			if(!dead[y]){
				q.push(y);
			}
		}
	}
	if(ans == int(2e9)) ans=-1;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5092 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 4 ms 5120 KB Output is correct
21 Correct 7 ms 5248 KB Output is correct
22 Correct 9 ms 5248 KB Output is correct
23 Correct 8 ms 5248 KB Output is correct
24 Correct 7 ms 5248 KB Output is correct
25 Correct 11 ms 5248 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 5 ms 5120 KB Output is correct
28 Correct 8 ms 5248 KB Output is correct
29 Correct 7 ms 5248 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 7 ms 5248 KB Output is correct
32 Correct 9 ms 5248 KB Output is correct
33 Correct 8 ms 5248 KB Output is correct
34 Correct 8 ms 5248 KB Output is correct
35 Correct 7 ms 5248 KB Output is correct
36 Correct 7 ms 5248 KB Output is correct
37 Correct 8 ms 5248 KB Output is correct
38 Correct 7 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5092 KB Output is correct
19 Correct 692 ms 13856 KB Output is correct
20 Correct 693 ms 13932 KB Output is correct
21 Correct 658 ms 13600 KB Output is correct
22 Correct 584 ms 13944 KB Output is correct
23 Correct 831 ms 14584 KB Output is correct
24 Correct 333 ms 14072 KB Output is correct
25 Correct 1226 ms 27916 KB Output is correct
26 Correct 840 ms 27768 KB Output is correct
27 Correct 885 ms 31480 KB Output is correct
28 Correct 2874 ms 50936 KB Output is correct
29 Correct 2783 ms 50936 KB Output is correct
30 Correct 881 ms 31480 KB Output is correct
31 Correct 879 ms 31480 KB Output is correct
32 Correct 1116 ms 31484 KB Output is correct
33 Correct 1443 ms 21512 KB Output is correct
34 Correct 2316 ms 40184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Correct 4 ms 5120 KB Output is correct
3 Correct 4 ms 5120 KB Output is correct
4 Correct 4 ms 5120 KB Output is correct
5 Correct 4 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 4 ms 5120 KB Output is correct
10 Correct 4 ms 5120 KB Output is correct
11 Correct 4 ms 5120 KB Output is correct
12 Correct 4 ms 5120 KB Output is correct
13 Correct 4 ms 5120 KB Output is correct
14 Correct 4 ms 5120 KB Output is correct
15 Correct 5 ms 5120 KB Output is correct
16 Correct 4 ms 5120 KB Output is correct
17 Correct 4 ms 5120 KB Output is correct
18 Correct 4 ms 5092 KB Output is correct
19 Correct 4 ms 5120 KB Output is correct
20 Correct 4 ms 5120 KB Output is correct
21 Correct 7 ms 5248 KB Output is correct
22 Correct 9 ms 5248 KB Output is correct
23 Correct 8 ms 5248 KB Output is correct
24 Correct 7 ms 5248 KB Output is correct
25 Correct 11 ms 5248 KB Output is correct
26 Correct 7 ms 5248 KB Output is correct
27 Correct 5 ms 5120 KB Output is correct
28 Correct 8 ms 5248 KB Output is correct
29 Correct 7 ms 5248 KB Output is correct
30 Correct 7 ms 5248 KB Output is correct
31 Correct 7 ms 5248 KB Output is correct
32 Correct 9 ms 5248 KB Output is correct
33 Correct 8 ms 5248 KB Output is correct
34 Correct 8 ms 5248 KB Output is correct
35 Correct 7 ms 5248 KB Output is correct
36 Correct 7 ms 5248 KB Output is correct
37 Correct 8 ms 5248 KB Output is correct
38 Correct 7 ms 5248 KB Output is correct
39 Correct 692 ms 13856 KB Output is correct
40 Correct 693 ms 13932 KB Output is correct
41 Correct 658 ms 13600 KB Output is correct
42 Correct 584 ms 13944 KB Output is correct
43 Correct 831 ms 14584 KB Output is correct
44 Correct 333 ms 14072 KB Output is correct
45 Correct 1226 ms 27916 KB Output is correct
46 Correct 840 ms 27768 KB Output is correct
47 Correct 885 ms 31480 KB Output is correct
48 Correct 2874 ms 50936 KB Output is correct
49 Correct 2783 ms 50936 KB Output is correct
50 Correct 881 ms 31480 KB Output is correct
51 Correct 879 ms 31480 KB Output is correct
52 Correct 1116 ms 31484 KB Output is correct
53 Correct 1443 ms 21512 KB Output is correct
54 Correct 2316 ms 40184 KB Output is correct
55 Correct 57 ms 6784 KB Output is correct
56 Correct 36 ms 5888 KB Output is correct
57 Correct 303 ms 13816 KB Output is correct
58 Correct 174 ms 27220 KB Output is correct
59 Correct 943 ms 27692 KB Output is correct
60 Correct 2838 ms 50680 KB Output is correct
61 Correct 1022 ms 34168 KB Output is correct
62 Correct 938 ms 31480 KB Output is correct
63 Correct 1186 ms 31608 KB Output is correct
64 Correct 2604 ms 40160 KB Output is correct
65 Correct 2109 ms 41448 KB Output is correct
66 Correct 2936 ms 50900 KB Output is correct
67 Correct 530 ms 50504 KB Output is correct
68 Correct 1280 ms 41092 KB Output is correct
69 Correct 1281 ms 41340 KB Output is correct
70 Correct 1170 ms 39420 KB Output is correct