QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198809#7103. Red Black TreeckisekiAC ✓1016ms33908kbC++203.9kb2023-10-03 17:26:052023-10-03 17:26:05

Judging History

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

  • [2023-10-03 17:26:05]
  • 评测
  • 测评结果:AC
  • 用时:1016ms
  • 内存:33908kb
  • [2023-10-03 17:26:05]
  • 提交

answer

#pragma GCC optimize("Ofast,no-stack-protector")
#include <bits/stdc++.h>
using namespace std;

namespace {

const int maxn = 100025;
int dfn[maxn], dfo[maxn], dfc;
int pa[maxn][17];
vector<pair<int,int>> g[maxn];
int red[maxn];
int up_red[maxn];

int dep[maxn];
int64_t pre[maxn];

bool anc(int u, int v) {
	return dfn[u] <= dfn[v] and dfo[v] <= dfo[u];
}

int lca(int a, int b) {
	if (anc(a, b))
		return a;
	for (int i = 16; i >= 0; --i)
		if (not anc(pa[a][i], b))
			a = pa[a][i];
	return pa[a][0];
}

vector<pair<int, int>> build(vector<int> &vs, int r) {
	vector<pair<int, int>> res;
	sort(vs.begin(), vs.end(), [](int i, int j) {return dfn[i] < dfn[j];});
	vector<int> s = {r};
	for (int v : vs) if (v != r) {
		if (int o = lca(v, s.back()); o != s.back()) {
			while (s.size() >= 2) {
				if (dfn[s[s.size() - 2]] < dfn[o]) break;
				res.emplace_back(s[s.size() - 2], s.back());
				s.pop_back();
			}
			if (s.back() != o) {
				res.emplace_back(o, s.back());
				s.back() = o;
			}
		}
		s.push_back(v);
	}
	for (size_t i = 1; i < s.size(); ++i)
		res.emplace_back(s[i - 1], s[i]);
	return res;
}

vector<int> stk;
void dfs(int i, int f) {
	dfn[i] = dfc++;
	pa[i][0] = f;
	for (int l = 0; l + 1 < 17; l++)
		pa[i][l + 1] = pa[pa[i][l]][l];
	if (red[i]) stk.push_back(i);
	up_red[i] = stk.back();
	for (auto [j, w]: g[i]) {
		if (j == f) continue;
		pre[j] = pre[i] + w;
		dep[j] = dep[i] + 1;
		dfs(j, i);
	}
	if (red[i]) stk.pop_back();
	dfo[i] = dfc;
}

int64_t up[maxn];
bool mark[maxn];
vector<int> T[maxn];

/*
unuse:
all no cover
dead
potential
*/
// first: unuse
// second: used
pair<array<int64_t, 3>, int64_t> dfs2(int u) {
	pair<array<int64_t, 3>, int64_t> ret = {{0, 0, 0}, 0};

	if (mark[u]) {
		ret.first[0] = max(ret.first[0], up[u]);
		ret.first[2] = pre[u];
	}

	int64_t cur = 0;

	pair<pair<int64_t, int>, pair<int64_t, int>> best = {};
	vector<int64_t> used;
	used.reserve(T[u].size());
	for (int v : T[u]) {
		auto sub = dfs2(v);
		ret.first[0] = max(ret.first[0], sub.first[0]);

		if (dep[up_red[v]] < dep[u]) {
			ret.first[2] = max(ret.first[2], sub.first[2]);
		} else {
			sub.first[1] = max(sub.first[1], sub.first[2] - pre[up_red[v]]);
			sub.first[2] = pre[u];
		}
		ret.first[1] = max(ret.first[1], sub.first[1]);

		if (sub.first[0] >= best.first.first) {
			best.second = best.first;
			best.first = {sub.first[0], v};
		} else if (sub.first[0] > best.second.first) {
			best.second = {sub.first[0], v};
		}
		used.push_back(sub.second);
		cur = max(cur, sub.first[1]);
		cur = max(cur, sub.first[2] - pre[u]);
	}
	ret.second = cur;

	for (size_t i = 0; i < used.size(); ++i) {
		int64_t now = used[i];
		if (mark[u])
			now = max(now, up[u]);
		if (T[u][i] == best.first.second)
			now = max(now, best.second.first);
		else
			now = max(now, best.first.first);
		ret.second = min(ret.second, now);
	}
	return ret;
}

void solve() {
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 1; i <= n; i++)
		g[i].clear();
	stk.reserve(n);

	for (int i = 1; i <= n; i++)
		red[i] = false;
	for (int i = 0; i < m; i++) {
		int x;
		cin >> x;
		red[x] = true;
	}

	for (int i = 1; i < n; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		g[u].emplace_back(v, w);
		g[v].emplace_back(u, w);
	}
	dfs(1, 1);
	for (int z = 1; z <= n; ++z)    
		up[z] = pre[z] - pre[up_red[z]];

	while (q--) {
		int k;
		cin >> k;
		vector<int> v(k);
		for (int &z: v) cin >> z;
		for (int z : v) {
			mark[z] = true;
		}

		auto es = build(v, 1);
		for (auto [x, y]: es) {
			T[x].push_back(y);
		}

		cout << dfs2(1).second << '\n';

		for (auto [x, y] : es)
			T[x].clear();
		for (int z : v)
			mark[z] = false;
	}
}

} // namespace

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int tc;
	cin >> tc;
	while (tc--) {
		solve();
	}
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1016ms
memory: 33908kb

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