답안 #551187

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
551187 2022-04-20T04:53:45 Z ymm 경주 (Race) (IOI11_race) C++17
100 / 100
862 ms 60764 KB
///
///   There's a reason for your defeat, DIO. One simple reason...
///   You pissed me off.
///

#include <bits/stdc++.h>
#define Loop(x,l,r) for(ll x = ll(l); x < ll(r); ++x)
#define LoopR(x,l,r) for(ll x = ll(r)-1; x >= ll(l); --x)
#define Kill(x) exit((cout << (x) << '\n', 0))
typedef long long ll;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
using namespace std;

#ifndef DARD
#include "race.h"
#endif

const int N = 200'010;
vector<pll> A[N];
bool block[N];
int cnt[N];
int n, k;

int dfs_cnt_rem(int v, int p)
{
	cnt[v] = 1;
	Loop (i,0,A[v].size()) {
		if (block[A[v][i].first]) {
			swap(A[v][i], A[v].back());
			A[v].pop_back();
			--i;
		} else if (A[v][i].first != p) {
			cnt[v] += dfs_cnt_rem(A[v][i].first, v);
		}
	}
	return cnt[v];
}

int dfs_cent(int v, int p, int cnt)
{
	for (auto [u, _] : A[v]) {
		if (u == p)
			continue;
		if (::cnt[u] > cnt/2)
			return dfs_cent(u, v, cnt);
	}
	return v;
}

void dfs_len(int v, int p, pll cur, vector<pll>& vec)
{
	vec.push_back(cur);
	for (auto [u, w] : A[v]) {
		if (u == p)
			continue;
		dfs_len(u, v, {cur.first+w, cur.second+1}, vec);
	}
}

int solve(int v)
{
	dfs_cnt_rem(v, -1);
	v = dfs_cent(v, -1, cnt[v]);
	map<ll, int> mp; mp[0] = 0;
	vector<pll> vec;
	int ans = N;
	for (auto [u, w] : A[v]) {
		vec.clear();
		dfs_len(u, v, {w, 1}, vec);
		for (auto [x, y] : vec) {
			auto it = mp.find(k-x);
			if (it != mp.end())
				ans = min(ans, (int)(y + it->second));
		}
		for (auto [x, y] : vec) {
			int &z = mp[x];
			if (!z || z > y)
				z = y;
		}
	}
	block[v] = 1;
	for (auto [u, _] : A[v])
		ans = min(ans, solve(u));
	return ans;
}

int best_path(int n, int k, int h[][2], int l[])
{
	::n = n; ::k = k;
	Loop (i,0,n-1) {
		A[h[i][0]].push_back({h[i][1], l[i]});
		A[h[i][1]].push_back({h[i][0], l[i]});
	}
	int ans = solve(0);
	return ans == N? -1: ans;
}

#ifdef DARD
int main()
{
//	int constexpr n = 4;
//	int constexpr k = 3;
//	int h[n-1][2] = {{0, 1}, {1, 2}, {1, 3}};
//	int l[n-1] = {1, 2, 4};
	int constexpr n = 3;
	int constexpr k = 3;
	int h[n-1][2] = {{0, 1}, {1, 2}};
	int l[n-1] = {1, 1};
//	int constexpr n = 11;
//	int constexpr k = 12;
//	int h[n-1][2] = {{0, 1}, {0, 2}, {2, 3}, {3, 4}, {4, 5}, {0, 6}, {6, 7}, {6, 8}, {8, 9}, {8, 10}};
//	int l[n-1] = {3, 4, 5, 4, 6, 3, 2, 5, 6, 7};
	cout << best_path(n, k, h, l) << '\n';
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4996 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 4 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4996 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 4 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5000 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 5 ms 5204 KB Output is correct
23 Correct 5 ms 5204 KB Output is correct
24 Correct 5 ms 5148 KB Output is correct
25 Correct 5 ms 5204 KB Output is correct
26 Correct 5 ms 5204 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 4 ms 5144 KB Output is correct
30 Correct 4 ms 5144 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 5 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 4 ms 5204 KB Output is correct
38 Correct 4 ms 5204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4996 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 4 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 242 ms 14592 KB Output is correct
20 Correct 225 ms 14812 KB Output is correct
21 Correct 197 ms 14792 KB Output is correct
22 Correct 225 ms 14832 KB Output is correct
23 Correct 220 ms 14976 KB Output is correct
24 Correct 114 ms 13452 KB Output is correct
25 Correct 151 ms 18496 KB Output is correct
26 Correct 105 ms 20708 KB Output is correct
27 Correct 296 ms 21712 KB Output is correct
28 Correct 862 ms 60764 KB Output is correct
29 Correct 841 ms 59912 KB Output is correct
30 Correct 310 ms 21640 KB Output is correct
31 Correct 265 ms 21648 KB Output is correct
32 Correct 352 ms 21652 KB Output is correct
33 Correct 471 ms 24044 KB Output is correct
34 Correct 681 ms 45156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5004 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 3 ms 5008 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4996 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 4 ms 5000 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 5000 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 5 ms 5204 KB Output is correct
23 Correct 5 ms 5204 KB Output is correct
24 Correct 5 ms 5148 KB Output is correct
25 Correct 5 ms 5204 KB Output is correct
26 Correct 5 ms 5204 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
29 Correct 4 ms 5144 KB Output is correct
30 Correct 4 ms 5144 KB Output is correct
31 Correct 4 ms 5204 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 5 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 4 ms 5204 KB Output is correct
37 Correct 4 ms 5204 KB Output is correct
38 Correct 4 ms 5204 KB Output is correct
39 Correct 242 ms 14592 KB Output is correct
40 Correct 225 ms 14812 KB Output is correct
41 Correct 197 ms 14792 KB Output is correct
42 Correct 225 ms 14832 KB Output is correct
43 Correct 220 ms 14976 KB Output is correct
44 Correct 114 ms 13452 KB Output is correct
45 Correct 151 ms 18496 KB Output is correct
46 Correct 105 ms 20708 KB Output is correct
47 Correct 296 ms 21712 KB Output is correct
48 Correct 862 ms 60764 KB Output is correct
49 Correct 841 ms 59912 KB Output is correct
50 Correct 310 ms 21640 KB Output is correct
51 Correct 265 ms 21648 KB Output is correct
52 Correct 352 ms 21652 KB Output is correct
53 Correct 471 ms 24044 KB Output is correct
54 Correct 681 ms 45156 KB Output is correct
55 Correct 20 ms 6700 KB Output is correct
56 Correct 15 ms 6048 KB Output is correct
57 Correct 123 ms 14736 KB Output is correct
58 Correct 47 ms 12340 KB Output is correct
59 Correct 262 ms 30152 KB Output is correct
60 Correct 804 ms 58456 KB Output is correct
61 Correct 315 ms 23336 KB Output is correct
62 Correct 274 ms 21716 KB Output is correct
63 Correct 340 ms 21640 KB Output is correct
64 Correct 811 ms 38732 KB Output is correct
65 Correct 782 ms 45184 KB Output is correct
66 Correct 823 ms 57824 KB Output is correct
67 Correct 124 ms 20000 KB Output is correct
68 Correct 446 ms 38084 KB Output is correct
69 Correct 448 ms 38412 KB Output is correct
70 Correct 379 ms 36708 KB Output is correct