Submission #384077

# Submission time Handle Problem Language Result Execution time Memory
384077 2021-03-31T11:23:23 Z Drew_ Race (IOI11_race) C++14
100 / 100
1506 ms 60504 KB
#include "race.h"
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cassert>
using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ii pair<int, int>
#define f1 first
#define s2 second

const int MAX = 2e5 + 7;
const int inf = 1e9 + 7;

int n, k;
int sz[MAX];
set<ii> adj[MAX];
static int ans;

int get_sz(int node, int par)
{
	sz[node] = 1;
	for (ii to : adj[node])
		if (to.f1 != par)
			sz[node] += get_sz(to.f1, node);
	return sz[node];
}

int centroid(int node, int par, int n)
{
	for (ii to : adj[node])
		if (to.f1 != par && sz[to.f1] > n/2)
			return centroid(to.f1, node, n);
	return node;
}

//{distance, length, id
vector<pair<int, ii>> vec;
void dfs(int node, int par, int id, int dist, int h, bool zero)
{
	if (dist > k)
		return;

	if (!zero) //prevent duplicate data
		vec.pb({dist, {h, id}});

	for (ii to : adj[node])
	{
		if (to.f1 != par)
			dfs(to.f1, node, id, dist + to.s2, h+1, to.s2 == 0);
	}
}

void rc(int node)
{
	int n = get_sz(node, -1);
	int c = centroid(node, -1, n);
	vector<ii> edge(adj[c].begin(), adj[c].end());

	vec.clear();
	vec.pb({0, {0, 0}});

	int id = 1;
	for (ii e : edge)
		dfs(e.f1, c, id++, e.s2, 1, false);

	sort(vec.begin(), vec.end());

	{
		vector<pair<int, ii>> nv;
		nv.pb(vec[0]);

		set<ii> inserted; //distance, id
		for (int i = 1; i < vec.size(); ++i)
		{
			if (!inserted.count({vec[i].f1, vec[i].s2.s2}))
			{
				inserted.insert({vec[i].f1, vec[i].s2.s2});
				nv.pb(vec[i]);
			}
		}
		vec = nv;
	}

	for (pair<int, ii> x : vec)
	{
		auto it = lower_bound(vec.begin(), vec.end(), mp(k - x.f1, mp(-1, -1)));
		if (it == vec.end())
			continue;

		if ((*it).s2.s2 == x.s2.s2)
			it = next(it);

		if (it == vec.end())
			continue;

		if ((*it).f1 != k - x.f1)
			continue;

		assert((*it).s2.s2 != x.s2.s2);
		ans = min(ans, (*it).s2.f1 + x.s2.f1);
	}


	for (ii e : edge)
	{
		adj[c].erase(e);
		adj[e.f1].erase({c, e.s2});
		rc(e.f1);
	}
}

int best_path(int N, int K, int H[][2], int L[])
{
	n = N, k = K;
	for (int i = 0; i < N-1; ++i)
	{
		adj[H[i][0]].insert({H[i][1], L[i]});
		adj[H[i][1]].insert({H[i][0], L[i]});
	}

	ans = n;

	rc(0);
	return ans == n ? -1 : ans;
}

Compilation message

race.cpp: In function 'void rc(int)':
race.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for (int i = 1; i < vec.size(); ++i)
      |                   ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9708 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 8 ms 9708 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 8 ms 9708 KB Output is correct
14 Correct 9 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 8 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9708 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 8 ms 9708 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 8 ms 9708 KB Output is correct
14 Correct 9 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 8 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 8 ms 9708 KB Output is correct
21 Correct 10 ms 9964 KB Output is correct
22 Correct 8 ms 9836 KB Output is correct
23 Correct 8 ms 9836 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 10 ms 9964 KB Output is correct
26 Correct 10 ms 9964 KB Output is correct
27 Correct 8 ms 9836 KB Output is correct
28 Correct 9 ms 9856 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 9 ms 9964 KB Output is correct
31 Correct 9 ms 9964 KB Output is correct
32 Correct 9 ms 9964 KB Output is correct
33 Correct 10 ms 9964 KB Output is correct
34 Correct 9 ms 9836 KB Output is correct
35 Correct 9 ms 9836 KB Output is correct
36 Correct 9 ms 9836 KB Output is correct
37 Correct 9 ms 9964 KB Output is correct
38 Correct 9 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9708 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 8 ms 9708 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 8 ms 9708 KB Output is correct
14 Correct 9 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 8 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 352 ms 20972 KB Output is correct
20 Correct 329 ms 21612 KB Output is correct
21 Correct 359 ms 22400 KB Output is correct
22 Correct 398 ms 23232 KB Output is correct
23 Correct 178 ms 21484 KB Output is correct
24 Correct 149 ms 21656 KB Output is correct
25 Correct 246 ms 24812 KB Output is correct
26 Correct 138 ms 27756 KB Output is correct
27 Correct 425 ms 32876 KB Output is correct
28 Correct 512 ms 45164 KB Output is correct
29 Correct 511 ms 44268 KB Output is correct
30 Correct 441 ms 32984 KB Output is correct
31 Correct 452 ms 32956 KB Output is correct
32 Correct 499 ms 32620 KB Output is correct
33 Correct 573 ms 32748 KB Output is correct
34 Correct 350 ms 32748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 9708 KB Output is correct
2 Correct 8 ms 9728 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
11 Correct 8 ms 9708 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 8 ms 9708 KB Output is correct
14 Correct 9 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 8 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 8 ms 9708 KB Output is correct
21 Correct 10 ms 9964 KB Output is correct
22 Correct 8 ms 9836 KB Output is correct
23 Correct 8 ms 9836 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 10 ms 9964 KB Output is correct
26 Correct 10 ms 9964 KB Output is correct
27 Correct 8 ms 9836 KB Output is correct
28 Correct 9 ms 9856 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 9 ms 9964 KB Output is correct
31 Correct 9 ms 9964 KB Output is correct
32 Correct 9 ms 9964 KB Output is correct
33 Correct 10 ms 9964 KB Output is correct
34 Correct 9 ms 9836 KB Output is correct
35 Correct 9 ms 9836 KB Output is correct
36 Correct 9 ms 9836 KB Output is correct
37 Correct 9 ms 9964 KB Output is correct
38 Correct 9 ms 9964 KB Output is correct
39 Correct 352 ms 20972 KB Output is correct
40 Correct 329 ms 21612 KB Output is correct
41 Correct 359 ms 22400 KB Output is correct
42 Correct 398 ms 23232 KB Output is correct
43 Correct 178 ms 21484 KB Output is correct
44 Correct 149 ms 21656 KB Output is correct
45 Correct 246 ms 24812 KB Output is correct
46 Correct 138 ms 27756 KB Output is correct
47 Correct 425 ms 32876 KB Output is correct
48 Correct 512 ms 45164 KB Output is correct
49 Correct 511 ms 44268 KB Output is correct
50 Correct 441 ms 32984 KB Output is correct
51 Correct 452 ms 32956 KB Output is correct
52 Correct 499 ms 32620 KB Output is correct
53 Correct 573 ms 32748 KB Output is correct
54 Correct 350 ms 32748 KB Output is correct
55 Correct 31 ms 11372 KB Output is correct
56 Correct 30 ms 11244 KB Output is correct
57 Correct 253 ms 23396 KB Output is correct
58 Correct 162 ms 29536 KB Output is correct
59 Correct 479 ms 35040 KB Output is correct
60 Correct 1506 ms 60504 KB Output is correct
61 Correct 516 ms 37044 KB Output is correct
62 Correct 598 ms 42844 KB Output is correct
63 Correct 716 ms 42940 KB Output is correct
64 Correct 1285 ms 46808 KB Output is correct
65 Correct 446 ms 35628 KB Output is correct
66 Correct 947 ms 44756 KB Output is correct
67 Correct 516 ms 50620 KB Output is correct
68 Correct 703 ms 41184 KB Output is correct
69 Correct 711 ms 41312 KB Output is correct
70 Correct 664 ms 39776 KB Output is correct