WA20求助
查看原帖
WA20求助
240060
林兔兔楼主2021/4/7 18:56
#include<bits/stdc++.h>
using namespace std;
const int N=2005,M=8005;
int n,m,scc[N],sc,dfn[N],low[N],stk[N],top;
int head[N],to[M],nex[M],tot,cnt;
bool vis[N];
struct edge{
	int u,v;
}e[M<<1];
inline int read()
{
    int x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x;
}
inline void add(int x,int y){
	nex[++tot]=head[x];
	head[x]=tot;
	to[tot]=y;
	e[tot]=(edge){x,y};
}
void tarjan(int x){
	int y;
	dfn[x]=low[x]=++cnt;
	stk[++top]=x;
	for(register int i=head[x];i;i=nex[i]){
		y=to[i];
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}else if(!scc[y]) low[x]=min(low[x],dfn[y]);
	}
	if(low[x]==dfn[x]){
		y=-1;
		++sc;
		while(x!=y&&top){
			y=stk[top--];
			scc[y]=sc;
		}
	}
	return;
}
void dfs(int x){
	int y;
	vis[x]=1;
	for(register int i=head[x];i;i=nex[i]){
		y=to[i];
		if(vis[y]) continue;
		dfs(y);
	}
}
bool find(int x){
	x=scc[x];
	memset(vis,0,sizeof(vis));
	dfs(x);
	for(register int i=1;i<=n;++i) if(vis[scc[i]]&&vis[scc[i+n]]) return 0;
	return 1;
}
int main(){
	scanf("%d%d",&n,&m);
	int x,y,a,b;char ch=getchar();
	char ch1[10],ch2[10];
	for(register int i=1;i<=m;++i){
		a=0,b=0;
		scanf("%d%s%d%s",&x,ch1,&y,ch2);
		if(ch1[0]=='Y') a=1;
		if(ch2[0]=='Y') b=1;
		add(x+(a^1)*n,y+b*n);
		add(y+(b^1)*n,x+a*n);
	}
	for(register int i=1;i<=2*n;++i) if(!dfn[i]) tarjan(i);
	tot=0,memset(head,0,sizeof(head));
	for(register int i=1;i<=m;++i){
		x=scc[e[i].u],y=scc[e[i].v];
		if(x==y) continue;
		add(x,y);
	}
	for(register int i=1;i<=n;++i){
		x=find(i),y=find(i+n);
		if(!x&&y)      cout<<'Y';
		else if(!y&&x) cout<<'N';
		else           cout<<'?';
	}
	return 0;
}
2021/4/7 18:56
加载中...