답안 #836871

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
836871 2023-08-24T16:32:05 Z Elias 경주 (Race) (IOI11_race) C++17
100 / 100
614 ms 31404 KB
#ifndef _DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#endif

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()

vector<vector<pair<int, int>>> adj;
vector<int> blocked;

int best = INT32_MAX;
int k;

struct Tree
{
	int n = 0;

	int centroid = -1;

	void spread(int i, int p = -1)
	{
		n++;
		for (auto [c, D] : adj[i])
		{
			if (c != p && !blocked[c])
			{
				spread(c, i);
			}
		}
	}

	int find_centroid(int i, int p = -1)
	{
		int subtree_size = 1;
		int largest_subtree = 0;

		for (auto [c, D] : adj[i])
		{
			if (c != p && !blocked[c])
			{
				int sub = find_centroid(c, i);
				subtree_size += sub;
				largest_subtree = max(largest_subtree, sub);
			}
		}

		if (largest_subtree <= n / 2 && n - subtree_size <= n / 2)
			centroid = i;

		return subtree_size;
	}

	void dfs(int i, vector<pair<int, int>> &out, int d = 0, int depth = 0, int p = -1)
	{
		out.push_back({d, depth});

		for (auto [c, D] : adj[i])
		{
			if (c != p && !blocked[c])
			{
				dfs(c, out, d + D, depth + 1, i);
			}
		}
	}
	vector<int> split()
	{
		if (centroid == -1)
			return {};
		blocked[centroid] = true;
		vector<int> out;
		for (auto [c, d] : adj[centroid])
		{
			if (!blocked[c])
				out.push_back(c);
		}
		return out;
	}

	Tree(int start)
	{
		spread(start);

		if (n > 1)
		{
			find_centroid(start);

			assert(centroid != -1);

			multiset<pair<int, int>> all;
			all.insert({0, 0});

			for (auto [c, d] : adj[centroid])
			{
				if (blocked[c])
				continue;
				vector<pair<int, int>> blub;
				dfs(c, blub, d, 1, centroid);

				for (auto &[a, b] : blub)
				{
					int other = k - a;

					auto ptr = all.lower_bound({other, 0});
					if (ptr != all.end() && ptr->first == other)
					{
						int new_one = b + ptr->second;
						best = min(best, new_one);
					}
				}

				for (auto x : blub)
					all.insert(x);
			}
		}
	}
};

#ifndef _DEBUG
#include "race.h"
#else
int best_path(int N, int K, int H[][2], int L[]);
#endif

int best_path(int N, int K, int H[][2], int L[])
{
	adj.resize(N);
	blocked.resize(N);
	k = K;

	for (int i = 0; i < N - 1; i++)
	{
		adj[H[i][1]].push_back(pair<int, int>{H[i][0], L[i]});
		adj[H[i][0]].push_back(pair<int, int>{H[i][1], L[i]});
	}

	vector<int> todo = {0};

	while (todo.size() && todo.size() != N - 1)
	{
		int i = todo.back();
		todo.pop_back();

		Tree tree(i);

		if (tree.n > 1)
			for (int x : tree.split())
				todo.push_back(x);
	}

	if (best == INT32_MAX)
		return -1;

	return best;
}

#ifdef _DEBUG

#define MAX_N 500000

static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;

inline void my_assert(int e)
{
	if (!e)
		abort();
}

void read_input()
{
	int i;
	my_assert(2 == scanf("%d %d", &N, &K));
	for (i = 0; i < N - 1; i++)
		my_assert(3 == scanf("%d %d %d", &H[i][0], &H[i][1], &L[i]));
	my_assert(1 == scanf("%d", &solution));
}

signed main()
{
	freopen("/home/elias/Downloads/ioi2011tests/race-test/subtask4/grader.in.13", "r", stdin);

	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int ans;
	read_input();
	ans = best_path(N, K, H, L);
	if (ans == solution)
		printf("Correct.\n");
	else
		printf("Incorrect. Returned %d, Expected %d.\n", ans, solution);

	return 0;
}

#endif

Compilation message

race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:143:36: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  143 |  while (todo.size() && todo.size() != N - 1)
      |                        ~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 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 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 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 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 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 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 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 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 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 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 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 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 265 ms 12688 KB Output is correct
20 Correct 247 ms 12920 KB Output is correct
21 Correct 243 ms 12856 KB Output is correct
22 Correct 211 ms 12660 KB Output is correct
23 Correct 257 ms 12980 KB Output is correct
24 Correct 134 ms 12720 KB Output is correct
25 Correct 253 ms 14148 KB Output is correct
26 Correct 190 ms 15820 KB Output is correct
27 Correct 259 ms 24932 KB Output is correct
28 Correct 556 ms 31404 KB Output is correct
29 Correct 566 ms 30896 KB Output is correct
30 Correct 254 ms 24908 KB Output is correct
31 Correct 254 ms 24860 KB Output is correct
32 Correct 304 ms 24904 KB Output is correct
33 Correct 431 ms 24488 KB Output is correct
34 Correct 506 ms 24100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 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 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 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 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 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 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 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 2 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 265 ms 12688 KB Output is correct
40 Correct 247 ms 12920 KB Output is correct
41 Correct 243 ms 12856 KB Output is correct
42 Correct 211 ms 12660 KB Output is correct
43 Correct 257 ms 12980 KB Output is correct
44 Correct 134 ms 12720 KB Output is correct
45 Correct 253 ms 14148 KB Output is correct
46 Correct 190 ms 15820 KB Output is correct
47 Correct 259 ms 24932 KB Output is correct
48 Correct 556 ms 31404 KB Output is correct
49 Correct 566 ms 30896 KB Output is correct
50 Correct 254 ms 24908 KB Output is correct
51 Correct 254 ms 24860 KB Output is correct
52 Correct 304 ms 24904 KB Output is correct
53 Correct 431 ms 24488 KB Output is correct
54 Correct 506 ms 24100 KB Output is correct
55 Correct 15 ms 1492 KB Output is correct
56 Correct 16 ms 1576 KB Output is correct
57 Correct 216 ms 12984 KB Output is correct
58 Correct 57 ms 12824 KB Output is correct
59 Correct 202 ms 15812 KB Output is correct
60 Correct 567 ms 31112 KB Output is correct
61 Correct 280 ms 24936 KB Output is correct
62 Correct 252 ms 24816 KB Output is correct
63 Correct 305 ms 25036 KB Output is correct
64 Correct 614 ms 25184 KB Output is correct
65 Correct 596 ms 25020 KB Output is correct
66 Correct 545 ms 29744 KB Output is correct
67 Correct 156 ms 25248 KB Output is correct
68 Correct 310 ms 28448 KB Output is correct
69 Correct 338 ms 28740 KB Output is correct
70 Correct 299 ms 27292 KB Output is correct