Submission #500892

# Submission time Handle Problem Language Result Execution time Memory
500892 2022-01-01T15:09:22 Z aymanrs Race (IOI11_race) C++14
100 / 100
581 ms 109176 KB
#include <bits/stdc++.h>
using namespace std;
struct node {
	list<pair<node*, int>> l;
	int subtree = 1;
	set<pair<long long, int>>* s = nullptr;
};
int m = 1e9, k;
void dfs(node* n, node* parent, long long sum, int depth){
	int ms = -1;
	node* mc = nullptr;
	for(auto p : n->l){
		if(p.first != parent){
			dfs(p.first, n, sum + p.second, depth + 1);
			n->subtree += p.first->subtree;
			if(p.first->subtree > ms){
				ms = p.first->subtree;
				mc = p.first;
			}
		}
	}
	if(ms == -1){
		n->s = new set<pair<long long, int>>();
	} else {
		n->s = mc->s;
	}
	n->s->insert({sum, depth});
	for(auto p : n->l){
		if(p.first != parent && p.first != mc){
			for(auto f : *p.first->s){
				long long target = k + 2 * sum - f.first;
				auto it = n->s->lower_bound({target, 0});
				if(it != n->s->end() && it->first == target){
					if(f.second + it->second - 2 * depth < m) m = f.second + it->second - 2 * depth;
				}
			}
			for(auto f : *p.first->s){
				n->s->insert(f);
			}
		}
	}
	auto it = n->s->lower_bound({k + sum, 0});
	if(it != n->s->end() && it->first == k + sum){
		if(depth + it->second - 2 * depth < m) m = depth + it->second - 2 * depth;
	}
}
int best_path(int N, int K, int H[][2], int L[]){
	k = K;
	node nodes[N+1];
	int u, v, c;
	for(int i = 0;i < N-1;i++){
		u = H[i][0];
		v = H[i][1];
		c = L[i];
		nodes[u].l.push_back({&nodes[v], c});
		nodes[v].l.push_back({&nodes[u], c});
	}
	dfs(&nodes[1], nullptr, 0, 0);
	map<set<pair<long long, int>>*, bool> del;
	for(int i = 1;i <= N;i++) {
		if(del[nodes[i].s]) continue;
		delete nodes[i].s;
		del[nodes[i].s] = true;
	}
	return m == 1e9 ? -1 : m;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 288 KB Output is correct
12 Correct 1 ms 288 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 288 KB Output is correct
12 Correct 1 ms 288 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 292 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 560 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 2 ms 568 KB Output is correct
24 Correct 3 ms 692 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 1 ms 588 KB Output is correct
27 Correct 1 ms 420 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 1 ms 580 KB Output is correct
31 Correct 2 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Correct 2 ms 588 KB Output is correct
34 Correct 1 ms 588 KB Output is correct
35 Correct 1 ms 560 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 288 KB Output is correct
12 Correct 1 ms 288 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 201 ms 34592 KB Output is correct
20 Correct 165 ms 34368 KB Output is correct
21 Correct 197 ms 34096 KB Output is correct
22 Correct 221 ms 33740 KB Output is correct
23 Correct 240 ms 49024 KB Output is correct
24 Correct 206 ms 39968 KB Output is correct
25 Correct 118 ms 37072 KB Output is correct
26 Correct 84 ms 38024 KB Output is correct
27 Correct 257 ms 54164 KB Output is correct
28 Correct 272 ms 74836 KB Output is correct
29 Correct 296 ms 68552 KB Output is correct
30 Correct 250 ms 54376 KB Output is correct
31 Correct 277 ms 54128 KB Output is correct
32 Correct 360 ms 54212 KB Output is correct
33 Correct 244 ms 45012 KB Output is correct
34 Correct 314 ms 78880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 0 ms 288 KB Output is correct
12 Correct 1 ms 288 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 0 ms 292 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 560 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 2 ms 568 KB Output is correct
24 Correct 3 ms 692 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 1 ms 588 KB Output is correct
27 Correct 1 ms 420 KB Output is correct
28 Correct 2 ms 588 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 1 ms 580 KB Output is correct
31 Correct 2 ms 588 KB Output is correct
32 Correct 1 ms 588 KB Output is correct
33 Correct 2 ms 588 KB Output is correct
34 Correct 1 ms 588 KB Output is correct
35 Correct 1 ms 560 KB Output is correct
36 Correct 1 ms 460 KB Output is correct
37 Correct 1 ms 460 KB Output is correct
38 Correct 1 ms 564 KB Output is correct
39 Correct 201 ms 34592 KB Output is correct
40 Correct 165 ms 34368 KB Output is correct
41 Correct 197 ms 34096 KB Output is correct
42 Correct 221 ms 33740 KB Output is correct
43 Correct 240 ms 49024 KB Output is correct
44 Correct 206 ms 39968 KB Output is correct
45 Correct 118 ms 37072 KB Output is correct
46 Correct 84 ms 38024 KB Output is correct
47 Correct 257 ms 54164 KB Output is correct
48 Correct 272 ms 74836 KB Output is correct
49 Correct 296 ms 68552 KB Output is correct
50 Correct 250 ms 54376 KB Output is correct
51 Correct 277 ms 54128 KB Output is correct
52 Correct 360 ms 54212 KB Output is correct
53 Correct 244 ms 45012 KB Output is correct
54 Correct 314 ms 78880 KB Output is correct
55 Correct 15 ms 4432 KB Output is correct
56 Correct 10 ms 3128 KB Output is correct
57 Correct 96 ms 31404 KB Output is correct
58 Correct 99 ms 28348 KB Output is correct
59 Correct 75 ms 38000 KB Output is correct
60 Correct 247 ms 66876 KB Output is correct
61 Correct 290 ms 60044 KB Output is correct
62 Correct 256 ms 54224 KB Output is correct
63 Correct 349 ms 54172 KB Output is correct
64 Correct 557 ms 109176 KB Output is correct
65 Correct 581 ms 102460 KB Output is correct
66 Correct 239 ms 73372 KB Output is correct
67 Correct 440 ms 57540 KB Output is correct
68 Correct 458 ms 72772 KB Output is correct
69 Correct 560 ms 74040 KB Output is correct
70 Correct 438 ms 69696 KB Output is correct