답안 #497629

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
497629 2021-12-23T12:12:46 Z zhougz Roadside Advertisements (NOI17_roadsideadverts) C++17
100 / 100
87 ms 10924 KB
/**
 *    author: zhougz
 *    created: 23/12/2021 19:37:06
**/
#include <bits/stdc++.h>

using namespace std;

int n, k;
const int MAXN = 50'050, MAXK = log2(MAXN) + 1;
vector<pair<int, int>> v[MAXN];
int anc[MAXN][MAXK], dist[MAXN], dep[MAXN], par[20];
vector<tuple<int, int, int>> edges;

void clear() {
	iota(par, par + 20, 0);
	edges.clear();
}

int find(int x) {
	return x == par[x] ? x : par[x] = find(par[x]);
}

void unite(int x, int y) {
	par[find(y)] = find(x);
}

void dfs(int x, int par) {
	anc[x][0] = par;
	for (int i = 1; i <= k; i++) {
		int half_par = anc[x][i - 1];
		if (half_par == -1) {
			break;
		}
		anc[x][i] = anc[half_par][i - 1];
	}
	for (const auto &[z, w] : v[x]) {
		if (z != par) {
			dist[z] = dist[x] + w;
			dep[z] = dep[x] + 1;
			dfs(z, x);
		}
	}
}

int lca(int a, int b) {
	if (dep[a] > dep[b]) {
		swap(a, b);
	}
	int bal = dep[b] - dep[a];
	for (int i = 0; i <= k; i++) {
		if (bal & (1 << i)) {
			b = anc[b][i];
		}
	}
	assert(dep[a] == dep[b]);
	if (a == b) {
		return a;
	}
	for (int i = k; i >= 0; i--) {
		if (anc[a][i] == anc[b][i]) {
			continue;
		}
		a = anc[a][i];
		b = anc[b][i];
	}
	assert(anc[a][0] == anc[b][0]);
	return anc[a][0];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	k = log2(n);
	for (int i = 0, a, b, w; i < n - 1; i++) {
		cin >> a >> b >> w;
		v[a].emplace_back(b, w);
		v[b].emplace_back(a, w);
	}
	dfs(0, -1);
	int q;
	cin >> q;
	while (q--) {
		vector<int> nodes;
		for (int i = 0, x; i < 5; i++) {
			cin >> x;
			nodes.push_back(x);
		}
		for (int i = 0; i < 5; i++) {
			for (int j = i + 1; j < 5; j++) {
				nodes.push_back(lca(nodes[i], nodes[j]));
			}
		}
		sort(nodes.begin(), nodes.end());
		nodes.erase(unique(nodes.begin(), nodes.end()), nodes.end());
		clear();
		for (int i = 0; i < (int)nodes.size(); i++) {
			for (int j = i + 1; j < (int)nodes.size(); j++) {
				edges.emplace_back(dist[nodes[i]] + dist[nodes[j]] - 2 * dist[lca(nodes[i], nodes[j])], i, j);
			}
		}
		sort(edges.begin(), edges.end());
		int res = 0;
		for (const auto &[w, a, b] : edges) {
			if (find(a) != find(b)) {
				unite(a, b);
				res += w;
			}
		}
		cout << res << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 9328 KB Output is correct
2 Correct 74 ms 10784 KB Output is correct
3 Correct 62 ms 10724 KB Output is correct
4 Correct 66 ms 10728 KB Output is correct
5 Correct 81 ms 10924 KB Output is correct
6 Correct 73 ms 10816 KB Output is correct
7 Correct 64 ms 10792 KB Output is correct
8 Correct 67 ms 10712 KB Output is correct
9 Correct 75 ms 10824 KB Output is correct
10 Correct 62 ms 10712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 7516 KB Output is correct
2 Correct 43 ms 9964 KB Output is correct
3 Correct 42 ms 9988 KB Output is correct
4 Correct 30 ms 9960 KB Output is correct
5 Correct 35 ms 9976 KB Output is correct
6 Correct 39 ms 9928 KB Output is correct
7 Correct 35 ms 9956 KB Output is correct
8 Correct 35 ms 9952 KB Output is correct
9 Correct 36 ms 9952 KB Output is correct
10 Correct 36 ms 9928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 87 ms 9328 KB Output is correct
3 Correct 74 ms 10784 KB Output is correct
4 Correct 62 ms 10724 KB Output is correct
5 Correct 66 ms 10728 KB Output is correct
6 Correct 81 ms 10924 KB Output is correct
7 Correct 73 ms 10816 KB Output is correct
8 Correct 64 ms 10792 KB Output is correct
9 Correct 67 ms 10712 KB Output is correct
10 Correct 75 ms 10824 KB Output is correct
11 Correct 62 ms 10712 KB Output is correct
12 Correct 24 ms 7516 KB Output is correct
13 Correct 43 ms 9964 KB Output is correct
14 Correct 42 ms 9988 KB Output is correct
15 Correct 30 ms 9960 KB Output is correct
16 Correct 35 ms 9976 KB Output is correct
17 Correct 39 ms 9928 KB Output is correct
18 Correct 35 ms 9956 KB Output is correct
19 Correct 35 ms 9952 KB Output is correct
20 Correct 36 ms 9952 KB Output is correct
21 Correct 36 ms 9928 KB Output is correct
22 Correct 83 ms 9444 KB Output is correct
23 Correct 86 ms 7964 KB Output is correct
24 Correct 69 ms 10336 KB Output is correct
25 Correct 67 ms 10340 KB Output is correct
26 Correct 72 ms 10340 KB Output is correct
27 Correct 86 ms 10248 KB Output is correct
28 Correct 81 ms 10276 KB Output is correct
29 Correct 76 ms 10332 KB Output is correct
30 Correct 70 ms 10352 KB Output is correct
31 Correct 79 ms 10452 KB Output is correct