Submission #263339

# Submission time Handle Problem Language Result Execution time Memory
263339 2020-08-13T15:49:41 Z Berted Race (IOI11_race) C++14
100 / 100
478 ms 99592 KB
#include "race.h"
#include <vector>
#include <iostream>
#include <set>
#define ll long long
#define vi vector<int>
#define pii pair<ll, ll>
#define fst first
#define snd second
#define vpi vector<pii>
#define pub push_back
#define spi set<pii>

using namespace std;

const int INF = 1e9;

vpi adj[200001];
int ans = INF, k;

struct DS
{
	ll a = 0, b = 0; set<pii> s;
	void insert(ll x, ll y) {s.insert({x - a, y - b});}
	void offset(ll x, ll y) {a += x; b += y;}
	int size() {return s.size();}
	int retMin(ll t)
	{
		auto it = s.upper_bound({t - a, -1000000000});
		if (it != s.end() && it -> fst + a == t) return it -> snd + b;
		else return INF;
	}
};

DS* DFS(int u, int p, int pv)
{
	DS* ret = nullptr;
	for (auto v : adj[u])
	{
		if (v.fst != p)
		{
			DS* ret2 = DFS(v.fst, u, v.snd);
			if (ret == nullptr) {ret = ret2;}
			else if (ret -> size() < ret2 -> size())
			{
				for (const auto &v : ret -> s)
				{
					ll vv = v.fst + ret -> a;
					ans = min(ans, (int)ret -> b + (int)v.snd + ret2 -> retMin(k - vv));
				}
				for (const auto &v : ret -> s)
				{
					ret2 -> insert(v.fst + ret -> a, v.snd + ret -> b);
				}
				swap(ret, ret2);
			}
			else
			{
				for (const auto &v : ret2 -> s)
				{
					ll vv = v.fst + ret2 -> a;
					ans = min(ans, (int)ret2 -> b + (int)v.snd + ret -> retMin(k - vv));
				}
				for (const auto &v : ret2 -> s)
				{
					ret -> insert(v.fst + ret2 -> a, v.snd + ret2 -> b);
				}
			}
		}
	}
	if (ret == nullptr) {ret = new DS;}
	ret -> insert(0, 0);
	ans = min(ans, ret -> retMin(k));

	/*cout << "Node " << u << " " << p << " " << ret -> a << " " << ret -> b << ":\n";
	for (auto x : ret -> s)
	{
		cout << x.fst + ret -> a << ", " << x.snd + ret -> b << "\n";
	}*/

	ret -> offset(pv, 1);
	return ret;
}

int best_path(int N, int K, int H[][2], int L[])
{
	k = K;
	for (int i = 0; i < N - 1; i++)
	{
		adj[H[i][0]].emplace_back(H[i][1], L[i]);
		adj[H[i][1]].emplace_back(H[i][0], L[i]);
	}
	DFS(0, -1, 0);
	if (ans == INF) return -1;
	else return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 3 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 3 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 5 ms 5376 KB Output is correct
22 Correct 5 ms 5504 KB Output is correct
23 Correct 5 ms 5376 KB Output is correct
24 Correct 4 ms 5376 KB Output is correct
25 Correct 5 ms 5376 KB Output is correct
26 Correct 5 ms 5376 KB Output is correct
27 Correct 4 ms 5248 KB Output is correct
28 Correct 5 ms 5376 KB Output is correct
29 Correct 5 ms 5376 KB Output is correct
30 Correct 5 ms 5376 KB Output is correct
31 Correct 5 ms 5376 KB Output is correct
32 Correct 5 ms 5376 KB Output is correct
33 Correct 6 ms 5376 KB Output is correct
34 Correct 4 ms 5248 KB Output is correct
35 Correct 4 ms 5248 KB Output is correct
36 Correct 4 ms 5248 KB Output is correct
37 Correct 4 ms 5248 KB Output is correct
38 Correct 4 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 3 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 143 ms 32100 KB Output is correct
20 Correct 138 ms 32148 KB Output is correct
21 Correct 137 ms 31736 KB Output is correct
22 Correct 141 ms 31456 KB Output is correct
23 Correct 170 ms 46328 KB Output is correct
24 Correct 129 ms 37456 KB Output is correct
25 Correct 128 ms 31352 KB Output is correct
26 Correct 91 ms 37496 KB Output is correct
27 Correct 275 ms 44920 KB Output is correct
28 Correct 253 ms 69880 KB Output is correct
29 Correct 263 ms 68088 KB Output is correct
30 Correct 257 ms 44792 KB Output is correct
31 Correct 262 ms 44792 KB Output is correct
32 Correct 281 ms 44920 KB Output is correct
33 Correct 196 ms 32632 KB Output is correct
34 Correct 306 ms 65656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 4 ms 4992 KB Output is correct
3 Correct 4 ms 4992 KB Output is correct
4 Correct 4 ms 4992 KB Output is correct
5 Correct 4 ms 4992 KB Output is correct
6 Correct 4 ms 4992 KB Output is correct
7 Correct 4 ms 4992 KB Output is correct
8 Correct 4 ms 4992 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 4 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Correct 4 ms 4992 KB Output is correct
15 Correct 4 ms 4992 KB Output is correct
16 Correct 3 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 4 ms 4992 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 5 ms 5376 KB Output is correct
22 Correct 5 ms 5504 KB Output is correct
23 Correct 5 ms 5376 KB Output is correct
24 Correct 4 ms 5376 KB Output is correct
25 Correct 5 ms 5376 KB Output is correct
26 Correct 5 ms 5376 KB Output is correct
27 Correct 4 ms 5248 KB Output is correct
28 Correct 5 ms 5376 KB Output is correct
29 Correct 5 ms 5376 KB Output is correct
30 Correct 5 ms 5376 KB Output is correct
31 Correct 5 ms 5376 KB Output is correct
32 Correct 5 ms 5376 KB Output is correct
33 Correct 6 ms 5376 KB Output is correct
34 Correct 4 ms 5248 KB Output is correct
35 Correct 4 ms 5248 KB Output is correct
36 Correct 4 ms 5248 KB Output is correct
37 Correct 4 ms 5248 KB Output is correct
38 Correct 4 ms 5248 KB Output is correct
39 Correct 143 ms 32100 KB Output is correct
40 Correct 138 ms 32148 KB Output is correct
41 Correct 137 ms 31736 KB Output is correct
42 Correct 141 ms 31456 KB Output is correct
43 Correct 170 ms 46328 KB Output is correct
44 Correct 129 ms 37456 KB Output is correct
45 Correct 128 ms 31352 KB Output is correct
46 Correct 91 ms 37496 KB Output is correct
47 Correct 275 ms 44920 KB Output is correct
48 Correct 253 ms 69880 KB Output is correct
49 Correct 263 ms 68088 KB Output is correct
50 Correct 257 ms 44792 KB Output is correct
51 Correct 262 ms 44792 KB Output is correct
52 Correct 281 ms 44920 KB Output is correct
53 Correct 196 ms 32632 KB Output is correct
54 Correct 306 ms 65656 KB Output is correct
55 Correct 17 ms 8576 KB Output is correct
56 Correct 13 ms 7168 KB Output is correct
57 Correct 83 ms 28152 KB Output is correct
58 Correct 81 ms 26084 KB Output is correct
59 Correct 103 ms 37240 KB Output is correct
60 Correct 251 ms 68600 KB Output is correct
61 Correct 309 ms 50680 KB Output is correct
62 Correct 258 ms 44920 KB Output is correct
63 Correct 292 ms 44920 KB Output is correct
64 Correct 476 ms 99592 KB Output is correct
65 Correct 478 ms 94072 KB Output is correct
66 Correct 293 ms 64064 KB Output is correct
67 Correct 246 ms 48132 KB Output is correct
68 Correct 390 ms 64124 KB Output is correct
69 Correct 425 ms 69980 KB Output is correct
70 Correct 402 ms 61772 KB Output is correct