Submission #114450

# Submission time Handle Problem Language Result Execution time Memory
114450 2019-06-01T11:46:53 Z mosesmayer Race (IOI11_race) C++17
100 / 100
414 ms 60620 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include "race.h"
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef vector<int> vi;
typedef pair<int,int> pii;

const int MXN = 1e6;
const int mxsz = 2e5 + 3;
int n, k;
vector<pii> adj[mxsz];

int sz[mxsz], dep[mxsz], bc[mxsz], in[mxsz], out[mxsz], flat[mxsz];
long long dist[mxsz];
gp_hash_table<long long, int> sack;

void getsz(int u, int prv = -1){
	static int tme = 0;
	in[u] = ++tme; flat[tme] = u;
	sz[u] = 1;
	for (int i = 0, nx, w; i < adj[u].size(); i++){
		nx = adj[u][i].fi; w = adj[u][i].se;
		if (nx == prv) continue;
		dep[nx] = dep[u] + 1;
		dist[nx] = dist[u] + w;
		getsz(nx, u);
		sz[u] += sz[nx];
		if (bc[u] == -1 || sz[nx] > sz[bc[u]]) bc[u] = nx;
	}
	out[u] = tme;
}

int ans = 0x3f3f3f3f;

void dfs(int u, int prv, bool keep){
	for (int i = 0, nx; i < adj[u].size(); i++){
		nx = adj[u][i].fi;
		if (nx == prv || nx == bc[u]) continue;
		dfs(nx, u, 0);
	}
	if (bc[u] != -1) dfs(bc[u], u, 1);
//	cout << " begin calc " << u << ' ' << dist[u] << '\n';
	sack[dist[u]] = u;
	if (bc[u] != -1){
//		cout << "try big:\n";
		long long w = k + dist[u];
		if (sack.find(w) != sack.end()){
//			cout << "found " <<sack[w] << '\n';
			ans = min(ans, dep[sack[w]] - dep[u]);
		}
	}
	for (int i = 0, nx, w; i < adj[u].size(); i++){
		nx = adj[u][i].fi;
		if (nx == prv || nx == bc[u]) continue;
		for (int j = in[nx]; j <= out[nx]; j++){
			int x = flat[j];
			long long w = dist[x] - dist[u] - dist[u];
			w = 1LL * k - w;
//			cout << "from " << x << " dist " << w << '\n';
			if (sack.find(w) != sack.end()){
				int y = sack[w];
//				cout << "found " << y << '\n';
				ans = min(ans, dep[x] + dep[y] - 2 * dep[u]);
			}
		}
		for (int j = in[nx]; j <= out[nx]; j++){
			int x = flat[j];
			long long w = dist[x];
//			cout << " insert " << x << ' ' << w << '\n';
			if (sack.find(w) == sack.end()
					|| dep[x] < dep[sack[w]]) sack[w] = x;
		}
	}
//	for (int i = 0; i <= k; i++) cout << sack[i] << " \n"[i==k];
//	for (int i = in[u]; i <= out[u]; i++){
//		int x = flat[i];
//		long long w = dist[x] - dist[u] - dist[u];
//		w = 1LL * k - w;
//		cout << "from " << x << " dist " << w << '\n';
//		if (sack.find(w) != sack.end()){
//			int y = sack[w];
//			cout << "found " << y << '\n';
//			ans = min(ans, dep[x] + dep[y] - 2 * dep[u]);
//		}
//	}
	if (!keep){
		sack.clear();
//		cout << "clear\n";
	}
//	cout << "end calc " << u << '\n' << '\n';
}

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]].emplace_back(H[i][1], L[i]);
		adj[H[i][1]].emplace_back(H[i][0], L[i]);
	}
	memset(bc, -1, sizeof(bc));
//	for (int i = 0; i < n; i++){
//		cout << i << " : ";
//		for (pii p : adj[i]) cout << '{' << p.fi << ", " << p.se << "} ";
//		cout << '\n';
//	}
	getsz(0);
//	for (int i = 0; i < n; i++){
//		cout << dep[i] << ' ' << dist[i] << ",\n"[i==n-1];
//	}
	dfs(0, -1, 1);
	if (ans == 0x3f3f3f3f) return -1;
	return ans;
}

Compilation message

race.cpp: In function 'void getsz(int, int)':
race.cpp:24:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0, nx, w; i < adj[u].size(); i++){
                         ~~^~~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, bool)':
race.cpp:39:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0, nx; i < adj[u].size(); i++){
                      ~~^~~~~~~~~~~~~~~
race.cpp:55:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0, nx, w; i < adj[u].size(); i++){
                         ~~^~~~~~~~~~~~~~~
race.cpp:55:22: warning: unused variable 'w' [-Wunused-variable]
  for (int i = 0, nx, w; i < adj[u].size(); i++){
                      ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5888 KB Output is correct
2 Correct 6 ms 5888 KB Output is correct
3 Correct 6 ms 5888 KB Output is correct
4 Correct 6 ms 5888 KB Output is correct
5 Correct 6 ms 5888 KB Output is correct
6 Correct 6 ms 5888 KB Output is correct
7 Correct 6 ms 5888 KB Output is correct
8 Correct 6 ms 5888 KB Output is correct
9 Correct 6 ms 5888 KB Output is correct
10 Correct 6 ms 5888 KB Output is correct
11 Correct 6 ms 5888 KB Output is correct
12 Correct 6 ms 5888 KB Output is correct
13 Correct 6 ms 5888 KB Output is correct
14 Correct 6 ms 5888 KB Output is correct
15 Correct 6 ms 5888 KB Output is correct
16 Correct 6 ms 5888 KB Output is correct
17 Correct 6 ms 5888 KB Output is correct
18 Correct 6 ms 5888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5888 KB Output is correct
2 Correct 6 ms 5888 KB Output is correct
3 Correct 6 ms 5888 KB Output is correct
4 Correct 6 ms 5888 KB Output is correct
5 Correct 6 ms 5888 KB Output is correct
6 Correct 6 ms 5888 KB Output is correct
7 Correct 6 ms 5888 KB Output is correct
8 Correct 6 ms 5888 KB Output is correct
9 Correct 6 ms 5888 KB Output is correct
10 Correct 6 ms 5888 KB Output is correct
11 Correct 6 ms 5888 KB Output is correct
12 Correct 6 ms 5888 KB Output is correct
13 Correct 6 ms 5888 KB Output is correct
14 Correct 6 ms 5888 KB Output is correct
15 Correct 6 ms 5888 KB Output is correct
16 Correct 6 ms 5888 KB Output is correct
17 Correct 6 ms 5888 KB Output is correct
18 Correct 6 ms 5888 KB Output is correct
19 Correct 6 ms 5888 KB Output is correct
20 Correct 7 ms 5888 KB Output is correct
21 Correct 8 ms 6016 KB Output is correct
22 Correct 8 ms 6016 KB Output is correct
23 Correct 8 ms 6016 KB Output is correct
24 Correct 9 ms 6016 KB Output is correct
25 Correct 8 ms 6016 KB Output is correct
26 Correct 8 ms 6016 KB Output is correct
27 Correct 7 ms 5888 KB Output is correct
28 Correct 8 ms 6016 KB Output is correct
29 Correct 8 ms 6016 KB Output is correct
30 Correct 8 ms 6016 KB Output is correct
31 Correct 8 ms 6016 KB Output is correct
32 Correct 8 ms 6016 KB Output is correct
33 Correct 8 ms 6016 KB Output is correct
34 Correct 8 ms 5988 KB Output is correct
35 Correct 8 ms 6016 KB Output is correct
36 Correct 8 ms 6016 KB Output is correct
37 Correct 7 ms 5980 KB Output is correct
38 Correct 8 ms 6016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5888 KB Output is correct
2 Correct 6 ms 5888 KB Output is correct
3 Correct 6 ms 5888 KB Output is correct
4 Correct 6 ms 5888 KB Output is correct
5 Correct 6 ms 5888 KB Output is correct
6 Correct 6 ms 5888 KB Output is correct
7 Correct 6 ms 5888 KB Output is correct
8 Correct 6 ms 5888 KB Output is correct
9 Correct 6 ms 5888 KB Output is correct
10 Correct 6 ms 5888 KB Output is correct
11 Correct 6 ms 5888 KB Output is correct
12 Correct 6 ms 5888 KB Output is correct
13 Correct 6 ms 5888 KB Output is correct
14 Correct 6 ms 5888 KB Output is correct
15 Correct 6 ms 5888 KB Output is correct
16 Correct 6 ms 5888 KB Output is correct
17 Correct 6 ms 5888 KB Output is correct
18 Correct 6 ms 5888 KB Output is correct
19 Correct 124 ms 13532 KB Output is correct
20 Correct 125 ms 14896 KB Output is correct
21 Correct 115 ms 14920 KB Output is correct
22 Correct 126 ms 14968 KB Output is correct
23 Correct 148 ms 15352 KB Output is correct
24 Correct 126 ms 15112 KB Output is correct
25 Correct 90 ms 19924 KB Output is correct
26 Correct 56 ms 23672 KB Output is correct
27 Correct 201 ms 24568 KB Output is correct
28 Correct 275 ms 60408 KB Output is correct
29 Correct 250 ms 59984 KB Output is correct
30 Correct 217 ms 24668 KB Output is correct
31 Correct 205 ms 24484 KB Output is correct
32 Correct 301 ms 24580 KB Output is correct
33 Correct 214 ms 23564 KB Output is correct
34 Correct 338 ms 42632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5888 KB Output is correct
2 Correct 6 ms 5888 KB Output is correct
3 Correct 6 ms 5888 KB Output is correct
4 Correct 6 ms 5888 KB Output is correct
5 Correct 6 ms 5888 KB Output is correct
6 Correct 6 ms 5888 KB Output is correct
7 Correct 6 ms 5888 KB Output is correct
8 Correct 6 ms 5888 KB Output is correct
9 Correct 6 ms 5888 KB Output is correct
10 Correct 6 ms 5888 KB Output is correct
11 Correct 6 ms 5888 KB Output is correct
12 Correct 6 ms 5888 KB Output is correct
13 Correct 6 ms 5888 KB Output is correct
14 Correct 6 ms 5888 KB Output is correct
15 Correct 6 ms 5888 KB Output is correct
16 Correct 6 ms 5888 KB Output is correct
17 Correct 6 ms 5888 KB Output is correct
18 Correct 6 ms 5888 KB Output is correct
19 Correct 6 ms 5888 KB Output is correct
20 Correct 7 ms 5888 KB Output is correct
21 Correct 8 ms 6016 KB Output is correct
22 Correct 8 ms 6016 KB Output is correct
23 Correct 8 ms 6016 KB Output is correct
24 Correct 9 ms 6016 KB Output is correct
25 Correct 8 ms 6016 KB Output is correct
26 Correct 8 ms 6016 KB Output is correct
27 Correct 7 ms 5888 KB Output is correct
28 Correct 8 ms 6016 KB Output is correct
29 Correct 8 ms 6016 KB Output is correct
30 Correct 8 ms 6016 KB Output is correct
31 Correct 8 ms 6016 KB Output is correct
32 Correct 8 ms 6016 KB Output is correct
33 Correct 8 ms 6016 KB Output is correct
34 Correct 8 ms 5988 KB Output is correct
35 Correct 8 ms 6016 KB Output is correct
36 Correct 8 ms 6016 KB Output is correct
37 Correct 7 ms 5980 KB Output is correct
38 Correct 8 ms 6016 KB Output is correct
39 Correct 124 ms 13532 KB Output is correct
40 Correct 125 ms 14896 KB Output is correct
41 Correct 115 ms 14920 KB Output is correct
42 Correct 126 ms 14968 KB Output is correct
43 Correct 148 ms 15352 KB Output is correct
44 Correct 126 ms 15112 KB Output is correct
45 Correct 90 ms 19924 KB Output is correct
46 Correct 56 ms 23672 KB Output is correct
47 Correct 201 ms 24568 KB Output is correct
48 Correct 275 ms 60408 KB Output is correct
49 Correct 250 ms 59984 KB Output is correct
50 Correct 217 ms 24668 KB Output is correct
51 Correct 205 ms 24484 KB Output is correct
52 Correct 301 ms 24580 KB Output is correct
53 Correct 214 ms 23564 KB Output is correct
54 Correct 338 ms 42632 KB Output is correct
55 Correct 17 ms 7500 KB Output is correct
56 Correct 14 ms 6784 KB Output is correct
57 Correct 79 ms 15020 KB Output is correct
58 Correct 47 ms 14700 KB Output is correct
59 Correct 73 ms 32972 KB Output is correct
60 Correct 282 ms 60372 KB Output is correct
61 Correct 222 ms 26760 KB Output is correct
62 Correct 207 ms 24680 KB Output is correct
63 Correct 300 ms 24568 KB Output is correct
64 Correct 407 ms 33888 KB Output is correct
65 Correct 414 ms 43580 KB Output is correct
66 Correct 302 ms 60620 KB Output is correct
67 Correct 239 ms 25060 KB Output is correct
68 Correct 332 ms 43616 KB Output is correct
69 Correct 306 ms 43744 KB Output is correct
70 Correct 314 ms 42920 KB Output is correct