QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#244984#7103. Red Black TreeSylviallineTL 2ms9668kbC++232.3kb2023-11-09 17:42:142023-11-09 17:42:15

Judging History

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

  • [2023-11-09 17:42:15]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:9668kb
  • [2023-11-09 17:42:14]
  • 提交

answer

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
using ll=long long;
void solve();
int main(){
    cin.tie(nullptr)->ios::sync_with_stdio(false);
	cout.tie(nullptr);
	int t=1;
	cin>>t;
	while(t--)solve();
}

const int N=1e5+3,M=2e5+3;
int to[M],nxt[M],val[M],lst[N],tt;
ll cst[N],dis[N];
int dfn[N],nfd[N],ti;
void add(int u,int v,int w){
	to[++tt]=v;
	val[tt]=w;
	nxt[tt]=lst[u];
	lst[u]=tt;
}
bool isr[N];
int dep[N],fa[N],sz[N],ch[N];
void dfsp(int u){
	dfn[u]=++ti;
	if(isr[u])cst[u]=0;
	for(int i=lst[u];i;i=nxt[i]){
		int v=to[i];
		if(v!=fa[u]){
			dis[v]=dis[u]+val[i];
			cst[v]=cst[u]+val[i];

			fa[v]=u;
			dep[v]=dep[u]+1;
			dfsp(v);
			sz[u]+=sz[v];
			if(sz[ch[u]]<sz[v])ch[u]=v;
		}
	}
	nfd[u]=ti;
}
int top[N];
void dfsc(int u){
	if(!top[u])top[u]=u;
	if(ch[u])top[ch[u]]=top[u];
	for(int i=lst[u];i;i=nxt[i]){
		int v=to[i];
		if(v!=fa[u])dfsc(v);
	}
}
int glca(int x,int y){
	for(;top[x]!=top[y];x=fa[top[x]])if(dep[top[x]]<dep[top[y]])swap(x,y);
	return dep[x]<dep[y]?x:y;
}
struct BIT{
	int b[N],n;
	vector<pair<int,int>>V;
	void modify(int l,int r){
		V.emplace_back(l,r);
		mdf(l,r+1,1);
	}
	void mdf(int l,int r,int d){
		for(;l<r;l+=l&-l)b[l]+=d;
		l=min(l,n+1);
		for(;r<l;r+=r&-r)b[r]-=d;
	}
	int query(int x){
		int ans=0;
		for(;x;x&=x-1)ans+=b[x];
		return ans;
	}
	void clear(){
		while(V.size()){
			auto [l,r]=V.back();
			mdf(l,r+1,-1);
			V.pop_back();
		}
	}
}B;
void clear(int n){
	for(int i=1;i<=n;i++)lst[i]=sz[i]=ch[i]=isr[i]=top[i]=0;
	ti=tt=0;
}
void solve(){
	int n,m,q;
	cin>>n>>m>>q;
	B.n=n;
	clear(n);
	for(int i=1;i<=m;i++){
		int x;cin>>x;
		isr[x]=1;
	}
	for(int i=1;i<n;i++){
		int u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);add(v,u,w);
	}
	dfsp(1);dfsc(1);
	for(int i=1;i<=q;i++){
		int k;cin>>k;
		vector<int>V(k);
		for(auto &p:V)cin>>p;
		if(k==1){
			cout<<0<<endl;
			continue;
		}
		sort(V.begin(),V.end(),[](int x,int y){
			return cst[x]>cst[y];
		});
		int lca=V[0];
		B.modify(dfn[V[0]],nfd[V[0]]);
		ll ans=cst[V[1]];
		for(int j=1;j<k;j++){
			if(B.query(dfn[V[j]]))break;
			B.modify(dfn[V[j]],nfd[V[j]]);
			lca=glca(lca,V[j]);
			if(j+1==k||dis[V[0]]-dis[lca]>=cst[V[j+1]]){
				ans=min(ans,dis[V[0]]-dis[lca]);
				break;
			}
			ans=min(ans,cst[V[j+1]]);
		}
		B.clear();
		cout<<ans<<endl;
	}
}

詳細信息

Test #1:

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

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:

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: