QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#585752#7103. Red Black TreefengTL 2ms5644kbC++202.9kb2024-09-23 22:03:262024-09-23 22:03:33

Judging History

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

  • [2024-09-23 22:03:33]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:5644kb
  • [2024-09-23 22:03:26]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define PII pair<int,int>
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
struct edge {
	int v, w;
};
vector<edge> tr[maxn];
bool sta[maxn];
int dep[maxn], f[maxn][30], c[maxn][30];
template<typename T>inline void readT(T& x) {
	bool f = 1; x = 0; char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-') f = !f; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
	x = (f ? x : -x); return;
}
template<typename T>
inline void writeT(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) writeT(x / 10);
	putchar(x % 10 + '0'); return;
}
void ini(int u, int fa) {
	dep[u] = dep[fa] + 1;
	f[u][0] = fa;
	for (int i = 1; i <= 21; ++i) {
		f[u][i] = f[f[u][i - 1]][i - 1];
		c[u][i] = c[f[u][i - 1]][i - 1] + c[u][i - 1];
	}
	for (edge e : tr[u]) {
		int v = e.v;
		int w = e.w;
		if (v == fa) continue;
		c[v][0] = w;
		ini(v, u);
	}
}
int lca(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	for (int i = 21; i >= 0; --i) {
		if (dep[f[u][i]] < dep[v]) continue;
		u = f[u][i];
	}
	if (u == v) return u;
	for (int i = 21; i >= 0; --i) {
		if (f[u][i] == f[v][i]) continue;
		u = f[u][i];
		v = f[v][i];
	}
	return f[u][0];
}
int get(int u, int aim) {
	int res = 0;
	for (int i = 21; i >= 0; --i) {
		if (dep[f[u][i]] < dep[aim]) continue;
		res += c[u][i];
		u = f[u][i];
	}
	return res;
}
int ans[maxn];
void dfs(int u, int d, int fa) {
	if (!sta[u]) ans[u] = d;
	else ans[u] = 0;
	for (edge e : tr[u]) {
		int v = e.v;
		int w = e.w;
		if (v == fa) continue;
		if (sta[u]) dfs(v, w, u);
		else dfs(v, d + w, u);
	}
}
vector<int> t;
bool check(int mid) {
	vector<int> te;
	for (int i = 0; i < t.size(); ++i) {
		if (ans[t[i]] > mid) {
			te.push_back(t[i]);
		}
	}
	if (te.size() <= 1) return true;
	int an = te[0];
	for (int i = 1; i < te.size(); ++i) {
		an = lca(te[i], an);
	}
	for (int i = 0; i < te.size(); ++i) {
		if (get(t[i], an) > mid) return false;
	}
	return true;
}
void solve() {
	int n, m, q;
	readT(n);readT(m);readT(q);
	for (int i = 1; i <= n; ++i) {
		tr[i].clear();
		sta[i] = false;
	}
	for (int i = 1; i <= m; ++i) {
		int a; readT(a);
		sta[a] = true;
	}
	for (int i = 1; i < n; ++i) {
		int u, v, w;
		readT(u); readT(v); readT(w);
		tr[u].push_back({ v,w });
		tr[v].push_back({ u,w });
	}
	ini(1, 0);
	dfs(1, 0, 0);
	while (q--) {
		int k; readT(k);
		t.clear();
		for (int i = 0; i < k; i++) {
			int a; readT(a);
			t.push_back(a);
		}
		int l = 0, r = 1e15, res = LLONG_MAX;
		while (l <= r) {
			int mid = l + r >> 1;
			if (check(mid)) {
				r = mid - 1;
				res = mid;
			}
			else l = mid + 1;
		}
		writeT(res);
		putchar('\n');
	}
}
signed main() {
	//ios::sync_with_stdio(false);
	//cin.tie(0); cout.tie(0);
	int T; readT(T);
	while (T--) {
		solve();
	}
	return 0;
}

详细

Test #1:

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

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: -100
Time Limit Exceeded

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:

127597015
89958266
0
319801028
319801028
255904892
317070839
1265145897
1265145897
644904388
538333404
455103436
285643094
285643094
285643094
134328286
0
785245841
691421476
605409472
479058444
371688030
303203698
387433843
910180170
910180170
646916464
121535083
121535083
121535083
121535083
12153...

result: