답안 #915872

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
915872 2024-01-24T20:06:32 Z Julto 경주 (Race) (IOI11_race) C++14
100 / 100
2512 ms 126980 KB
#include <bits/stdc++.h>
using namespace std;

const int mxN = 2e5 + 5, inf = 1e9;

int sz[mxN], cenPar[mxN], dep[mxN], par[mxN][18], sum[mxN];
vector<array<int, 2>> adj[mxN];
vector<array<int, 4>> dists[mxN];
bool done[mxN];

void dfs(int u, int p){
	par[u][0] = p;
	for(int i = 1; i < 18; i++){
		par[u][i] = par[par[u][i - 1]][i - 1];
	}
	for(auto i : adj[u]){
		if(i[0] == p) continue;
		dep[i[0]] = dep[u] + 1;
		sum[i[0]] = sum[u] + i[1];
		dfs(i[0], u);
	}
}

int lca(int a, int b){
	if(dep[a] < dep[b]){
		swap(a, b);
	}
	for(int i = 17; i >= 0; i--){
		if(dep[a] - (1 << i) >= dep[b]){
			a = par[a][i];
		}
	}
	if(a == b){
		return a;
	}
	for(int i = 17; i >= 0; i--){
		if(par[a][i] != par[b][i]){
			a = par[a][i];
			b = par[b][i];
		}
	}
	return par[a][0];
}

int calcDist(int a, int b){
	return sum[a] + sum[b] - 2 * sum[lca(a, b)];
}

int calcEdge(int a, int b){
	return dep[a] + dep[b] - 2 * dep[lca(a, b)];
}

int calcSz(int u, int p){
	sz[u] = 1;
	for(auto i : adj[u]){
		if(i[0] == p || done[i[0]]) continue;
		sz[u] += calcSz(i[0], u);
	}
	return sz[u];
}

int get(int u, int p, int x){
	for(auto i : adj[u]){
		if(i[0] == p || done[i[0]]) continue;
		if(sz[i[0]] * 2 > x){
			return get(i[0], u, x);
		}
	}
	return u;
}

int build(int u){
	int centroid = get(u, u, calcSz(u, u));
	done[centroid] = 1;
	dists[centroid].push_back({0, 0, centroid, centroid});
	for(auto i : adj[centroid]){
		if(!done[i[0]]){
			int x = build(i[0]);
			cenPar[x] = centroid;
			for(auto j : dists[x]){
				dists[centroid].push_back({calcDist(centroid, j[3]), calcEdge(centroid, j[3]), x, j[3]});
			}
		}
	}
	sort(dists[centroid].begin(), dists[centroid].end());
	return centroid;
}

int qry(int v, int k){
	int cur = v, subtree = v;
	int ans = inf;
	while(cur != -1){
		int goal = k - calcDist(v, cur);
		array<int, 4> find = {goal, -inf, -inf, -inf};
		int lb = lower_bound(dists[cur].begin(), dists[cur].end(), find) - dists[cur].begin();
		for(int j = lb; j < dists[cur].size() && dists[cur][j][0] == goal; j++){
			if(dists[cur][j][2] == subtree) continue;
			int x = calcEdge(cur, v);
			ans = min(ans, x + dists[cur][j][1]);
			//cout << v << " " << dists[cur][j][3] << ' ' << x + dists[cur][j][1] << '\n';
			break;
				
		}
		subtree = cur;
		cur = cenPar[cur];
	}
	return ans;
}

int best_path(int n, int k, int h[][2], int l[]){
	for(int i = 0; i < n - 1; i++){
		adj[h[i][0]].push_back({h[i][1], l[i]});
		adj[h[i][1]].push_back({h[i][0], l[i]});
	}
	dfs(0, 0);
	for(int i = 0; i < n; i++){
		cenPar[i] = -1;
	}
	build(0);
	/*for(int i = 0; i < n; i++){
		cout << cenPar[i] << " ";
	}
	cout << '\n';
	for(int i = 0; i < n; i++){
		cout << i << '\n';
		for(auto j : dists[i]){
			cout << j[0] << " " << j[1] << " " << j[2] << "\n";
		}
		cout << '\n';
	}*/
	int ans = inf;
	for(int i = 0; i < n; i++){
		//cout << qry(i, k) << " ";
		ans = min(ans, qry(i, k));
	}

	return (ans == inf ? -1 : ans);
}

Compilation message

race.cpp: In function 'int qry(int, int)':
race.cpp:96:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   96 |   for(int j = lb; j < dists[cur].size() && dists[cur][j][0] == goal; j++){
      |                   ~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 4 ms 14684 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 5 ms 14692 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 4 ms 14684 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 5 ms 14692 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 5 ms 14816 KB Output is correct
22 Correct 6 ms 14940 KB Output is correct
23 Correct 8 ms 15116 KB Output is correct
24 Correct 5 ms 14940 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 5 ms 14940 KB Output is correct
27 Correct 5 ms 14820 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 5 ms 14940 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 5 ms 14940 KB Output is correct
32 Correct 5 ms 14940 KB Output is correct
33 Correct 5 ms 14820 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 5 ms 14940 KB Output is correct
36 Correct 5 ms 14936 KB Output is correct
37 Correct 5 ms 14940 KB Output is correct
38 Correct 5 ms 14936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 4 ms 14684 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 5 ms 14692 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
19 Correct 447 ms 53044 KB Output is correct
20 Correct 427 ms 54116 KB Output is correct
21 Correct 446 ms 52556 KB Output is correct
22 Correct 415 ms 49984 KB Output is correct
23 Correct 440 ms 57528 KB Output is correct
24 Correct 214 ms 44664 KB Output is correct
25 Correct 865 ms 67524 KB Output is correct
26 Correct 443 ms 71112 KB Output is correct
27 Correct 424 ms 71352 KB Output is correct
28 Correct 2208 ms 126980 KB Output is correct
29 Correct 2277 ms 126560 KB Output is correct
30 Correct 420 ms 72120 KB Output is correct
31 Correct 424 ms 71332 KB Output is correct
32 Correct 806 ms 71608 KB Output is correct
33 Correct 1873 ms 100576 KB Output is correct
34 Correct 1700 ms 98864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 3 ms 14684 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 4 ms 14684 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 3 ms 14680 KB Output is correct
7 Correct 5 ms 14692 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14680 KB Output is correct
10 Correct 4 ms 14680 KB Output is correct
11 Correct 4 ms 14684 KB Output is correct
12 Correct 3 ms 14684 KB Output is correct
13 Correct 4 ms 14684 KB Output is correct
14 Correct 3 ms 14684 KB Output is correct
15 Correct 3 ms 14684 KB Output is correct
16 Correct 3 ms 14684 KB Output is correct
17 Correct 3 ms 14936 KB Output is correct
18 Correct 3 ms 14680 KB Output is correct
19 Correct 3 ms 14936 KB Output is correct
20 Correct 3 ms 14684 KB Output is correct
21 Correct 5 ms 14816 KB Output is correct
22 Correct 6 ms 14940 KB Output is correct
23 Correct 8 ms 15116 KB Output is correct
24 Correct 5 ms 14940 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 5 ms 14940 KB Output is correct
27 Correct 5 ms 14820 KB Output is correct
28 Correct 4 ms 14940 KB Output is correct
29 Correct 5 ms 14940 KB Output is correct
30 Correct 5 ms 14940 KB Output is correct
31 Correct 5 ms 14940 KB Output is correct
32 Correct 5 ms 14940 KB Output is correct
33 Correct 5 ms 14820 KB Output is correct
34 Correct 5 ms 14940 KB Output is correct
35 Correct 5 ms 14940 KB Output is correct
36 Correct 5 ms 14936 KB Output is correct
37 Correct 5 ms 14940 KB Output is correct
38 Correct 5 ms 14936 KB Output is correct
39 Correct 447 ms 53044 KB Output is correct
40 Correct 427 ms 54116 KB Output is correct
41 Correct 446 ms 52556 KB Output is correct
42 Correct 415 ms 49984 KB Output is correct
43 Correct 440 ms 57528 KB Output is correct
44 Correct 214 ms 44664 KB Output is correct
45 Correct 865 ms 67524 KB Output is correct
46 Correct 443 ms 71112 KB Output is correct
47 Correct 424 ms 71352 KB Output is correct
48 Correct 2208 ms 126980 KB Output is correct
49 Correct 2277 ms 126560 KB Output is correct
50 Correct 420 ms 72120 KB Output is correct
51 Correct 424 ms 71332 KB Output is correct
52 Correct 806 ms 71608 KB Output is correct
53 Correct 1873 ms 100576 KB Output is correct
54 Correct 1700 ms 98864 KB Output is correct
55 Correct 24 ms 19036 KB Output is correct
56 Correct 26 ms 19292 KB Output is correct
57 Correct 335 ms 57572 KB Output is correct
58 Correct 70 ms 36036 KB Output is correct
59 Correct 421 ms 71052 KB Output is correct
60 Correct 2105 ms 125892 KB Output is correct
61 Correct 423 ms 71504 KB Output is correct
62 Correct 407 ms 71636 KB Output is correct
63 Correct 749 ms 71692 KB Output is correct
64 Correct 1069 ms 89088 KB Output is correct
65 Correct 859 ms 83140 KB Output is correct
66 Correct 2512 ms 123884 KB Output is correct
67 Correct 276 ms 53756 KB Output is correct
68 Correct 773 ms 68632 KB Output is correct
69 Correct 749 ms 69184 KB Output is correct
70 Correct 688 ms 66616 KB Output is correct