답안 #349028

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
349028 2021-01-16T11:01:49 Z Mefarnis 경주 (Race) (IOI11_race) C++14
100 / 100
717 ms 38480 KB
#include <bits/stdc++.h>
#include "race.h"
#define fi first
#define se second
#define maxn 200000
#define maxk 1000001
#define pb push_back
using namespace std;
typedef pair<int,int> pi;

int n,k,ans;
int sz[maxn];
int dist[maxn];
int depth[maxn];
bool mark[maxn];
int minDepth[maxk];
vector<int> nodes;
vector<pi> adj[maxn];

int dfs(int u , int dad , int de , int di) {
	sz[u] = 1;
	dist[u] = di;
	depth[u] = de;
	nodes.pb(u);
	int deg = adj[u].size();
	for( int i = 0 ; i < deg ; i++ ) {
		int v = adj[u][i].fi;
		int vdi = di + adj[u][i].se;
		if(v != dad && !mark[v])
			sz[u] += dfs(v,u,de+1,vdi);
	}
	return sz[u];
}

void Dfs(int u) {
	nodes.clear();
	dfs(u,-1,0,0);
}

int findRoot() {
	int nsz = nodes.size();
	int root = nodes[0];
	int minSz = sz[root];
	int limit = (nsz+1) / 2;
	for( int i = 1 ; i < nsz ; i++ ) {
		int u = nodes[i];
		if(sz[u] >= limit && sz[u] < minSz) {
			root = u;
			minSz = sz[u];
		}
	}
	return root;
}

void solve(int u) {
	Dfs(u);
	u = findRoot();
	mark[u] = true;
	int deg = adj[u].size();
	vector<int> rem;
	for( int i = 0 ; i < deg ; i++ ) {
		int v = adj[u][i].fi;
		int d = adj[u][i].se;
		if(!mark[v]) {
			Dfs(v);
			vector<pi> upd;
			int nsz = nodes.size();
			for( int j = 0 ; j < nsz ; j++ ) {
				int w = nodes[j];
				int wDist = dist[w] + d;
				int wDepth = depth[w] + 1;
				if(wDist <= k) {
					rem.pb(wDist);
					upd.pb(pi(wDist,wDepth));
					int remDist = k-wDist;
					if(minDepth[remDist] != -1)
						ans = min(ans,minDepth[remDist]+wDepth);
				}
				if(wDist == k)
					ans = min(ans,wDepth);
			}
			int nupd = upd.size();
			for( int j = 0 ; j < nupd ; j++ ) {
				int key = upd[j].fi;
				int val = upd[j].se;
				if(minDepth[key] == -1 || val < minDepth[key])
					minDepth[key] = val;
			}
		}
	}
	int r = rem.size();
	for( int i = 0 ; i < r ; i++ )
		minDepth[rem[i]] = -1;
	for( int i = 0 ; i < deg ; i++ ) {
		int v = adj[u][i].fi;
		if(!mark[v])
			solve(v);
	}
}

int best_path(int N, int K, int e[][2], int c[]) {
	n = N;
	k = K;
	ans = n+1;
	for( int i = 0 ; i < n-1 ; i++ ) {
		int u = e[i][0];
		int v = e[i][1];
		int d = c[i];
		adj[u].pb(pi(v,d));
		adj[v].pb(pi(u,d));
	}
	memset(minDepth,-1,sizeof(minDepth));
	solve(0);
	return (ans < n) ? ans : -1;
}

/*
int main() {
	int n = 11 , k = 12;
	int e[10][2];
	e[0][0] = 0 , e[0][1] = 1;
	e[1][0] = 0 , e[1][1] = 2;
	e[2][0] = 2 , e[2][1] = 3;
	e[3][0] = 3 , e[3][1] = 4;
	e[4][0] = 4 , e[4][1] = 5;
	e[5][0] = 0 , e[5][1] = 6;
	e[6][0] = 6 , e[6][1] = 7;
	e[7][0] = 6 , e[7][1] = 8;
	e[8][0] = 8 , e[8][1] = 9;
	e[9][0] = 8 , e[9][1] = 10;
	int d[10];
	d[0] = 3;
	d[1] = 4;
	d[2] = 5;
	d[3] = 4;
	d[4] = 6;
	d[5] = 3;
	d[6] = 2;
	d[7] = 5;
	d[8] = 6;
	d[9] = 999;
	printf("%d\n",best_path(n,k,e,d));
	return 0;
}
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 7 ms 8940 KB Output is correct
3 Correct 6 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 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 6 ms 9068 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 7 ms 8940 KB Output is correct
17 Correct 7 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 7 ms 8940 KB Output is correct
3 Correct 6 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 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 6 ms 9068 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 7 ms 8940 KB Output is correct
17 Correct 7 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
19 Correct 7 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 7 ms 9068 KB Output is correct
23 Correct 7 ms 9068 KB Output is correct
24 Correct 7 ms 9068 KB Output is correct
25 Correct 7 ms 9068 KB Output is correct
26 Correct 7 ms 9068 KB Output is correct
27 Correct 7 ms 9068 KB Output is correct
28 Correct 7 ms 9068 KB Output is correct
29 Correct 7 ms 9068 KB Output is correct
30 Correct 7 ms 9068 KB Output is correct
31 Correct 8 ms 9068 KB Output is correct
32 Correct 7 ms 9068 KB Output is correct
33 Correct 7 ms 9068 KB Output is correct
34 Correct 7 ms 9068 KB Output is correct
35 Correct 7 ms 9068 KB Output is correct
36 Correct 7 ms 9068 KB Output is correct
37 Correct 8 ms 9068 KB Output is correct
38 Correct 9 ms 9196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 7 ms 8940 KB Output is correct
3 Correct 6 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 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 6 ms 9068 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 7 ms 8940 KB Output is correct
17 Correct 7 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
19 Correct 258 ms 15720 KB Output is correct
20 Correct 235 ms 15616 KB Output is correct
21 Correct 245 ms 16068 KB Output is correct
22 Correct 206 ms 16568 KB Output is correct
23 Correct 167 ms 15896 KB Output is correct
24 Correct 105 ms 15724 KB Output is correct
25 Correct 236 ms 19180 KB Output is correct
26 Correct 143 ms 24420 KB Output is correct
27 Correct 259 ms 22380 KB Output is correct
28 Correct 612 ms 36576 KB Output is correct
29 Correct 597 ms 35552 KB Output is correct
30 Correct 274 ms 22372 KB Output is correct
31 Correct 270 ms 22252 KB Output is correct
32 Correct 396 ms 22376 KB Output is correct
33 Correct 487 ms 21220 KB Output is correct
34 Correct 428 ms 21220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8940 KB Output is correct
2 Correct 7 ms 8940 KB Output is correct
3 Correct 6 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 6 ms 8940 KB Output is correct
8 Correct 7 ms 8940 KB Output is correct
9 Correct 6 ms 8940 KB Output is correct
10 Correct 6 ms 8940 KB Output is correct
11 Correct 6 ms 8940 KB Output is correct
12 Correct 6 ms 9068 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 7 ms 8940 KB Output is correct
17 Correct 7 ms 8940 KB Output is correct
18 Correct 7 ms 8940 KB Output is correct
19 Correct 7 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 7 ms 9068 KB Output is correct
23 Correct 7 ms 9068 KB Output is correct
24 Correct 7 ms 9068 KB Output is correct
25 Correct 7 ms 9068 KB Output is correct
26 Correct 7 ms 9068 KB Output is correct
27 Correct 7 ms 9068 KB Output is correct
28 Correct 7 ms 9068 KB Output is correct
29 Correct 7 ms 9068 KB Output is correct
30 Correct 7 ms 9068 KB Output is correct
31 Correct 8 ms 9068 KB Output is correct
32 Correct 7 ms 9068 KB Output is correct
33 Correct 7 ms 9068 KB Output is correct
34 Correct 7 ms 9068 KB Output is correct
35 Correct 7 ms 9068 KB Output is correct
36 Correct 7 ms 9068 KB Output is correct
37 Correct 8 ms 9068 KB Output is correct
38 Correct 9 ms 9196 KB Output is correct
39 Correct 258 ms 15720 KB Output is correct
40 Correct 235 ms 15616 KB Output is correct
41 Correct 245 ms 16068 KB Output is correct
42 Correct 206 ms 16568 KB Output is correct
43 Correct 167 ms 15896 KB Output is correct
44 Correct 105 ms 15724 KB Output is correct
45 Correct 236 ms 19180 KB Output is correct
46 Correct 143 ms 24420 KB Output is correct
47 Correct 259 ms 22380 KB Output is correct
48 Correct 612 ms 36576 KB Output is correct
49 Correct 597 ms 35552 KB Output is correct
50 Correct 274 ms 22372 KB Output is correct
51 Correct 270 ms 22252 KB Output is correct
52 Correct 396 ms 22376 KB Output is correct
53 Correct 487 ms 21220 KB Output is correct
54 Correct 428 ms 21220 KB Output is correct
55 Correct 17 ms 9708 KB Output is correct
56 Correct 18 ms 9708 KB Output is correct
57 Correct 132 ms 16984 KB Output is correct
58 Correct 58 ms 16608 KB Output is correct
59 Correct 150 ms 24412 KB Output is correct
60 Correct 664 ms 38480 KB Output is correct
61 Correct 283 ms 22628 KB Output is correct
62 Correct 276 ms 23524 KB Output is correct
63 Correct 405 ms 23648 KB Output is correct
64 Correct 717 ms 24452 KB Output is correct
65 Correct 493 ms 22280 KB Output is correct
66 Correct 706 ms 32864 KB Output is correct
67 Correct 197 ms 24792 KB Output is correct
68 Correct 408 ms 23520 KB Output is correct
69 Correct 405 ms 23800 KB Output is correct
70 Correct 384 ms 23012 KB Output is correct