답안 #833747

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
833747 2023-08-22T08:26:53 Z kingfran1907 경주 (Race) (IOI11_race) C++14
100 / 100
446 ms 104588 KB
#include "race.h"
#include <bits/stdc++.h>
#define X first
#define Y second
 
using namespace std;
typedef long long llint;
 
const int maxn = 2e5+10;
const int maxk = 1e6+10;
const int inf = 0x3f3f3f3f;
 
int n, k;
vector< pair<int, int> > graph[maxn];
map< llint, llint > s[maxn];
llint sol = inf;
llint ofx[maxn], ofy[maxn];
 
void solve(int x, int parr) {
	//printf("solving: %d %d\n", x, parr);
	s[x][0] = 0;
	for (auto iter : graph[x]) {
		int tren = iter.X;
		int cost = iter.Y;
		if (tren == parr) continue;
		solve(tren, x);
		
		//printf("merging %d %d\n", x, tren);
		ofx[tren] += cost;
		ofy[tren]++;
		if (s[tren].size() > s[x].size()) {
			//printf("child is larger\n");
			swap(s[tren], s[x]);
			swap(ofx[tren], ofx[x]);
			swap(ofy[tren], ofy[x]);
		}
		
		for (auto iter : s[tren]) {
			llint dis = iter.X + ofx[tren];
			//printf("trying %lld\n", dis);
			if (s[x].count(k - dis - ofx[x])) 
				sol = min(sol, s[x][k - dis - ofx[x]] + iter.Y + ofy[tren] + ofy[x]);
			
			dis -= ofx[x];
		}
		
		for (auto iter : s[tren]) {
			llint dis = iter.X + ofx[tren] - ofx[x];
			if (s[x].count(dis)) s[x][dis] = min(s[x][dis], iter.Y + ofy[tren] - ofy[x]);
			else s[x][dis] = iter.Y + ofy[tren] - ofy[x];
		} 
	}
}
 
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 a = H[i][0];
		int b = H[i][1];
		graph[a].push_back({b, L[i]});
		graph[b].push_back({a, L[i]});
	}
	solve(0, -1);
	if (sol == inf) return -1;
	return sol;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 6 ms 14420 KB Output is correct
7 Correct 6 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 6 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 6 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 6 ms 14420 KB Output is correct
7 Correct 6 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 6 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 6 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14548 KB Output is correct
22 Correct 8 ms 14676 KB Output is correct
23 Correct 8 ms 14676 KB Output is correct
24 Correct 8 ms 14676 KB Output is correct
25 Correct 8 ms 14676 KB Output is correct
26 Correct 7 ms 14752 KB Output is correct
27 Correct 8 ms 14548 KB Output is correct
28 Correct 8 ms 14676 KB Output is correct
29 Correct 9 ms 14724 KB Output is correct
30 Correct 8 ms 14676 KB Output is correct
31 Correct 8 ms 14676 KB Output is correct
32 Correct 8 ms 14676 KB Output is correct
33 Correct 8 ms 14676 KB Output is correct
34 Correct 8 ms 14676 KB Output is correct
35 Correct 8 ms 14676 KB Output is correct
36 Correct 7 ms 14676 KB Output is correct
37 Correct 8 ms 14548 KB Output is correct
38 Correct 7 ms 14676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 6 ms 14420 KB Output is correct
7 Correct 6 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 6 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 6 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
19 Correct 102 ms 36336 KB Output is correct
20 Correct 125 ms 37732 KB Output is correct
21 Correct 98 ms 37508 KB Output is correct
22 Correct 112 ms 37068 KB Output is correct
23 Correct 143 ms 49860 KB Output is correct
24 Correct 128 ms 40440 KB Output is correct
25 Correct 115 ms 36716 KB Output is correct
26 Correct 57 ms 43492 KB Output is correct
27 Correct 157 ms 49340 KB Output is correct
28 Correct 253 ms 85544 KB Output is correct
29 Correct 246 ms 84044 KB Output is correct
30 Correct 199 ms 49400 KB Output is correct
31 Correct 160 ms 49428 KB Output is correct
32 Correct 285 ms 49484 KB Output is correct
33 Correct 230 ms 54340 KB Output is correct
34 Correct 298 ms 87116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 7 ms 14420 KB Output is correct
3 Correct 8 ms 14420 KB Output is correct
4 Correct 7 ms 14420 KB Output is correct
5 Correct 7 ms 14420 KB Output is correct
6 Correct 6 ms 14420 KB Output is correct
7 Correct 6 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 6 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 7 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 6 ms 14420 KB Output is correct
18 Correct 7 ms 14420 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 7 ms 14420 KB Output is correct
21 Correct 8 ms 14548 KB Output is correct
22 Correct 8 ms 14676 KB Output is correct
23 Correct 8 ms 14676 KB Output is correct
24 Correct 8 ms 14676 KB Output is correct
25 Correct 8 ms 14676 KB Output is correct
26 Correct 7 ms 14752 KB Output is correct
27 Correct 8 ms 14548 KB Output is correct
28 Correct 8 ms 14676 KB Output is correct
29 Correct 9 ms 14724 KB Output is correct
30 Correct 8 ms 14676 KB Output is correct
31 Correct 8 ms 14676 KB Output is correct
32 Correct 8 ms 14676 KB Output is correct
33 Correct 8 ms 14676 KB Output is correct
34 Correct 8 ms 14676 KB Output is correct
35 Correct 8 ms 14676 KB Output is correct
36 Correct 7 ms 14676 KB Output is correct
37 Correct 8 ms 14548 KB Output is correct
38 Correct 7 ms 14676 KB Output is correct
39 Correct 102 ms 36336 KB Output is correct
40 Correct 125 ms 37732 KB Output is correct
41 Correct 98 ms 37508 KB Output is correct
42 Correct 112 ms 37068 KB Output is correct
43 Correct 143 ms 49860 KB Output is correct
44 Correct 128 ms 40440 KB Output is correct
45 Correct 115 ms 36716 KB Output is correct
46 Correct 57 ms 43492 KB Output is correct
47 Correct 157 ms 49340 KB Output is correct
48 Correct 253 ms 85544 KB Output is correct
49 Correct 246 ms 84044 KB Output is correct
50 Correct 199 ms 49400 KB Output is correct
51 Correct 160 ms 49428 KB Output is correct
52 Correct 285 ms 49484 KB Output is correct
53 Correct 230 ms 54340 KB Output is correct
54 Correct 298 ms 87116 KB Output is correct
55 Correct 19 ms 17620 KB Output is correct
56 Correct 13 ms 16432 KB Output is correct
57 Correct 66 ms 35068 KB Output is correct
58 Correct 58 ms 28280 KB Output is correct
59 Correct 82 ms 49740 KB Output is correct
60 Correct 249 ms 84264 KB Output is correct
61 Correct 191 ms 52932 KB Output is correct
62 Correct 176 ms 49480 KB Output is correct
63 Correct 232 ms 49440 KB Output is correct
64 Correct 407 ms 104588 KB Output is correct
65 Correct 399 ms 102824 KB Output is correct
66 Correct 252 ms 81100 KB Output is correct
67 Correct 149 ms 43832 KB Output is correct
68 Correct 314 ms 66444 KB Output is correct
69 Correct 362 ms 71504 KB Output is correct
70 Correct 446 ms 64204 KB Output is correct