Submission #1082354

# Submission time Handle Problem Language Result Execution time Memory
1082354 2024-08-31T08:08:21 Z vuavisao Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
35 ms 10904 KB
#include <bits/stdc++.h>
using namespace std;

const int N = (int) 5e4 + 10;

int n, q;
vector<pair<int, int>> g[N];

int Lg, dist[N], parent[20][N];
int cnt, in[N];
int sDist[N];

void dfs(int u, int p) {
	in[u] = ++ cnt;
	for (const auto& x : g[u]) {
		int v = x.first, w = x.second;
		if (v == p) continue;
		parent[0][v] = u;
		dist[v] = dist[u] + 1;
		sDist[v] = sDist[u] + w;
		dfs(v, u);
	}
};

int lca(int u, int v) {
	if (dist[u] < dist[v]) swap(u, v);

	int delta = dist[u] - dist[v];
	for (int lg = Lg; lg >= 0; -- lg) {
		if (((delta >> lg) & 1)) {
			u = parent[lg][u];
		}
	}
	if (u == v) return u;
	
	for (int lg = Lg; lg >= 0; -- lg) {
		if (parent[lg][u] != parent[lg][v]) {
			u = parent[lg][u];
			v = parent[lg][v];
		}
	}
	return parent[0][u];
}

int getSdist(int u, int p) {
	return sDist[u] - sDist[p];
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> n;
	for (int i = 2; i <= n; ++ i) {
		int u, v, w; cin >> u >> v >> w;
		++ u, ++ v;
		g[u].push_back(make_pair(v, w));
		g[v].push_back(make_pair(u, w));
	}
	
	Lg = __lg(n);
	dist[1] = 1; dfs(1, 0);
	for (int lg = 1; lg <= Lg; ++ lg) {
		for (int u = 1; u <= n; ++ u) {
			parent[lg][u] = parent[lg - 1][parent[lg - 1][u]];
		}
	}

	cin >> q;
	while (q --) {
		int ver[5];
		for (int i = 0; i < 5; ++ i) cin >> ver[i], ++ ver[i];
		sort(ver, ver + 5, [&] (int u, int v) -> bool {
			return in[u] < in[v];
		});
		int anc = lca(ver[0], ver[1]);
		for (int i = 2; i < 5; ++ i) {
			anc = lca(anc, ver[i]);
		}
		int res = getSdist(ver[0], anc);
		for (int i = 1; i < 5; ++ i) {
			res += getSdist(ver[i], lca(ver[i], ver[i - 1]));
		}
		cout << res << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 9028 KB Output is correct
2 Correct 31 ms 10832 KB Output is correct
3 Correct 34 ms 10844 KB Output is correct
4 Correct 31 ms 10844 KB Output is correct
5 Correct 30 ms 10872 KB Output is correct
6 Correct 30 ms 10904 KB Output is correct
7 Correct 33 ms 10844 KB Output is correct
8 Correct 30 ms 10844 KB Output is correct
9 Correct 34 ms 10832 KB Output is correct
10 Correct 31 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 6992 KB Output is correct
2 Correct 20 ms 10076 KB Output is correct
3 Correct 20 ms 10020 KB Output is correct
4 Correct 19 ms 10076 KB Output is correct
5 Correct 19 ms 10180 KB Output is correct
6 Correct 19 ms 10072 KB Output is correct
7 Correct 19 ms 10076 KB Output is correct
8 Correct 19 ms 10084 KB Output is correct
9 Correct 19 ms 10076 KB Output is correct
10 Correct 18 ms 10036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1624 KB Output is correct
2 Correct 30 ms 9028 KB Output is correct
3 Correct 31 ms 10832 KB Output is correct
4 Correct 34 ms 10844 KB Output is correct
5 Correct 31 ms 10844 KB Output is correct
6 Correct 30 ms 10872 KB Output is correct
7 Correct 30 ms 10904 KB Output is correct
8 Correct 33 ms 10844 KB Output is correct
9 Correct 30 ms 10844 KB Output is correct
10 Correct 34 ms 10832 KB Output is correct
11 Correct 31 ms 10844 KB Output is correct
12 Correct 17 ms 6992 KB Output is correct
13 Correct 20 ms 10076 KB Output is correct
14 Correct 20 ms 10020 KB Output is correct
15 Correct 19 ms 10076 KB Output is correct
16 Correct 19 ms 10180 KB Output is correct
17 Correct 19 ms 10072 KB Output is correct
18 Correct 19 ms 10076 KB Output is correct
19 Correct 19 ms 10084 KB Output is correct
20 Correct 19 ms 10076 KB Output is correct
21 Correct 18 ms 10036 KB Output is correct
22 Correct 32 ms 9052 KB Output is correct
23 Correct 28 ms 7252 KB Output is correct
24 Correct 31 ms 10076 KB Output is correct
25 Correct 33 ms 10076 KB Output is correct
26 Correct 32 ms 10076 KB Output is correct
27 Correct 32 ms 10072 KB Output is correct
28 Correct 35 ms 10256 KB Output is correct
29 Correct 32 ms 10188 KB Output is correct
30 Correct 31 ms 10072 KB Output is correct
31 Correct 30 ms 10160 KB Output is correct