QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#626291#7103. Red Black Treekian2009AC ✓1332ms36628kbC++142.3kb2024-10-10 02:25:262024-10-10 02:25:27

Judging History

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

  • [2024-10-10 02:25:27]
  • 评测
  • 测评结果:AC
  • 用时:1332ms
  • 内存:36628kb
  • [2024-10-10 02:25:26]
  • 提交

answer

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

typedef long long ll;

const int MAXN = 1e5 + 10, MAXBIT = 18;

ll t, n, m, k, q, par[MAXN], near[MAXN], h[MAXN], dp[MAXN][MAXBIT], dist[MAXN];
int cnt, st[MAXN], ed[MAXN];
vector<pair<int, int>> adj[MAXN];
vector<pair<ll, int>> num;
set<pair<int, int>> mark;
bool red[MAXN];

void setup() {
	for (int i = 0; i < n; i++) {
		red[i] = cnt = near[i] = 0;
		adj[i].clear();
	}
}

void input() {
	cin >> n >> m >> q;
	setup();
	for (int i = 0; i < m; i++) {
		int u;
		cin >> u;
		red[--u] = true;	
	}
	for (int i = 0; i < n - 1; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		adj[--u].push_back({--v, w});
		adj[v].push_back({u, w});	
	}
}

void calcDp(int u) {
	dp[u][0] = par[u];
	for (int i = 1; i < MAXBIT; i++)
		dp[u][i] = dp[dp[u][i - 1]][i - 1];
}

void dfs(int u, int p, ll d) {
	st[u] = cnt;
	cnt++;
	par[u] = p;
	if (!red[u])
		near[u] = near[p] + d;
	calcDp(u);	
	for (auto x : adj[u])
		if (x.first != p) {
			h[x.first] = h[u] + 1;
			dist[x.first] = dist[u] + x.second;
			dfs(x.first, u, x.second);
		}
	ed[u] = cnt;
	cnt++;
}

int up(int u, int d) {
	ll res = u;
	for (int i = 0; i < MAXBIT; i++)
		if ((d >> i) & 1) 
			res = dp[res][i];
	return res;
}
 
int lca(int u, int v) {
	if (h[u] > h[v])
		swap(u, v);
	v = up(v, h[v] - h[u]);
	if (u == v)
		return u;
	for (int i = MAXBIT - 1; i >= 0; i--)
		if (dp[u][i] != dp[v][i]) {
			u = dp[u][i];
			v = dp[v][i];
		}
	return par[u];
}
 
int add(int u) {
	mark.insert({st[u], u});
	return lca((*mark.begin()).second, (*mark.rbegin()).second);	
}

ll findAns() {
	ll res = num.back().first, ma = 0;
	for (int i = num.size() - 1; i >= 0; i--) {
		int r = add(num[i].second);
		ma = max(dist[num[i].second], ma);
		res = min(res, max((i != 0? num[i - 1].first: 0), ma - dist[r]));
	}	
	return res;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> t;
	for (int i = 0; i < t; i++) {
		input();
		dfs(0, 0, 0);
		for (int j = 0; j < q; j++) {
			cin >> k;
			num.clear();
			mark.clear();
			for (int l = 0; l < k; l++) {
				int u;
				cin >> u;
				--u;
				num.push_back({near[u], u});
			}
			sort(num.begin(), num.end());
			cout << findAns() << '\n';
		}
	}
	return 0;	
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 10592kb

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: 1332ms
memory: 36628kb

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