QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#356950#7103. Red Black TreekarunaAC ✓576ms24648kbC++201.9kb2024-03-18 16:25:472024-03-18 16:25:48

Judging History

你现在查看的是最新测评结果

  • [2024-03-18 16:25:48]
  • 评测
  • 测评结果:AC
  • 用时:576ms
  • 内存:24648kb
  • [2024-03-18 16:25:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 101010;
typedef long long ll;

int n, m, q; bool red[MAXN];
vector<pair<int, int>> g[MAXN];

int sp[20][MAXN], C[MAXN], R[MAXN]; ll D[MAXN];
void dfs(int p, int v) {
	if (red[v]) {
		D[v] = 0;
		R[v] = v;
	}
	for (auto [w, x] : g[v]) if (p != x) {
		sp[0][x] = v;
		C[x] = C[v] + 1;
		D[x] = D[v] + w;
		R[x] = R[v];
		dfs(v, x);
	}
}
int lca(int u, int v) {
	if (C[u] > C[v]) swap(u, v);
	int d = C[v] - C[u];
	for (int i = 0; i < 20; i++) {
		if (d >> i & 1)	v = sp[i][v];
	}
	for (int i = 19; i >= 0; i--) {
		if (sp[i][u] != sp[i][v]) {
			u = sp[i][u];
			v = sp[i][v];
		}
	}
	return (u == v) ? u : sp[0][u];
}

int main() {
	cin.tie(0); ios_base::sync_with_stdio(0);
	int T; cin >> T;
	while (T--) {
		cin >> n >> m >> q;
		for (int i = 0; i < m; i++) {
			int x; cin >> x;
			red[x] = true;
		}
		for (int i = 0; i < n - 1; i++) {
			int u, v, w;
			cin >> u >> v >> w;

			g[u].push_back({w, v});
			g[v].push_back({w, u});
		}
		dfs(-1, 1);

		for (int j = 1; j < 20; j++) {
			for (int i = 1; i <= n; i++) {
				sp[j][i] = sp[j - 1][sp[j - 1][i]];
			}
		}
		while (q--) {
			int sz; cin >> sz;
			vector<int> V(sz);
			for (int &x : V) {
				cin >> x;
			}
			int x = V[0];
			for (int i = 1; i < sz; i++) {
				if (D[x] < D[V[i]]) {
					x = V[i];
				}
			}
			vector<int> W;
			
			ll mx = 0;
			for (int i = 0; i < sz; i++) {
				if (R[V[i]] == R[x]) {
					W.push_back(V[i]);
				}
				else {
					mx = max(mx, D[V[i]]);
				}
			}

			sort(W.begin(), W.end(), [&](int i, int j) {
				return D[i] > D[j];
			});

			ll ans = D[x];
			sz = W.size();
			for (int i = 0, v = W[0]; i < sz; i++) {
				v = lca(v, W[i]);
				ans = min(ans, max(mx, max(D[x] - D[v], (i == sz - 1) ? 0 : D[W[i + 1]])));
			}

			cout << ans << '\n';
		}

		for (int i = 1; i <= n; i++) {
			g[i].clear();
			red[i] = false;
		}
	}
}

这程序好像有点Bug,我给组数据试试?

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 10004kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 576ms
memory: 24648kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed