求条
查看原帖
求条
1246763
ny_WYR楼主2025/1/31 20:19

tarjantarjan缩点+树剖WA 90pts90pts,求助。
提交记录

#include<bits/stdc++.h>
using namespace std;
int const N=2e4+5;
int const M=1e5+5;
int h[N],h2[N];
struct edge{
	int t,next;
}e[M<<1],e2[M<<1];
int n,m,idx,q;
void init(int x,int h[]){
	for(int i=1;i<=x;i++) h[i]=-1;
}
void add(int f,int t,edge e[],int h[]){
	e[idx].t=t;
	e[idx].next=h[f];
	h[f]=idx++;
}
int dfn[N],low[N],tot;
int stk[N],tp;
int id[N];
vector<int> dcc[N];
bool cut[N];
int cnt;
int root;
int res;
void tarjan(int u){
	dfn[u]=low[u]=++tot;
	stk[++tp]=u;
	int son=0;
	for(int i=h[u];~i;i=e[i].next){
		int v=e[i].t;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
			if(low[v]>=dfn[u]){
				son++;
				if((u!=root||son>1)&&!cut[u]){
					cut[u]=true;
					res++;
				}
				cnt++;
				while(1){
					int p=stk[tp--];
					dcc[cnt].push_back(p);
					if(p==v) break;
				}
				dcc[cnt].push_back(u);
			}
		}
		else low[u]=min(low[u],dfn[v]);
	}
}
int sz[N],dep[N],fa[N],son[N],top[N];

void dfs(int u,int p){
	fa[u]=p;
	sz[u]=1;
	dep[u]=dep[p]+1;
	for(int i=h2[u];~i;i=e2[i].next){
		int v=e2[i].t;
		if(v==p) continue;
		dfs(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[son[u]]){
			son[u]=v;
		}
	}
}
void DFS(int u,int p,int tp){
	dfn[u]=++tot;
	top[u]=tp;
	if(son[u]) DFS(son[u],u,tp);
	for(int i=h2[u];~i;i=e2[i].next){
		int v=e2[i].t;
		if(v==p||v==son[u]) continue;
		DFS(v,u,v);
	}
}
int c[N];
int lowbit(int x){
	return x&(-x);
}
void update(int x,int k){
	for(int i=x;i<=n;i+=lowbit(i))
		c[i]+=k;
}
int getsum(int x){
	int sum=0;
	for(int i=x;i>0;i-=lowbit(i)){
		sum+=c[i];
	}
	return sum;
}
void modify(int x,int y,int d){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		update(dfn[top[x]],d);
		update(dfn[x]+1,-d);
		x=fa[top[x]];
	}
	if(dep[x]<dep[y]) swap(x,y);
	update(dfn[y],d);
	update(dfn[x]+1,-d);
}
signed main(){
	scanf("%d%d",&n,&m);
	init(n,h);
	for(int i=1;i<=m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v,e,h);
		add(v,u,e,h);
	}
	for(int i=1;i<=n;i++){
		if(!dfn[i]){
			root=i;
			tarjan(i);
		}
	}
	int num=cnt;
	init(cnt+res,h2);
	idx=0;
	for(int i=1;i<=cnt;i++){
		for(auto v:dcc[i]){
			if(cut[v]){
				if(!id[v]) id[v]=++num;
				add(id[v],i,e2,h2);
				add(i,id[v],e2,h2);
			}
			else id[v]=i;
		}
	}
	tot=0;
	memset(dfn,0,sizeof dfn);
	dfs(1,0);
	DFS(1,0,1);
	scanf("%d",&q);
	for(int i=1;i<=q;i++){
		int x,y,f;
		scanf("%d%d%d",&x,&y,&f);
		x=id[x]; y=id[y];
		modify(x,y,1);
		if(getsum(dfn[id[f]])&&cut[f]) printf("yes\n");
		else printf("no\n");
		modify(x,y,-1);
	}
	return 0;
}
2025/1/31 20:19
加载中...