QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#191067#7103. Red Black TreeGeospizaWA 1888ms41900kbC++202.3kb2023-09-29 17:24:472023-09-29 17:24:47

Judging History

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

  • [2023-09-29 17:24:47]
  • 评测
  • 测评结果:WA
  • 用时:1888ms
  • 内存:41900kb
  • [2023-09-29 17:24:47]
  • 提交

answer

//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
	int T=1;
	cin>>T;
	while(T--){
		ll n,m,q;
		cin>>n>>m>>q;
		vector<ll>color(n+5),dis(n+5),dep(n+5),des(n+5);
		for(int i=1;i<=m;i++){
			int r; cin>>r;
			color[r]=1;
			dis[r]=0;
		}
		vector<vector<pll>>ed(n+5);
		vector<vector<ll>>fa(n+5,vector<ll>(20));
		for(int i=1;i<n;i++){
			ll u,v,w;
			cin>>u>>v>>w;
			ed[u].push_back({v,w});
			ed[v].push_back({u,w});
		}
		function<void(ll)>dfs1=[&](int u){
			for(int i=1;i<=19;i++)
				fa[u][i]=fa[fa[u][i-1]][i-1];
			for(auto [v,w]:ed[u]){
				if(v==fa[u][0])
					continue;
				fa[v][0]=u,dep[v]=dep[u]+1,des[v]=des[u]+w;
				dfs1(v);
			}
		};
		auto lca=[&](int u,int v)->ll
		{
			if(dep[u]<dep[v])
				swap(u,v);
			for(int i=19;i>=0;i--)
				if(dep[fa[u][i]]>=dep[v])
					u=fa[u][i];
			if(u==v)
				return u;
			for(int i=19;i>=0;i--){
				if(fa[u][i]!=fa[v][i])
					u=fa[u][i],v=fa[v][i];
			}
			return fa[u][0];
		};
		auto dist=[&](int u,int v)->ll
		{
			return des[u]+des[v]-2ll*des[lca(u,v)];
		};
		fa[1][0]=0;
		dep[1]=1;
		dfs1(1);
		function<void(ll fa,ll u,ll now)>dfs=[&](ll fa,ll u,ll now)
		{
			if(color[u]==1){
				now=0;
			}
			dis[u]=now;
			for(auto [v,w]:ed[u]){
				if(v==fa)
					continue;
				dfs(u,v,now+w);
			}
		};
		dfs(0,1,0);

		while(q--){
			ll k;
			cin>>k;
			vector<ll>v(k+5);
			vector<pll>val(k+5);
			for(int i=1;i<=k;i++){
				cin>>v[i];
				val[i]={dis[v[i]],v[i]};
			}
			
			sort(val.begin()+1,val.begin()+1+k,[&](pll x,pll y){
				return x.first>y.first;
			});

			ll ans=val[1].first,p=val[1].second;
			for(int l=1;l<=k;l++){
				//cout<<l<<"\n";
				p=lca(val[l].second,p);
				while(l+1<=k&&lca(val[l+1].second,p)==p){
					//cout<<l<<endl;
					l++;
				}
				ll now=min(val[1].first,dist(p,val[1].second));
				ans=min(ans,max(now,val[l+1].first));
			}
			cout<<ans<<"\n";
		}
	}
}
/*

2
12 2 5
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
1 1
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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1888ms
memory: 41900kb

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 691st lines differ - expected: '549790714', found: '374667774'