답안 #870832

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
870832 2023-11-09T09:09:13 Z NintsiChkhaidze 경주 (Race) (IOI11_race) C++17
100 / 100
833 ms 46008 KB
#include "race.h"
#include <bits/stdc++.h>
#define pb push_back
using namespace std;

const int NN = 2e5 + 5;
vector <pair <int,int> > v[NN];
int k,ans,vis[NN];
map <int,int> mp;

int findcentroid(int x,int par,int n,int &centroid){
	int s=1;
	for (auto [to,w]: v[x]){
		if (!vis[to] && to != par) 
			s += findcentroid(to,x,n,centroid);
	}
	
	if (centroid == -1 && (par == -1 || s*2 >= n)) centroid = x;
	return s;
}

void dfs(int x,int par,int dist,int dep,int type){
	
	if (dist == k){
		ans = min(ans,dep);
		return;
	}
	
	if (!type){
		if (mp.find(k - dist) != mp.end()){
			ans=min(ans,mp[k - dist] + dep);
		}
	}else{
		if (mp.find(dist) == mp.end()){
			mp[dist] = dep;
		}else{
			mp[dist] = min(mp[dist],dep);
		}
	}
	
	for (auto [to,w]: v[x]){
		if (!vis[to] && to != par) dfs(to,x,w + dist,dep + 1,type);
	}
}
void solve(int x,int n){
	int centroid = -1;
	findcentroid(x,-1,n,centroid);
	vis[centroid] = 1;
	mp.clear();
	
	for (auto [to,w]: v[centroid]){
		if (!vis[to]){
			dfs(to,x,w,1,0);
			dfs(to,x,w,1,1);
		}
	}
	
	for (auto [to,w]: v[centroid]){
		if (!vis[to]) solve(to,n/2);
	}
}
int best_path(int N, int K, int H[][2], int L[]){
    
  	for (int i = 0; i < N - 1; i++){
  		int a = H[i][0] + 1,b = H[i][1] + 1;
  		v[a].pb({b,L[i]});
  		v[b].pb({a,L[i]});
  	}
	k=K;
	ans=1e9;
	solve(1,N);
	if (ans == 1e9) ans=-1;
	return ans; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9560 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9816 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9568 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9560 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9816 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9568 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9564 KB Output is correct
20 Correct 2 ms 9564 KB Output is correct
21 Correct 3 ms 9564 KB Output is correct
22 Correct 3 ms 9560 KB Output is correct
23 Correct 3 ms 9564 KB Output is correct
24 Correct 3 ms 9564 KB Output is correct
25 Correct 3 ms 9560 KB Output is correct
26 Correct 3 ms 9564 KB Output is correct
27 Correct 2 ms 9564 KB Output is correct
28 Correct 3 ms 9644 KB Output is correct
29 Correct 3 ms 9564 KB Output is correct
30 Correct 3 ms 9564 KB Output is correct
31 Correct 3 ms 9564 KB Output is correct
32 Correct 3 ms 9564 KB Output is correct
33 Correct 4 ms 9564 KB Output is correct
34 Correct 3 ms 9564 KB Output is correct
35 Correct 3 ms 9564 KB Output is correct
36 Correct 3 ms 9564 KB Output is correct
37 Correct 3 ms 9564 KB Output is correct
38 Correct 3 ms 9560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9560 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9816 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9568 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 200 ms 15188 KB Output is correct
20 Correct 206 ms 15428 KB Output is correct
21 Correct 197 ms 15440 KB Output is correct
22 Correct 154 ms 15444 KB Output is correct
23 Correct 213 ms 15716 KB Output is correct
24 Correct 96 ms 15448 KB Output is correct
25 Correct 87 ms 19024 KB Output is correct
26 Correct 75 ms 22608 KB Output is correct
27 Correct 301 ms 19508 KB Output is correct
28 Correct 569 ms 46008 KB Output is correct
29 Correct 559 ms 44884 KB Output is correct
30 Correct 291 ms 22356 KB Output is correct
31 Correct 314 ms 22420 KB Output is correct
32 Correct 377 ms 22356 KB Output is correct
33 Correct 172 ms 20972 KB Output is correct
34 Correct 596 ms 31324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9560 KB Output is correct
2 Correct 2 ms 9560 KB Output is correct
3 Correct 2 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9816 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9564 KB Output is correct
13 Correct 2 ms 9568 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 2 ms 9564 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9564 KB Output is correct
20 Correct 2 ms 9564 KB Output is correct
21 Correct 3 ms 9564 KB Output is correct
22 Correct 3 ms 9560 KB Output is correct
23 Correct 3 ms 9564 KB Output is correct
24 Correct 3 ms 9564 KB Output is correct
25 Correct 3 ms 9560 KB Output is correct
26 Correct 3 ms 9564 KB Output is correct
27 Correct 2 ms 9564 KB Output is correct
28 Correct 3 ms 9644 KB Output is correct
29 Correct 3 ms 9564 KB Output is correct
30 Correct 3 ms 9564 KB Output is correct
31 Correct 3 ms 9564 KB Output is correct
32 Correct 3 ms 9564 KB Output is correct
33 Correct 4 ms 9564 KB Output is correct
34 Correct 3 ms 9564 KB Output is correct
35 Correct 3 ms 9564 KB Output is correct
36 Correct 3 ms 9564 KB Output is correct
37 Correct 3 ms 9564 KB Output is correct
38 Correct 3 ms 9560 KB Output is correct
39 Correct 200 ms 15188 KB Output is correct
40 Correct 206 ms 15428 KB Output is correct
41 Correct 197 ms 15440 KB Output is correct
42 Correct 154 ms 15444 KB Output is correct
43 Correct 213 ms 15716 KB Output is correct
44 Correct 96 ms 15448 KB Output is correct
45 Correct 87 ms 19024 KB Output is correct
46 Correct 75 ms 22608 KB Output is correct
47 Correct 301 ms 19508 KB Output is correct
48 Correct 569 ms 46008 KB Output is correct
49 Correct 559 ms 44884 KB Output is correct
50 Correct 291 ms 22356 KB Output is correct
51 Correct 314 ms 22420 KB Output is correct
52 Correct 377 ms 22356 KB Output is correct
53 Correct 172 ms 20972 KB Output is correct
54 Correct 596 ms 31324 KB Output is correct
55 Correct 18 ms 10332 KB Output is correct
56 Correct 13 ms 10076 KB Output is correct
57 Correct 115 ms 17012 KB Output is correct
58 Correct 36 ms 16592 KB Output is correct
59 Correct 212 ms 27476 KB Output is correct
60 Correct 565 ms 44620 KB Output is correct
61 Correct 406 ms 23832 KB Output is correct
62 Correct 334 ms 22608 KB Output is correct
63 Correct 345 ms 22384 KB Output is correct
64 Correct 833 ms 27224 KB Output is correct
65 Correct 693 ms 32340 KB Output is correct
66 Correct 599 ms 42484 KB Output is correct
67 Correct 82 ms 22928 KB Output is correct
68 Correct 532 ms 27472 KB Output is correct
69 Correct 519 ms 27220 KB Output is correct
70 Correct 483 ms 26592 KB Output is correct