답안 #331140

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
331140 2020-11-27T14:05:40 Z prarie 경주 (Race) (IOI11_race) C++14
100 / 100
636 ms 34412 KB
#include "race.h"
 
#include <bits/stdc++.h>
 
#define fastio ios_base::sync_with_stdio(0), cin.tie(0)
#define fi first
#define se second
#define endl '\n'
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compress(v) sort(all(v)), (v).erase(unique(all((v))), v.end())
 
using namespace std;
 
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
 
const int N = 2e5 + 5;
const int K = 1e6 + 5;
 
int n, k;
vector<pii> adj[N];
bool fin[N];
int sz[N];
int d[K];
vector<int> cl;
 
int get_cent(int t_size, int cur, int pv = -1) {
	for (auto &next : adj[cur]) {
		if (pv == next.first || fin[next.first]) continue;
		if (sz[next.first] > t_size / 2) 
			return get_cent(t_size, next.first, cur);
	}
	return cur;
}
 
void get_sz(int cur, int pv = -1) {
	sz[cur] = 1;
	for (auto &next : adj[cur]) {
		if (next.first == pv || fin[next.first]) continue;
		get_sz(next.first, cur);
		sz[cur] += sz[next.first];
	}
}
 
void assign(int cur, int w, int pv, int cnt) {
	if (w > k) return;
	if (d[w] > cnt) d[w] = cnt, cl.push_back(w);
	for (auto &next : adj[cur]) {
		if (next.first == pv || fin[next.first]) continue;
		assign(next.first, next.second + w, cur, cnt + 1);
	}
}
 
int get(int cur, int w, int pv, int cnt) {
	int ret = N;
	if (w > k) return ret;
	ret = min(ret, cnt + d[k - w]);
	for (auto &next : adj[cur]) {
		if (next.first == pv || fin[next.first]) continue;
		ret = min(get(next.first, w + next.second, cur, cnt + 1), ret);
	}
	return ret;
}
 
int calc(int node) {
	int ret = N;
	// Find Centroid
	get_sz(node);
	int cent = get_cent(sz[node], node);
	//cout << node << ' ' << cent << endl;
	if (cent == -1) {
		fin[node] = true;
		return ret;
	}
 
	fin[cent] = true;
 
	// Find Minimum nodes of length K
	d[0] = 0;
	for (auto &next : adj[cent]) {
		if (fin[next.first]) continue;
		ret = min(ret, get(next.first, next.second, -1, 1));
		assign(next.first, next.second, -1, 1);
	}
	for (auto &x : cl) d[x] = 0x3f3f3f3f;
	cl.clear();
 
	// call calc for subtrees
	for (auto &next : adj[cent]) {
		if (fin[next.first]) continue;
		ret = min(ret, calc(next.first));
	}
	return ret;
}
 
int best_path(int N, int K, int H[][2], int L[])
{
	n = N, k = K;
	for (int i = 0; i < N - 1; i++) {
		int u = H[i][0], v = H[i][1], w = L[i];
		adj[u].emplace_back(v, w);
		adj[v].emplace_back(u, w);
	}
	memset(d, 0x3f, sizeof d);
	int ans = calc(0);
	return (ans != ::N ? ans : -1);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8940 KB Output is correct
2 Correct 6 ms 8904 KB Output is correct
3 Correct 5 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 5 ms 8940 KB Output is correct
8 Correct 5 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 5 ms 8940 KB Output is correct
11 Correct 7 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 5 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 5 ms 8940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8940 KB Output is correct
2 Correct 6 ms 8904 KB Output is correct
3 Correct 5 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 5 ms 8940 KB Output is correct
8 Correct 5 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 5 ms 8940 KB Output is correct
11 Correct 7 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 5 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 5 ms 8940 KB Output is correct
19 Correct 5 ms 8940 KB Output is correct
20 Correct 6 ms 8940 KB Output is correct
21 Correct 7 ms 9068 KB Output is correct
22 Correct 8 ms 9068 KB Output is correct
23 Correct 6 ms 9068 KB Output is correct
24 Correct 6 ms 9196 KB Output is correct
25 Correct 6 ms 9068 KB Output is correct
26 Correct 7 ms 9068 KB Output is correct
27 Correct 6 ms 9068 KB Output is correct
28 Correct 6 ms 9068 KB Output is correct
29 Correct 7 ms 9068 KB Output is correct
30 Correct 6 ms 9196 KB Output is correct
31 Correct 6 ms 9068 KB Output is correct
32 Correct 8 ms 9068 KB Output is correct
33 Correct 7 ms 9068 KB Output is correct
34 Correct 6 ms 9068 KB Output is correct
35 Correct 7 ms 9068 KB Output is correct
36 Correct 6 ms 9068 KB Output is correct
37 Correct 8 ms 9068 KB Output is correct
38 Correct 7 ms 9068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8940 KB Output is correct
2 Correct 6 ms 8904 KB Output is correct
3 Correct 5 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 5 ms 8940 KB Output is correct
8 Correct 5 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 5 ms 8940 KB Output is correct
11 Correct 7 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 5 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 5 ms 8940 KB Output is correct
19 Correct 147 ms 14444 KB Output is correct
20 Correct 156 ms 15596 KB Output is correct
21 Correct 170 ms 15596 KB Output is correct
22 Correct 159 ms 15824 KB Output is correct
23 Correct 98 ms 16072 KB Output is correct
24 Correct 68 ms 15984 KB Output is correct
25 Correct 150 ms 18412 KB Output is correct
26 Correct 98 ms 21356 KB Output is correct
27 Correct 210 ms 22784 KB Output is correct
28 Correct 313 ms 34412 KB Output is correct
29 Correct 335 ms 33260 KB Output is correct
30 Correct 208 ms 22764 KB Output is correct
31 Correct 214 ms 22764 KB Output is correct
32 Correct 255 ms 23020 KB Output is correct
33 Correct 261 ms 21612 KB Output is correct
34 Correct 235 ms 22636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 8940 KB Output is correct
2 Correct 6 ms 8904 KB Output is correct
3 Correct 5 ms 8940 KB Output is correct
4 Correct 6 ms 8940 KB Output is correct
5 Correct 6 ms 8940 KB Output is correct
6 Correct 6 ms 8940 KB Output is correct
7 Correct 5 ms 8940 KB Output is correct
8 Correct 5 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 5 ms 8940 KB Output is correct
11 Correct 7 ms 8940 KB Output is correct
12 Correct 7 ms 8940 KB Output is correct
13 Correct 6 ms 8940 KB Output is correct
14 Correct 6 ms 8940 KB Output is correct
15 Correct 6 ms 8940 KB Output is correct
16 Correct 5 ms 8940 KB Output is correct
17 Correct 5 ms 8940 KB Output is correct
18 Correct 5 ms 8940 KB Output is correct
19 Correct 5 ms 8940 KB Output is correct
20 Correct 6 ms 8940 KB Output is correct
21 Correct 7 ms 9068 KB Output is correct
22 Correct 8 ms 9068 KB Output is correct
23 Correct 6 ms 9068 KB Output is correct
24 Correct 6 ms 9196 KB Output is correct
25 Correct 6 ms 9068 KB Output is correct
26 Correct 7 ms 9068 KB Output is correct
27 Correct 6 ms 9068 KB Output is correct
28 Correct 6 ms 9068 KB Output is correct
29 Correct 7 ms 9068 KB Output is correct
30 Correct 6 ms 9196 KB Output is correct
31 Correct 6 ms 9068 KB Output is correct
32 Correct 8 ms 9068 KB Output is correct
33 Correct 7 ms 9068 KB Output is correct
34 Correct 6 ms 9068 KB Output is correct
35 Correct 7 ms 9068 KB Output is correct
36 Correct 6 ms 9068 KB Output is correct
37 Correct 8 ms 9068 KB Output is correct
38 Correct 7 ms 9068 KB Output is correct
39 Correct 147 ms 14444 KB Output is correct
40 Correct 156 ms 15596 KB Output is correct
41 Correct 170 ms 15596 KB Output is correct
42 Correct 159 ms 15824 KB Output is correct
43 Correct 98 ms 16072 KB Output is correct
44 Correct 68 ms 15984 KB Output is correct
45 Correct 150 ms 18412 KB Output is correct
46 Correct 98 ms 21356 KB Output is correct
47 Correct 210 ms 22784 KB Output is correct
48 Correct 313 ms 34412 KB Output is correct
49 Correct 335 ms 33260 KB Output is correct
50 Correct 208 ms 22764 KB Output is correct
51 Correct 214 ms 22764 KB Output is correct
52 Correct 255 ms 23020 KB Output is correct
53 Correct 261 ms 21612 KB Output is correct
54 Correct 235 ms 22636 KB Output is correct
55 Correct 14 ms 9708 KB Output is correct
56 Correct 15 ms 9580 KB Output is correct
57 Correct 85 ms 15852 KB Output is correct
58 Correct 44 ms 15584 KB Output is correct
59 Correct 108 ms 21872 KB Output is correct
60 Correct 576 ms 34268 KB Output is correct
61 Correct 218 ms 23020 KB Output is correct
62 Correct 240 ms 22960 KB Output is correct
63 Correct 305 ms 22892 KB Output is correct
64 Correct 636 ms 24036 KB Output is correct
65 Correct 276 ms 23660 KB Output is correct
66 Correct 474 ms 31340 KB Output is correct
67 Correct 150 ms 23516 KB Output is correct
68 Correct 357 ms 24168 KB Output is correct
69 Correct 320 ms 24172 KB Output is correct
70 Correct 280 ms 23400 KB Output is correct