Submission #210546

# Submission time Handle Problem Language Result Execution time Memory
210546 2020-03-17T16:44:03 Z faremy Race (IOI11_race) C++14
100 / 100
813 ms 33272 KB
#include "race.h"

#include <vector>
#include <algorithm>


struct Edge
{
	Edge(int v, int w) : dest(v), weight(w) {}
	int dest, weight;
};

const int MAXN = 2e5;
const int MAXK = 1e6 + 1;

std::vector<Edge> tree[MAXN];
bool seen[MAXN];
int treeSize[MAXN];
int minRoad[MAXK];


int calcsize(int node)
{
	if (seen[node])
		return 0;
	seen[node] = true;
	treeSize[node] = 1;

	for (Edge &edge : tree[node])
		treeSize[node] += calcsize(edge.dest);
	seen[node] = false;
	return treeSize[node];
}

int findcentr(int node, int tsize)
{
	seen[node] = true;
	int maxSub = tsize - treeSize[node];	

	for (Edge &edge : tree[node])
		if (!seen[edge.dest])
		{
			int centr = findcentr(edge.dest, tsize);
			if (centr != -1)
			{
				seen[node] = false;
				return centr;
			}

			maxSub = std::max(maxSub, treeSize[edge.dest]);
		}

	seen[node] = false;
	if (maxSub > tsize / 2)
		return -1;
	return node;
}

int count(int node, int taken, int dist, int wanted)
{
	if (seen[node] || dist > wanted)
		return MAXN;
	seen[node] = true;
	int length = taken + minRoad[wanted - dist];

	for (Edge &edge : tree[node])
		length = std::min(length, count(edge.dest, taken + 1, dist + edge.weight, wanted));
	seen[node] = false;
	return length;
}

void mark(int node, int taken, int dist, int wanted)
{
	if (seen[node] || dist > wanted)
		return;
	seen[node] = true;
	minRoad[dist] = std::min(minRoad[dist], taken);

	for (Edge &edge : tree[node])
		mark(edge.dest, taken + 1, dist + edge.weight, wanted);
	seen[node] = false;
}

void unmark(int node, int taken, int dist, int wanted)
{
	if (seen[node] || dist > wanted)
		return;
	seen[node] = true;
	minRoad[dist] = MAXN;

	for (Edge &edge : tree[node])
		unmark(edge.dest, taken + 1, dist + edge.weight, wanted);
	seen[node] = false;
}

int findpath(int node, int wanted)
{
	if (seen[node])
		return MAXN;
	int s = calcsize(node);
	int centroid = findcentr(node, s);
	seen[centroid] = true;

	int length = MAXN;
	minRoad[0] = 0;

	for (Edge &edge : tree[centroid])
	{
		length = std::min(length, count(edge.dest, 1, edge.weight, wanted));
		mark(edge.dest, 1, edge.weight, wanted);
	}

	for (Edge &edge : tree[centroid])
		unmark(edge.dest, 1, edge.weight, wanted);
	for (Edge &edge : tree[centroid])
		length = std::min(length, findpath(edge.dest, wanted));
	return length;
}

int best_path(int N, int K, int H[][2], int L[])
{
	for (int iEdge = 0; iEdge < N - 1; iEdge++)
	{
		tree[H[iEdge][0]].emplace_back(H[iEdge][1], L[iEdge]);
		tree[H[iEdge][1]].emplace_back(H[iEdge][0], L[iEdge]);
	}

	std::fill_n(minRoad, K + 1, MAXN);
	int length = findpath(0, K);

	if (length == MAXN)
		return -1;
	return length;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 8 ms 5112 KB Output is correct
13 Correct 7 ms 4984 KB Output is correct
14 Correct 8 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 7 ms 4984 KB Output is correct
18 Correct 7 ms 4984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 8 ms 5112 KB Output is correct
13 Correct 7 ms 4984 KB Output is correct
14 Correct 8 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 7 ms 4984 KB Output is correct
18 Correct 7 ms 4984 KB Output is correct
19 Correct 7 ms 4984 KB Output is correct
20 Correct 8 ms 4984 KB Output is correct
21 Correct 9 ms 5116 KB Output is correct
22 Correct 11 ms 8696 KB Output is correct
23 Correct 10 ms 8056 KB Output is correct
24 Correct 10 ms 8440 KB Output is correct
25 Correct 11 ms 8312 KB Output is correct
26 Correct 9 ms 6392 KB Output is correct
27 Correct 10 ms 8184 KB Output is correct
28 Correct 9 ms 5880 KB Output is correct
29 Correct 9 ms 6392 KB Output is correct
30 Correct 9 ms 6520 KB Output is correct
31 Correct 10 ms 7672 KB Output is correct
32 Correct 10 ms 7800 KB Output is correct
33 Correct 11 ms 8184 KB Output is correct
34 Correct 9 ms 7416 KB Output is correct
35 Correct 10 ms 8312 KB Output is correct
36 Correct 11 ms 8696 KB Output is correct
37 Correct 10 ms 8184 KB Output is correct
38 Correct 10 ms 7032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 8 ms 5112 KB Output is correct
13 Correct 7 ms 4984 KB Output is correct
14 Correct 8 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 7 ms 4984 KB Output is correct
18 Correct 7 ms 4984 KB Output is correct
19 Correct 208 ms 11740 KB Output is correct
20 Correct 227 ms 11744 KB Output is correct
21 Correct 213 ms 11640 KB Output is correct
22 Correct 197 ms 11768 KB Output is correct
23 Correct 117 ms 12024 KB Output is correct
24 Correct 80 ms 11896 KB Output is correct
25 Correct 173 ms 14456 KB Output is correct
26 Correct 133 ms 17400 KB Output is correct
27 Correct 232 ms 18808 KB Output is correct
28 Correct 389 ms 30200 KB Output is correct
29 Correct 404 ms 29304 KB Output is correct
30 Correct 229 ms 18776 KB Output is correct
31 Correct 235 ms 18812 KB Output is correct
32 Correct 303 ms 18936 KB Output is correct
33 Correct 356 ms 17656 KB Output is correct
34 Correct 293 ms 18552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 4984 KB Output is correct
3 Correct 7 ms 4984 KB Output is correct
4 Correct 8 ms 5112 KB Output is correct
5 Correct 8 ms 5112 KB Output is correct
6 Correct 8 ms 5112 KB Output is correct
7 Correct 7 ms 4984 KB Output is correct
8 Correct 8 ms 4984 KB Output is correct
9 Correct 8 ms 4984 KB Output is correct
10 Correct 8 ms 4984 KB Output is correct
11 Correct 7 ms 4984 KB Output is correct
12 Correct 8 ms 5112 KB Output is correct
13 Correct 7 ms 4984 KB Output is correct
14 Correct 8 ms 4984 KB Output is correct
15 Correct 8 ms 5112 KB Output is correct
16 Correct 7 ms 4984 KB Output is correct
17 Correct 7 ms 4984 KB Output is correct
18 Correct 7 ms 4984 KB Output is correct
19 Correct 7 ms 4984 KB Output is correct
20 Correct 8 ms 4984 KB Output is correct
21 Correct 9 ms 5116 KB Output is correct
22 Correct 11 ms 8696 KB Output is correct
23 Correct 10 ms 8056 KB Output is correct
24 Correct 10 ms 8440 KB Output is correct
25 Correct 11 ms 8312 KB Output is correct
26 Correct 9 ms 6392 KB Output is correct
27 Correct 10 ms 8184 KB Output is correct
28 Correct 9 ms 5880 KB Output is correct
29 Correct 9 ms 6392 KB Output is correct
30 Correct 9 ms 6520 KB Output is correct
31 Correct 10 ms 7672 KB Output is correct
32 Correct 10 ms 7800 KB Output is correct
33 Correct 11 ms 8184 KB Output is correct
34 Correct 9 ms 7416 KB Output is correct
35 Correct 10 ms 8312 KB Output is correct
36 Correct 11 ms 8696 KB Output is correct
37 Correct 10 ms 8184 KB Output is correct
38 Correct 10 ms 7032 KB Output is correct
39 Correct 208 ms 11740 KB Output is correct
40 Correct 227 ms 11744 KB Output is correct
41 Correct 213 ms 11640 KB Output is correct
42 Correct 197 ms 11768 KB Output is correct
43 Correct 117 ms 12024 KB Output is correct
44 Correct 80 ms 11896 KB Output is correct
45 Correct 173 ms 14456 KB Output is correct
46 Correct 133 ms 17400 KB Output is correct
47 Correct 232 ms 18808 KB Output is correct
48 Correct 389 ms 30200 KB Output is correct
49 Correct 404 ms 29304 KB Output is correct
50 Correct 229 ms 18776 KB Output is correct
51 Correct 235 ms 18812 KB Output is correct
52 Correct 303 ms 18936 KB Output is correct
53 Correct 356 ms 17656 KB Output is correct
54 Correct 293 ms 18552 KB Output is correct
55 Correct 18 ms 5752 KB Output is correct
56 Correct 21 ms 5624 KB Output is correct
57 Correct 120 ms 11896 KB Output is correct
58 Correct 48 ms 11628 KB Output is correct
59 Correct 133 ms 18168 KB Output is correct
60 Correct 730 ms 33272 KB Output is correct
61 Correct 273 ms 19008 KB Output is correct
62 Correct 287 ms 22776 KB Output is correct
63 Correct 415 ms 22776 KB Output is correct
64 Correct 813 ms 21112 KB Output is correct
65 Correct 361 ms 19704 KB Output is correct
66 Correct 571 ms 31224 KB Output is correct
67 Correct 144 ms 23396 KB Output is correct
68 Correct 407 ms 23544 KB Output is correct
69 Correct 409 ms 23672 KB Output is correct
70 Correct 363 ms 22904 KB Output is correct