QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#807063#7103. Red Black TreeSGColinAC ✓296ms25196kbC++202.2kb2024-12-09 18:36:312024-12-09 18:36:32

Judging History

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

  • [2024-12-09 18:36:32]
  • 评测
  • 测评结果:AC
  • 用时:296ms
  • 内存:25196kb
  • [2024-12-09 18:36:31]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

inline ll rd() {
	ll x = 0;
	bool f = 0;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) f |= (c == '-');
	for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
	return f ? -x : x;
}

#define eb emplace_back
#define all(s) (s).begin(), (s).end()
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)

#define N 100007

vector<pii> e[N];

bool red[N];

int tot, lg[N], dfn[N], st[17][N];

ll dep[N], ans[N];

int getpre(int u, int v) {return dfn[u] < dfn[v] ? u : v;}

void dfs(int u, int fa, ll mxd) {
	if (red[u]) mxd = dep[u];
	ans[u] = dep[u] - mxd;
	st[0][dfn[u] = ++tot] = fa;
	for (auto [v, w] : e[u]) if (v != fa) {
		dep[v] = dep[u] + w; dfs(v, u, mxd);
	}
}

int lca(int u, int v) {
	if (u == v) return u;
	if ((u = dfn[u]) > (v = dfn[v])) swap(u, v);
	int k = lg[v - u++];
	return getpre(st[k][u], st[k][v - (1 << k) + 1]);
}

vector<int> s;

vector<pii> tmp;

inline void work() {
	tot = 0;
	int n = rd(), m = rd(), q = rd();
	rep(i, 1, n) e[i].clear(), red[i] = false;
	rep(i, 1, m) red[rd()] = true;
	rep(i, 1, n - 1) {
		int u = rd(), v = rd(), w = rd();
		e[u].eb(v, w); e[v].eb(u, w);
	}
	dfs(1, 1, 0);
	rep(i, 1, lg[n])
		rep(u, 1, n - (1 << i) + 1) 
			st[i][u] = getpre(st[i - 1][u], st[i - 1][u + (1 << i - 1)]);
	rep(i, 1, q) {
		s.clear(); tmp.clear();
		int u = 0;
		per(t, rd(), 1) {
			int x = rd(); s.eb(x);
			if (u == 0 || ans[x] > ans[u]) u = x;
		}
		if (s.size() == 1) {puts("0"); continue;}

		ll lim = 0, redu = dep[u] - ans[u];
		for (auto x : s) {
			int anc = lca(u, x);
			if (ans[x] <= dep[x] - dep[anc] || dep[anc] <= redu) lim = max(lim, ans[x]);
			else tmp.eb(lca(u, x), x); 
		}
		if (lim == ans[u]) {printf("%lld\n", lim); continue;}
		
		ll res = 1e18;
		sort(all(tmp), [&](pii a, pii b){return dep[a.first] < dep[b.first];});
		for (auto [anc, x] : tmp) {
			res = min(res, max(lim, dep[u] - dep[anc]));
			lim = max(lim, ans[x]);
		}
		printf("%lld\n", res);
	}
}

int main() {
	rep(i, 2, N - 1) lg[i] = lg[i >> 1] + 1;
	per(t, rd(), 1) work();
	return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7996kb

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: 296ms
memory: 25196kb

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