Submission #383350

# Submission time Handle Problem Language Result Execution time Memory
383350 2021-03-29T17:53:39 Z Aryan_Raina Race (IOI11_race) C++14
100 / 100
590 ms 40172 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define ar array
 
const int INF = 1e9;
const int MOD = 1e9 + 7;

const int MXN = 2e5+9, MXK = 1e6+9;
vector<ar<int,2>> g[MXN];
int length[MXK], lastcnt[MXK], sz[MXN]; 
bool dead[MXK];
int n, k, ans = INT_MAX, cnt = 0;
int OneCentroid(int rt) {
	function<void(int,int)> get_sz = [&](int u, int pu) {
		sz[u] = 1;
		for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) {
			get_sz(v[1], u);
			sz[u] += sz[v[1]];
		}
	};
	get_sz(rt, rt);
	function<int(int,int)> cent = [&](int u, int pu) {
		for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) {
			if (sz[v[1]] > sz[rt]/2) return cent(v[1], u);
		}
		return u;
	};
	
	return cent(rt, rt);
}

void dfs(int u, int pu, int len, int dist, bool update) {
	if (k < dist || len > ans) return;
	if (!update && lastcnt[k-dist] == cnt) 
		ans = min(ans, length[k-dist] + len);
	if (update && (lastcnt[dist] < cnt || len < length[dist])) 
		length[dist] = len, lastcnt[dist] = cnt; 
	for (auto v : g[u]) if (v[1] != pu && !dead[v[1]]) 
		dfs(v[1], u, len + 1, dist + v[0], update);
}

void decompose(int start) {
	int cen = OneCentroid(start);
	length[0] = 0; lastcnt[0] = ++cnt;
	// get length, distance to each node in subtree of centroid from centroid
	// ans = min(ans, length) if distance corresponding to it is k
	for (auto v : g[cen]) if (!dead[v[1]]) {
		dfs(v[1], cen, 1, v[0], false);
		dfs(v[1], cen, 1, v[0], true);
	}
	dead[cen] = true;
	for (auto v : g[cen]) if (!dead[v[1]]) decompose(v[1]);
	dead[cen] = false;
}
 
int best_path(int N, int K, int H[][2], int L[]) {
	n = N, k = K;
	for (int i = 0; i < N-1; i++) {
		g[H[i][0]].push_back({L[i], H[i][1]});
		g[H[i][1]].push_back({L[i], H[i][0]});
	}
  	decompose(0);
	return (ans < INF/2) ? ans : -1;
}
 
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5112 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5112 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 6 ms 5228 KB Output is correct
22 Correct 8 ms 10092 KB Output is correct
23 Correct 8 ms 9324 KB Output is correct
24 Correct 9 ms 10860 KB Output is correct
25 Correct 8 ms 8556 KB Output is correct
26 Correct 7 ms 7788 KB Output is correct
27 Correct 5 ms 5100 KB Output is correct
28 Correct 6 ms 6636 KB Output is correct
29 Correct 7 ms 7660 KB Output is correct
30 Correct 7 ms 7916 KB Output is correct
31 Correct 8 ms 8684 KB Output is correct
32 Correct 8 ms 8812 KB Output is correct
33 Correct 9 ms 10476 KB Output is correct
34 Correct 8 ms 9580 KB Output is correct
35 Correct 9 ms 10988 KB Output is correct
36 Correct 9 ms 11500 KB Output is correct
37 Correct 8 ms 8940 KB Output is correct
38 Correct 7 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5112 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 138 ms 10348 KB Output is correct
20 Correct 134 ms 10476 KB Output is correct
21 Correct 144 ms 10476 KB Output is correct
22 Correct 135 ms 10604 KB Output is correct
23 Correct 87 ms 10604 KB Output is correct
24 Correct 66 ms 10476 KB Output is correct
25 Correct 142 ms 14956 KB Output is correct
26 Correct 108 ms 19180 KB Output is correct
27 Correct 198 ms 15980 KB Output is correct
28 Correct 332 ms 36588 KB Output is correct
29 Correct 340 ms 35180 KB Output is correct
30 Correct 200 ms 18932 KB Output is correct
31 Correct 200 ms 18884 KB Output is correct
32 Correct 245 ms 19052 KB Output is correct
33 Correct 267 ms 17864 KB Output is correct
34 Correct 227 ms 18668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 4 ms 5100 KB Output is correct
5 Correct 4 ms 5100 KB Output is correct
6 Correct 4 ms 5100 KB Output is correct
7 Correct 4 ms 5120 KB Output is correct
8 Correct 5 ms 5112 KB Output is correct
9 Correct 4 ms 5100 KB Output is correct
10 Correct 4 ms 5100 KB Output is correct
11 Correct 4 ms 5100 KB Output is correct
12 Correct 4 ms 5100 KB Output is correct
13 Correct 4 ms 5100 KB Output is correct
14 Correct 4 ms 5100 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 5100 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5100 KB Output is correct
19 Correct 4 ms 5100 KB Output is correct
20 Correct 4 ms 5100 KB Output is correct
21 Correct 6 ms 5228 KB Output is correct
22 Correct 8 ms 10092 KB Output is correct
23 Correct 8 ms 9324 KB Output is correct
24 Correct 9 ms 10860 KB Output is correct
25 Correct 8 ms 8556 KB Output is correct
26 Correct 7 ms 7788 KB Output is correct
27 Correct 5 ms 5100 KB Output is correct
28 Correct 6 ms 6636 KB Output is correct
29 Correct 7 ms 7660 KB Output is correct
30 Correct 7 ms 7916 KB Output is correct
31 Correct 8 ms 8684 KB Output is correct
32 Correct 8 ms 8812 KB Output is correct
33 Correct 9 ms 10476 KB Output is correct
34 Correct 8 ms 9580 KB Output is correct
35 Correct 9 ms 10988 KB Output is correct
36 Correct 9 ms 11500 KB Output is correct
37 Correct 8 ms 8940 KB Output is correct
38 Correct 7 ms 7404 KB Output is correct
39 Correct 138 ms 10348 KB Output is correct
40 Correct 134 ms 10476 KB Output is correct
41 Correct 144 ms 10476 KB Output is correct
42 Correct 135 ms 10604 KB Output is correct
43 Correct 87 ms 10604 KB Output is correct
44 Correct 66 ms 10476 KB Output is correct
45 Correct 142 ms 14956 KB Output is correct
46 Correct 108 ms 19180 KB Output is correct
47 Correct 198 ms 15980 KB Output is correct
48 Correct 332 ms 36588 KB Output is correct
49 Correct 340 ms 35180 KB Output is correct
50 Correct 200 ms 18932 KB Output is correct
51 Correct 200 ms 18884 KB Output is correct
52 Correct 245 ms 19052 KB Output is correct
53 Correct 267 ms 17864 KB Output is correct
54 Correct 227 ms 18668 KB Output is correct
55 Correct 12 ms 5740 KB Output is correct
56 Correct 13 ms 5740 KB Output is correct
57 Correct 92 ms 12012 KB Output is correct
58 Correct 44 ms 11744 KB Output is correct
59 Correct 114 ms 21356 KB Output is correct
60 Correct 538 ms 40172 KB Output is correct
61 Correct 213 ms 19180 KB Output is correct
62 Correct 237 ms 19052 KB Output is correct
63 Correct 311 ms 19052 KB Output is correct
64 Correct 590 ms 21484 KB Output is correct
65 Correct 292 ms 19692 KB Output is correct
66 Correct 488 ms 40092 KB Output is correct
67 Correct 134 ms 19548 KB Output is correct
68 Correct 281 ms 24428 KB Output is correct
69 Correct 250 ms 22892 KB Output is correct
70 Correct 243 ms 24300 KB Output is correct