답안 #402588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
402588 2021-05-12T02:28:28 Z penguinhacker Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
67 ms 11792 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=50000;
int n, q, t, pos[mxN], dp[mxN], d[mxN], anc[mxN][16];
vector<ar<int, 2>> adj[mxN];

void dfs(int u=0, int p=-1) {
	pos[u]=t++;
	for (int i=1; i<16; ++i)
		anc[u][i]=anc[anc[u][i-1]][i-1];
	for (ar<int, 2> v : adj[u]) {
		if (v[0]==p)
			continue;
		anc[v[0]][0]=u, d[v[0]]=d[u]+1, dp[v[0]]=dp[u]+v[1];
		dfs(v[0], u);
	}
}

int lca(int u, int v) {
	if (d[u]>d[v])
		swap(u, v);
	for (int i=15; ~i; --i)
		if (d[u]<=d[v]-(1<<i))
			v=anc[v][i];
	if (u==v)
		return u;
	for (int i=15; ~i; --i)
		if (anc[u][i]^anc[v][i])
			u=anc[u][i], v=anc[v][i];
	return anc[u][0];
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i=1; i<n; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		adj[u].push_back({v, w});
		adj[v].push_back({u, w});
	}
	dfs();
	cin >> q;
	while(q--) {
		int u[5], ans=0;
		for (int i=0; i<5; ++i)
			cin >> u[i], ans+=dp[u[i]];
		sort(u, u+5, [](int a, int b) {return pos[a]<pos[b];});
		int l=u[0];
		for (int i=1; i<5; ++i) {
			int l2=lca(u[i-1], u[i]);
			ans-=dp[l2];
			l=lca(l, l2);
		}
		ans-=dp[l];
		cout << ans << "\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 10008 KB Output is correct
2 Correct 62 ms 11716 KB Output is correct
3 Correct 63 ms 11792 KB Output is correct
4 Correct 63 ms 11744 KB Output is correct
5 Correct 62 ms 11764 KB Output is correct
6 Correct 64 ms 11700 KB Output is correct
7 Correct 65 ms 11720 KB Output is correct
8 Correct 62 ms 11736 KB Output is correct
9 Correct 63 ms 11716 KB Output is correct
10 Correct 62 ms 11700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 7772 KB Output is correct
2 Correct 40 ms 10692 KB Output is correct
3 Correct 41 ms 10764 KB Output is correct
4 Correct 41 ms 10728 KB Output is correct
5 Correct 41 ms 10760 KB Output is correct
6 Correct 40 ms 10692 KB Output is correct
7 Correct 42 ms 10816 KB Output is correct
8 Correct 44 ms 10712 KB Output is correct
9 Correct 41 ms 10692 KB Output is correct
10 Correct 42 ms 10700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 67 ms 10008 KB Output is correct
3 Correct 62 ms 11716 KB Output is correct
4 Correct 63 ms 11792 KB Output is correct
5 Correct 63 ms 11744 KB Output is correct
6 Correct 62 ms 11764 KB Output is correct
7 Correct 64 ms 11700 KB Output is correct
8 Correct 65 ms 11720 KB Output is correct
9 Correct 62 ms 11736 KB Output is correct
10 Correct 63 ms 11716 KB Output is correct
11 Correct 62 ms 11700 KB Output is correct
12 Correct 33 ms 7772 KB Output is correct
13 Correct 40 ms 10692 KB Output is correct
14 Correct 41 ms 10764 KB Output is correct
15 Correct 41 ms 10728 KB Output is correct
16 Correct 41 ms 10760 KB Output is correct
17 Correct 40 ms 10692 KB Output is correct
18 Correct 42 ms 10816 KB Output is correct
19 Correct 44 ms 10712 KB Output is correct
20 Correct 41 ms 10692 KB Output is correct
21 Correct 42 ms 10700 KB Output is correct
22 Correct 59 ms 9856 KB Output is correct
23 Correct 48 ms 8132 KB Output is correct
24 Correct 64 ms 11052 KB Output is correct
25 Correct 63 ms 11092 KB Output is correct
26 Correct 62 ms 11072 KB Output is correct
27 Correct 63 ms 11080 KB Output is correct
28 Correct 62 ms 11076 KB Output is correct
29 Correct 63 ms 11156 KB Output is correct
30 Correct 63 ms 11140 KB Output is correct
31 Correct 63 ms 11140 KB Output is correct