답안 #836009

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
836009 2023-08-24T04:50:19 Z Halym2007 경주 (Race) (IOI11_race) C++11
100 / 100
375 ms 47152 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define N 200005
const int INF = 1e9 + 7;
vector <int> nodes;
int dpt[N], types[N], mx = 1e9, rem[N], sub[N];
ll baha[N];
vector <pair <int, int>> v[N], G[N];
int p[1000005];
void dfs (int x, int pr) {
	sub[x] = 1;
	for (auto i : v[x]) {
		if (i.ff == pr or rem[i.ff]) {
			continue;
		}
		dfs (i.ff, x);
		sub[x] += sub[i.ff];
	}
}
int centroid (int x, int pr, int san) {
	for (auto i : v[x]) {
		if (i.ff == pr or rem[i.ff]) continue;
		if (sub[i.ff] > san / 2) {
			return centroid (i.ff, x, san);
		}
	}
	return x;
}

void solve (int x, int pr,  ll val, int type, int edge) {
//	cout << x << " " << val << " " << type << " " << edge << endl;
	baha[x] = val;
	types[x] = type;
	dpt[x] = edge;
	nodes.pb (x);
	for (auto i : v[x]) {
		if (i.ff == pr or rem[i.ff]) continue;
		solve (i.ff, x, val + i.ss, type, edge + 1);
	}
}



void build (int x, int pr, int k) {
	int centr = centroid (x, -1, sub[x]); // centr tapdyk
	rem[centr] = 1;
	int at = 0;
	nodes.clear();
	for (auto i : v[centr]) {
		if (rem[i.ff]) continue;
		solve (i.ff, 0, i.ss, ++at, 1);
	}
	for (int i : nodes) {
		G[types[i]].pb ({baha[i], dpt[i]});
	}
	for (int i = 1; i <= at; ++i) {
		for (auto j : G[i]) {
			if (j.ff <= k) {
				mx = min (mx, p[k - j.ff] + j.ss);
			}
		}
		for (auto j : G[i]) {
			if (j.ff <= k) {
				p[j.ff] = min (p[j.ff], j.ss);
			}
		}
		G[i].clear();
	}
	for (int i : nodes) if (baha[i] <= k) p[baha[i]] = INF;
	for (auto i : v[centr]) {
		if (rem[i.ff]) {
		continue;
		}
		dfs (i.ff, -1);
		build (i.ff, centr, k);
	}
}


int best_path(int n, int k, int h[][2], int l[]) {
	for (int i = 1; i <= k; ++i) p[i] = INF;
	for (int i = 0; i < n - 1; ++i) {
		int u = h[i][0], vv = h[i][1], w = l[i];
		v[u].pb ({vv, w});
		v[vv].pb ({u, w});
	}
	dfs (0, -1);
	build (0, -1, k);
	if (mx == 1e9) mx = -1;
	return mx;
}


//#define MAX_N 500000
//int n, k;
//int h[MAX_N][2];
//int l[MAX_N];
//
//int main() {
//	freopen ("input.txt", "r", stdin);
//  cin >> n >> k;
//  for(int i = 0; i < n-1; i++)
//    cin >> h[i][0] >> h[i][1] >> l[i];
//  int ans;
//  ans = best_path(n,k,h,l);
//  cout << ans << "\n";
//  return 0;
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9720 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9708 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9712 KB Output is correct
8 Correct 4 ms 9712 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9708 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9720 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9708 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9712 KB Output is correct
8 Correct 4 ms 9712 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9708 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9712 KB Output is correct
19 Correct 4 ms 9684 KB Output is correct
20 Correct 4 ms 9684 KB Output is correct
21 Correct 5 ms 9788 KB Output is correct
22 Correct 6 ms 13396 KB Output is correct
23 Correct 9 ms 12892 KB Output is correct
24 Correct 6 ms 13140 KB Output is correct
25 Correct 6 ms 13068 KB Output is correct
26 Correct 5 ms 11120 KB Output is correct
27 Correct 6 ms 13032 KB Output is correct
28 Correct 5 ms 10620 KB Output is correct
29 Correct 5 ms 11000 KB Output is correct
30 Correct 5 ms 11256 KB Output is correct
31 Correct 6 ms 12372 KB Output is correct
32 Correct 7 ms 12540 KB Output is correct
33 Correct 6 ms 12884 KB Output is correct
34 Correct 6 ms 12116 KB Output is correct
35 Correct 6 ms 12916 KB Output is correct
36 Correct 7 ms 13436 KB Output is correct
37 Correct 7 ms 12892 KB Output is correct
38 Correct 5 ms 11732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9720 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9708 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9712 KB Output is correct
8 Correct 4 ms 9712 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9708 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9712 KB Output is correct
19 Correct 102 ms 19664 KB Output is correct
20 Correct 100 ms 19956 KB Output is correct
21 Correct 99 ms 19656 KB Output is correct
22 Correct 89 ms 19876 KB Output is correct
23 Correct 83 ms 19948 KB Output is correct
24 Correct 52 ms 19904 KB Output is correct
25 Correct 110 ms 23084 KB Output is correct
26 Correct 78 ms 26836 KB Output is correct
27 Correct 131 ms 30116 KB Output is correct
28 Correct 287 ms 44128 KB Output is correct
29 Correct 375 ms 43048 KB Output is correct
30 Correct 149 ms 30096 KB Output is correct
31 Correct 136 ms 30060 KB Output is correct
32 Correct 198 ms 30160 KB Output is correct
33 Correct 237 ms 29364 KB Output is correct
34 Correct 209 ms 29736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9720 KB Output is correct
2 Correct 4 ms 9684 KB Output is correct
3 Correct 4 ms 9684 KB Output is correct
4 Correct 4 ms 9708 KB Output is correct
5 Correct 4 ms 9684 KB Output is correct
6 Correct 4 ms 9684 KB Output is correct
7 Correct 4 ms 9712 KB Output is correct
8 Correct 4 ms 9712 KB Output is correct
9 Correct 4 ms 9684 KB Output is correct
10 Correct 4 ms 9684 KB Output is correct
11 Correct 4 ms 9684 KB Output is correct
12 Correct 4 ms 9708 KB Output is correct
13 Correct 4 ms 9684 KB Output is correct
14 Correct 4 ms 9684 KB Output is correct
15 Correct 5 ms 9712 KB Output is correct
16 Correct 4 ms 9684 KB Output is correct
17 Correct 4 ms 9684 KB Output is correct
18 Correct 4 ms 9712 KB Output is correct
19 Correct 4 ms 9684 KB Output is correct
20 Correct 4 ms 9684 KB Output is correct
21 Correct 5 ms 9788 KB Output is correct
22 Correct 6 ms 13396 KB Output is correct
23 Correct 9 ms 12892 KB Output is correct
24 Correct 6 ms 13140 KB Output is correct
25 Correct 6 ms 13068 KB Output is correct
26 Correct 5 ms 11120 KB Output is correct
27 Correct 6 ms 13032 KB Output is correct
28 Correct 5 ms 10620 KB Output is correct
29 Correct 5 ms 11000 KB Output is correct
30 Correct 5 ms 11256 KB Output is correct
31 Correct 6 ms 12372 KB Output is correct
32 Correct 7 ms 12540 KB Output is correct
33 Correct 6 ms 12884 KB Output is correct
34 Correct 6 ms 12116 KB Output is correct
35 Correct 6 ms 12916 KB Output is correct
36 Correct 7 ms 13436 KB Output is correct
37 Correct 7 ms 12892 KB Output is correct
38 Correct 5 ms 11732 KB Output is correct
39 Correct 102 ms 19664 KB Output is correct
40 Correct 100 ms 19956 KB Output is correct
41 Correct 99 ms 19656 KB Output is correct
42 Correct 89 ms 19876 KB Output is correct
43 Correct 83 ms 19948 KB Output is correct
44 Correct 52 ms 19904 KB Output is correct
45 Correct 110 ms 23084 KB Output is correct
46 Correct 78 ms 26836 KB Output is correct
47 Correct 131 ms 30116 KB Output is correct
48 Correct 287 ms 44128 KB Output is correct
49 Correct 375 ms 43048 KB Output is correct
50 Correct 149 ms 30096 KB Output is correct
51 Correct 136 ms 30060 KB Output is correct
52 Correct 198 ms 30160 KB Output is correct
53 Correct 237 ms 29364 KB Output is correct
54 Correct 209 ms 29736 KB Output is correct
55 Correct 12 ms 10748 KB Output is correct
56 Correct 11 ms 10744 KB Output is correct
57 Correct 67 ms 20040 KB Output is correct
58 Correct 36 ms 21736 KB Output is correct
59 Correct 78 ms 27544 KB Output is correct
60 Correct 317 ms 47152 KB Output is correct
61 Correct 129 ms 30220 KB Output is correct
62 Correct 137 ms 34184 KB Output is correct
63 Correct 184 ms 34120 KB Output is correct
64 Correct 308 ms 32688 KB Output is correct
65 Correct 227 ms 30904 KB Output is correct
66 Correct 338 ms 44508 KB Output is correct
67 Correct 104 ms 38768 KB Output is correct
68 Correct 233 ms 35236 KB Output is correct
69 Correct 190 ms 35440 KB Output is correct
70 Correct 178 ms 34228 KB Output is correct