QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#218938#7103. Red Black Treeeuphria#WA 1685ms29004kbC++202.6kb2023-10-18 20:53:312023-10-18 20:53:32

Judging History

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

  • [2023-10-18 20:53:32]
  • 评测
  • 测评结果:WA
  • 用时:1685ms
  • 内存:29004kb
  • [2023-10-18 20:53:31]
  • 提交

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 + 1);
	for(int i = 1; i <= m; i++){
		int x; cin >> x;
		bac[x] = 1;
	}
	vector<vector<pair<int, ll>>> g(n + 1);
	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 + 1));
	vector<ll> val(n + 1), dfn(n + 1), deep(n + 1), dis(n + 1);
	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';
	while(q--){
		int k; cin >> k;
		vector<int> s(k + 1);
		ll l = 0, r = 0;
		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]]);
			});
		if(k == 1){
			cout << 0 << '\n';
		}
		else{
			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 << '\n';
				int lca = LCA(nw, s[i]);
				if(deep[nw] > deep[lca]){
					mx = mx + dis[nw] - dis[lca];
				}
				nw = lca;
				mx = max(mx, min(dis[s[i]] - dis[nw], dis[s[i]] - dis[val[s[i]]]));
				if(i != k) 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;
}

详细

Test #1:

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

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
Wrong Answer
time: 1685ms
memory: 29004kb

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:

wrong answer 38th lines differ - expected: '22932276', found: '38363450'