답안 #815688

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
815688 2023-08-08T18:40:38 Z PanosPask 경주 (Race) (IOI11_race) C++14
100 / 100
247 ms 71328 KB
#include "race.h"
#include "vector"
#include "unordered_map"

#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;

const int INF = 1e9;

int N, K;
int shift;
int moves_shift;
int ans = INF;
vector<vector<pi>> adj_list;
vector<int> sz;
unordered_map<int, int> minv;
vector<int> dep;
vector<int> dist;

// Euler tour variables
vector<int> tin;
vector<int> tout;
vector<int> trav;
int counter = 0;

void calculate(int node, int par, bool is_heavy)
{
	int BigKid = -1;
	int BigW = 0;
	int BigV = 0;

	for (auto [neigh, w] : adj_list[node]) {
		if (neigh != par && sz[neigh] > BigV) {
			BigKid = neigh;
			BigW = w;
			BigV = sz[neigh];
		}
	}

	for (auto [neigh, w] : adj_list[node]) {
		if (neigh != par && neigh != BigKid)
			calculate(neigh, node, false);
	}
	if (BigKid != -1) {
		calculate(BigKid, node, true);
		shift -= BigW;
		moves_shift++;
	}

	if (minv.find(K + shift) != minv.end())
		ans = min(ans, minv[K + shift] + moves_shift);
	minv[shift] = -moves_shift;
	for (auto [neigh, w] : adj_list[node]) {
		if (neigh == par || neigh == BigKid)
			continue;

		for (int t = tin[neigh]; t < tout[neigh]; t++) {
			int cur = trav[t];
			int len = dist[cur] - dist[node];
			int compliment = K - len;
			int v = dep[cur] - dep[node];

			if (minv.find(compliment + shift) != minv.end())
				ans = min(ans, minv[compliment + shift] + moves_shift + v);
		}
		for (int t = tin[neigh]; t < tout[neigh]; t++) {
			int cur = trav[t];
			int len = dist[cur] - dist[node];
			int v = dep[cur] - dep[node];

			if (minv.find(len + shift) != minv.end())
				minv[len + shift] = min(minv[len + shift], v - moves_shift);
			else
				minv[len + shift] = v - moves_shift;
		}
	}

	if (!is_heavy) {
		for (int t = tin[node]; t < tout[node]; t++) {
			int cur = trav[t];
			int len = dist[cur] - dist[node];

			minv.erase(len + shift);
		}
		shift = K;
		moves_shift = 0;
	}
}

void init(int node, int par)
{
	tin[node] = counter++;
	trav[tin[node]] = node;

	sz[node] = 1;
	for (auto [neigh, w] : adj_list[node]) {
		if (neigh == par)
			continue;

		dep[neigh] = dep[node] + 1;
		dist[neigh] = dist[node] + w;

		init(neigh, node);

		sz[node] += sz[neigh];
	}

	tout[node] = counter;
}

int best_path(int n, int k, int H[][2], int L[])
{
	N = n;
	K = k;

	adj_list.resize(N);
	sz.resize(N);
	dep.resize(N);
	dist.resize(N);

	shift = K;

	tin.resize(N);
	tout.resize(N);
	trav.resize(N);

	for (int i = 0; i < N - 1; i++) {
		int u = H[i][0];
		int v = H[i][1];
		int w = L[i];

		adj_list[u].pb(mp(v, w));
		adj_list[v].pb(mp(u, w));
	}

	init(0, -1);
	calculate(0, -1, true);

	if (ans >= N)
		return -1;
	else
		return ans;
}

Compilation message

race.cpp: In function 'void calculate(int, int, bool)':
race.cpp:37:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   37 |  for (auto [neigh, w] : adj_list[node]) {
      |            ^
race.cpp:45:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   45 |  for (auto [neigh, w] : adj_list[node]) {
      |            ^
race.cpp:58:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |  for (auto [neigh, w] : adj_list[node]) {
      |            ^
race.cpp: In function 'void init(int, int)':
race.cpp:101:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  101 |  for (auto [neigh, w] : adj_list[node]) {
      |            ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 436 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 444 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 440 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 516 KB Output is correct
37 Correct 1 ms 432 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 64 ms 11176 KB Output is correct
20 Correct 75 ms 11312 KB Output is correct
21 Correct 59 ms 11220 KB Output is correct
22 Correct 62 ms 11212 KB Output is correct
23 Correct 76 ms 11560 KB Output is correct
24 Correct 57 ms 11428 KB Output is correct
25 Correct 48 ms 21612 KB Output is correct
26 Correct 42 ms 30964 KB Output is correct
27 Correct 101 ms 22592 KB Output is correct
28 Correct 147 ms 71328 KB Output is correct
29 Correct 144 ms 68432 KB Output is correct
30 Correct 123 ms 22648 KB Output is correct
31 Correct 100 ms 22596 KB Output is correct
32 Correct 146 ms 22856 KB Output is correct
33 Correct 131 ms 21792 KB Output is correct
34 Correct 181 ms 32076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 436 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 2 ms 444 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 440 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 516 KB Output is correct
37 Correct 1 ms 432 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 64 ms 11176 KB Output is correct
40 Correct 75 ms 11312 KB Output is correct
41 Correct 59 ms 11220 KB Output is correct
42 Correct 62 ms 11212 KB Output is correct
43 Correct 76 ms 11560 KB Output is correct
44 Correct 57 ms 11428 KB Output is correct
45 Correct 48 ms 21612 KB Output is correct
46 Correct 42 ms 30964 KB Output is correct
47 Correct 101 ms 22592 KB Output is correct
48 Correct 147 ms 71328 KB Output is correct
49 Correct 144 ms 68432 KB Output is correct
50 Correct 123 ms 22648 KB Output is correct
51 Correct 100 ms 22596 KB Output is correct
52 Correct 146 ms 22856 KB Output is correct
53 Correct 131 ms 21792 KB Output is correct
54 Correct 181 ms 32076 KB Output is correct
55 Correct 7 ms 1620 KB Output is correct
56 Correct 5 ms 1364 KB Output is correct
57 Correct 51 ms 11336 KB Output is correct
58 Correct 30 ms 11152 KB Output is correct
59 Correct 50 ms 35636 KB Output is correct
60 Correct 143 ms 69372 KB Output is correct
61 Correct 117 ms 23976 KB Output is correct
62 Correct 127 ms 22860 KB Output is correct
63 Correct 129 ms 22792 KB Output is correct
64 Correct 201 ms 27548 KB Output is correct
65 Correct 247 ms 32812 KB Output is correct
66 Correct 168 ms 61820 KB Output is correct
67 Correct 83 ms 23224 KB Output is correct
68 Correct 150 ms 29904 KB Output is correct
69 Correct 156 ms 30200 KB Output is correct
70 Correct 164 ms 28748 KB Output is correct