Submission #231799

# Submission time Handle Problem Language Result Execution time Memory
231799 2020-05-14T20:01:02 Z peijar Race (IOI11_race) C++17
100 / 100
600 ms 34408 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define SZ(v) ((int)(v).size())

using ll = long long;

const int MAXN = 2e5;
int ans;
vector<pair<int, int>> G[MAXN];
int sz[MAXN];
int target;
int sz_above[MAXN];
int par[MAXN];
queue<int> q;
bool blocked[MAXN];

struct Path
{
	int len, val;
};

vector<Path> paths;
const int MAX = 1e6+1;
int have[MAX];

void dfs(int u)
{
	sz[u] = 1;
	for (auto [v, w] : G[u])
		if (!blocked[v] and v != par[u])
		{
			par[v] = u;
			dfs(v);
			sz[u] += sz[v];
		}
}

int get_centroid(int source)
{
	int centroid = source;
	int bst = sz[source];
	sz_above[source] = 0;
	q.push(source);
	while (SZ(q))
	{
		int u = q.front(); q.pop();

		int cur_sz = sz_above[u];
		for (auto [v, w] : G[u])
			if (!blocked[v] and v != par[u])
			{
				cur_sz = max(cur_sz, sz[v]);
				sz_above[v] = sz_above[u] + sz[u] - sz[v];
				q.push(v);
			}
		if (cur_sz < bst)
			bst = cur_sz, centroid = u;
	}
	return centroid;
}

void dfs2(int u, int p, int depth, int cur_weight)
{
	if (cur_weight > target)
		return ;
	paths.push_back({depth, cur_weight});
	for (auto [v, w] : G[u])
		if (v != p and !blocked[v])
			dfs2(v, u, depth + 1, cur_weight + w);
}

void run_centroid(int source)
{
	par[source] = -1;
	dfs(source);
	vector<int> to_reset;

	int centroid = get_centroid(source);
	have[0] = 0;
	for (auto [v, w] : G[centroid])
		if (!blocked[v])
		{
			dfs2(v, centroid, 1, w);
			for (auto [len, val] : paths)
				if (have[target - val] != -1)
					ans = min(ans, have[target - val] + len);
			for (auto [len, val] : paths)
			{
				if (have[val] != -1)
					have[val] = min(have[val], len);
				else
				{
					have[val] = len;
					to_reset.push_back(val);
				}
			}
			paths.clear();
		}
	for (auto v : to_reset)
		have[v] = -1;
	blocked[centroid] = true;
	for (auto [v, _] : G[centroid])
		if (!blocked[v])
			run_centroid(v);
}

int best_path(int nb_villes, int _target, int edges[][2], int lengths[])
{
	for (int i(0); i < MAX; ++i)
		have[i] = -1;
	target = _target;
	ans = 1e9;
	for (int i(0); i < nb_villes - 1; ++i)
	{
		G[edges[i][0]].emplace_back(edges[i][1], lengths[i]);
		G[edges[i][1]].emplace_back(edges[i][0], lengths[i]);
	}
	run_centroid(0);

	if (ans == 1e9)
		ans = -1;
	return ans;
}

Compilation message

race.cpp: In function 'void dfs(int)':
race.cpp:30:17: warning: unused variable 'w' [-Wunused-variable]
  for (auto [v, w] : G[u])
                 ^
race.cpp: In function 'int get_centroid(int)':
race.cpp:50:18: warning: unused variable 'w' [-Wunused-variable]
   for (auto [v, w] : G[u])
                  ^
race.cpp: In function 'void run_centroid(int)':
race.cpp:103:17: warning: unused variable '_' [-Wunused-variable]
  for (auto [v, _] : G[centroid])
                 ^
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8960 KB Output is correct
2 Correct 9 ms 8960 KB Output is correct
3 Correct 9 ms 8960 KB Output is correct
4 Correct 9 ms 8960 KB Output is correct
5 Correct 9 ms 8960 KB Output is correct
6 Correct 9 ms 8960 KB Output is correct
7 Correct 10 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 9088 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 9 ms 8960 KB Output is correct
14 Correct 9 ms 8960 KB Output is correct
15 Correct 9 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 9 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8960 KB Output is correct
2 Correct 9 ms 8960 KB Output is correct
3 Correct 9 ms 8960 KB Output is correct
4 Correct 9 ms 8960 KB Output is correct
5 Correct 9 ms 8960 KB Output is correct
6 Correct 9 ms 8960 KB Output is correct
7 Correct 10 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 9088 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 9 ms 8960 KB Output is correct
14 Correct 9 ms 8960 KB Output is correct
15 Correct 9 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 9 ms 8960 KB Output is correct
19 Correct 9 ms 8960 KB Output is correct
20 Correct 9 ms 8960 KB Output is correct
21 Correct 11 ms 9088 KB Output is correct
22 Correct 11 ms 9088 KB Output is correct
23 Correct 11 ms 9088 KB Output is correct
24 Correct 10 ms 8960 KB Output is correct
25 Correct 14 ms 9088 KB Output is correct
26 Correct 10 ms 8960 KB Output is correct
27 Correct 10 ms 8960 KB Output is correct
28 Correct 10 ms 9088 KB Output is correct
29 Correct 10 ms 9088 KB Output is correct
30 Correct 10 ms 9088 KB Output is correct
31 Correct 11 ms 9088 KB Output is correct
32 Correct 10 ms 9088 KB Output is correct
33 Correct 11 ms 9088 KB Output is correct
34 Correct 10 ms 8960 KB Output is correct
35 Correct 10 ms 8960 KB Output is correct
36 Correct 10 ms 9088 KB Output is correct
37 Correct 10 ms 9088 KB Output is correct
38 Correct 10 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8960 KB Output is correct
2 Correct 9 ms 8960 KB Output is correct
3 Correct 9 ms 8960 KB Output is correct
4 Correct 9 ms 8960 KB Output is correct
5 Correct 9 ms 8960 KB Output is correct
6 Correct 9 ms 8960 KB Output is correct
7 Correct 10 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 9088 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 9 ms 8960 KB Output is correct
14 Correct 9 ms 8960 KB Output is correct
15 Correct 9 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 9 ms 8960 KB Output is correct
19 Correct 193 ms 15224 KB Output is correct
20 Correct 180 ms 15224 KB Output is correct
21 Correct 193 ms 15352 KB Output is correct
22 Correct 171 ms 15480 KB Output is correct
23 Correct 130 ms 15480 KB Output is correct
24 Correct 86 ms 15480 KB Output is correct
25 Correct 159 ms 17016 KB Output is correct
26 Correct 121 ms 21112 KB Output is correct
27 Correct 230 ms 24312 KB Output is correct
28 Correct 401 ms 32508 KB Output is correct
29 Correct 414 ms 31992 KB Output is correct
30 Correct 226 ms 24312 KB Output is correct
31 Correct 232 ms 24440 KB Output is correct
32 Correct 293 ms 24324 KB Output is correct
33 Correct 383 ms 23144 KB Output is correct
34 Correct 337 ms 24068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8960 KB Output is correct
2 Correct 9 ms 8960 KB Output is correct
3 Correct 9 ms 8960 KB Output is correct
4 Correct 9 ms 8960 KB Output is correct
5 Correct 9 ms 8960 KB Output is correct
6 Correct 9 ms 8960 KB Output is correct
7 Correct 10 ms 8960 KB Output is correct
8 Correct 10 ms 8960 KB Output is correct
9 Correct 10 ms 8960 KB Output is correct
10 Correct 10 ms 8960 KB Output is correct
11 Correct 10 ms 9088 KB Output is correct
12 Correct 10 ms 8960 KB Output is correct
13 Correct 9 ms 8960 KB Output is correct
14 Correct 9 ms 8960 KB Output is correct
15 Correct 9 ms 8960 KB Output is correct
16 Correct 10 ms 8960 KB Output is correct
17 Correct 10 ms 8960 KB Output is correct
18 Correct 9 ms 8960 KB Output is correct
19 Correct 9 ms 8960 KB Output is correct
20 Correct 9 ms 8960 KB Output is correct
21 Correct 11 ms 9088 KB Output is correct
22 Correct 11 ms 9088 KB Output is correct
23 Correct 11 ms 9088 KB Output is correct
24 Correct 10 ms 8960 KB Output is correct
25 Correct 14 ms 9088 KB Output is correct
26 Correct 10 ms 8960 KB Output is correct
27 Correct 10 ms 8960 KB Output is correct
28 Correct 10 ms 9088 KB Output is correct
29 Correct 10 ms 9088 KB Output is correct
30 Correct 10 ms 9088 KB Output is correct
31 Correct 11 ms 9088 KB Output is correct
32 Correct 10 ms 9088 KB Output is correct
33 Correct 11 ms 9088 KB Output is correct
34 Correct 10 ms 8960 KB Output is correct
35 Correct 10 ms 8960 KB Output is correct
36 Correct 10 ms 9088 KB Output is correct
37 Correct 10 ms 9088 KB Output is correct
38 Correct 10 ms 8960 KB Output is correct
39 Correct 193 ms 15224 KB Output is correct
40 Correct 180 ms 15224 KB Output is correct
41 Correct 193 ms 15352 KB Output is correct
42 Correct 171 ms 15480 KB Output is correct
43 Correct 130 ms 15480 KB Output is correct
44 Correct 86 ms 15480 KB Output is correct
45 Correct 159 ms 17016 KB Output is correct
46 Correct 121 ms 21112 KB Output is correct
47 Correct 230 ms 24312 KB Output is correct
48 Correct 401 ms 32508 KB Output is correct
49 Correct 414 ms 31992 KB Output is correct
50 Correct 226 ms 24312 KB Output is correct
51 Correct 232 ms 24440 KB Output is correct
52 Correct 293 ms 24324 KB Output is correct
53 Correct 383 ms 23144 KB Output is correct
54 Correct 337 ms 24068 KB Output is correct
55 Correct 20 ms 9728 KB Output is correct
56 Correct 20 ms 9728 KB Output is correct
57 Correct 118 ms 17272 KB Output is correct
58 Correct 51 ms 16792 KB Output is correct
59 Correct 137 ms 21712 KB Output is correct
60 Correct 555 ms 34408 KB Output is correct
61 Correct 239 ms 24440 KB Output is correct
62 Correct 250 ms 24312 KB Output is correct
63 Correct 351 ms 24440 KB Output is correct
64 Correct 600 ms 26472 KB Output is correct
65 Correct 365 ms 25336 KB Output is correct
66 Correct 517 ms 30792 KB Output is correct
67 Correct 139 ms 25832 KB Output is correct
68 Correct 327 ms 26224 KB Output is correct
69 Correct 340 ms 26380 KB Output is correct
70 Correct 310 ms 25584 KB Output is correct