QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#242993#7103. Red Black TreexuxubaobaoWA 754ms73020kbC++172.2kb2023-11-07 19:28:232023-11-07 19:28:23

Judging History

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

  • [2023-11-07 19:28:23]
  • 评测
  • 测评结果:WA
  • 用时:754ms
  • 内存:73020kb
  • [2023-11-07 19:28:23]
  • 提交

answer

#include<bits/stdc++.h>
#define endl '\n'
#define LL long long
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
const int N=1e6+100,MOD=998244353;
int idx=0,dfn[N],dep[N],que[N],f[N][30],lg[N],n,m,s;
vector<array<int,2>> e[N];
int ne[N];
int dis[N];
int vis[N];
int id[N];

void dfs(int u,int pa) {
	dfn[u]=++idx,que[idx] = u;
	dep[u]=dep[pa] + 1;
	if(vis[u]) ne[u] = 0;
	for(auto [v, w] :e[u]){
		if(v==pa) continue;
		dis[v] = dis[u] + w;
		ne[v] = ne[u] + w;
		dfs(v,u);
		que[++idx] = u;
	}
}

void buildst(){
	for(int i=1;i<=idx;i++) f[i][0] = que[i];
	for(int j=1;j<=20;j++){
		for(int i=1;i+(1<<j)<=idx;++i){
			int f1=f[i][j-1],f2=f[i+(1<<j-1)][j-1];
			f[i][j]=dep[f1]<dep[f2]?f1:f2;
		}
	}
	lg[0]=-1;
	for(int i=1;i<=idx;i++)lg[i]=lg[i>>1]+1;
}
int getlca(int u,int v){
	if(dfn[u]>dfn[v])swap(u,v);
	u=dfn[u],v=dfn[v];
	int kk=lg[v-u+1];
	int f1=f[u][kk],f2=f[v-(1<<kk)+1][kk];
	return dep[f1]<dep[f2]?f1:f2;
}

void solve() {
	int n, m, q;
	cin >> n >> m >> q;
	for(int i = 1; i <= n; i ++) vis[i] = 0;
	for(int i = 1; i <= n; i ++) e[i].clear();
	idx = 0;
	for(int i = 1; i <= m; i ++) { 
		int x;
		cin >> x;
		vis[x] ++;
	}
	for(int i = 1; i < n; i ++) {
		int u, v, w;
		cin >> u >> v >> w;
		e[u].push_back({v, w});
		e[v].push_back({u, w});
	}
	dfs(1, 0);
	buildst();
	for(int i = 1; i <= q; i ++) {
		int p;
		cin >> p;
		vector<int> vx;
		for(int j = 1; j <= p; j ++) {
			int x;
			cin >> x;
			vx.push_back(x);
		}
		int l = 0, r = 1 << 30;
		while(r > l) {
			int mid = (l + r) >> 1;
			int ok = 1;
			int mx = -1, mi = 1 << 29;
			int cnt = 0;
			for(auto v : vx) {
				if(ne[v] <= mid) continue;
				mi = min(mi, dfn[v]);
				mx = max(mx, dfn[v]);
				cnt ++;
			}
			if(cnt >= 2) {
				int u = getlca(que[mx], que[mi]);
				for(auto v : vx) {
					if(ne[v] <= mid) continue;
					if(dis[v] - dis[u] > mid) ok = 0;
				}
			}
			if(ok) r = mid;
			else l = mid + 1;
		}
		cout << l << "\n";
	}
}

int main() {
	ios;
	int t;
	t = 1;
	cin >> t;
	while(t --){
		solve();
	}
}

/*
2
12 2 1
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
4 7 8 10 11
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 754ms
memory: 73020kb

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
1073741824
1073741824
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 8th lines differ - expected: '1265145897', found: '1073741824'