QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#218989#7103. Red Black Treeeuphria#AC ✓1436ms29124kbC++202.6kb2023-10-18 21:24:442023-10-18 21:24:45

Judging History

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

  • [2023-10-18 21:24:45]
  • 评测
  • 测评结果:AC
  • 用时:1436ms
  • 内存:29124kb
  • [2023-10-18 21:24:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define endl '\n'
using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
const int N = 1e6 + 10;
const int M = 131;
const int INF = 0x3f3f3f3f;
const long long LNF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;

void solve(){
	int n, m, q; cin >> n >> m >> q;
	vector<int> bac(n + 2);
	for(int i = 1; i <= m; i++){
		int x; cin >> x;
		bac[x] = 1;
	}
	vector<vector<pair<int, ll>>> g(n + 2);
	for(int i = 1; i < n; i++){
		int a, b, c; cin >> a >> b >> c;
		g[a].push_back({b, c});
		g[b].push_back({a, c});
	}
	int idx = 0;
	vector<vector<int>> father(18, vector<int>(n + 2));
	vector<ll> val(n + 2), dfn(n + 2), deep(n + 2), dis(n + 2);
	function <void(int, int, ll)> dfs = [&] (int u, int fa, ll v){
		val[u] = v;
		dfn[u] = ++idx;
		father[0][u] = fa;
		deep[u] = deep[fa] + 1;
		for(int i = 1; i <= 17; i++) father[i][u] = father[i - 1][father[i - 1][u]];
		for(auto [a, b] : g[u]){
			if(a == fa) continue;
			dis[a] = dis[u] + b;
			dfs(a, u, (bac[a] ? a : v));
		}
	};
	dfs(1, 0, 1);
	function<int(int, int)> LCA = [&] (int a, int b){
		if(deep[a] < deep[b]) swap(a, b);
		for(int i = 17; i >= 0; i--){
			if(deep[father[i][a]] >= deep[b]) a = father[i][a];
		}
		if(a == b) return a;
		for(int i = 17; i >= 0; i--){
			if(father[i][a] != father[i][b]){
				a = father[i][a]; b = father[i][b];
			}
		}
		return father[0][a];
	};
	//for(int i = 1; i <= n; i++) cout << "dfn " << i << ' ' << dfn[i] << '\n';
	val[n + 1] = n + 1;
	while(q--){
		int k; cin >> k;
		vector<int> s(k + 1);
		for(int i = 1; i <= k; i++){
			cin >> s[i];
		}
		sort(s.begin() + 1, s.end(), [&](int a, int b){
			return (dis[a] - dis[val[a]]) > (dis[b] - dis[val[b]]);
		});
		s.push_back(n + 1);
		ll nw = s[1], res = min(dis[s[1]] - dis[val[s[1]]], dis[s[2]] - dis[val[s[2]]]), mx = 0;
		//cout << nw << ' ' << res << ' ' << mx << '\n';
		for(int i = 2; i <= k; i++){
			//cout << "i " << i << ' ' << s[i] << '\n';
			int lca = LCA(nw, s[i]);
			if(deep[nw] > deep[lca]){
				mx = mx + dis[nw] - dis[lca];
			}
			mx = max(mx, min(dis[s[i]] - dis[lca], dis[s[i]] - dis[val[s[i]]]));
			nw = lca;
			res = min(res, max(mx, dis[s[i + 1]] - dis[val[s[i + 1]]]));
			//cout << nw << ' ' << res << ' ' << mx << '\n';
		}
		cout << res << '\n';
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout << fixed << setprecision(12);
	int t = 1;
	cin >> t;
	while(t--){
		solve();
	}
	return 0 - 0;
}

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

详细

Test #1:

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

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: 1436ms
memory: 29124kb

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