sub3全WA,其他全A求调
查看原帖
sub3全WA,其他全A求调
521283
wangif424楼主2024/7/22 14:49
#include <bits/stdc++.h>
#define R(x) x = read()
#define ENDL push('\n');
#define SPACE push(' ');
using namespace std;
char pbuf[1<<20], *pp=pbuf;
inline void push(const char &c) {
	if(pp - pbuf == 1<<20)fwrite(pbuf, 1, 1<<20, stdout),pp = pbuf;
	*pp++ = c;
}
class io {public:~io() {fwrite(pbuf, 1, pp - pbuf, stdout);}} _;
inline void write(int x) {
	if (x<0)x=-x,push('-');
	int sta[35],top=0;
	do {
		sta[top++]=x%10,x/=10;
	} while (x);
	while(top)push(sta[--top]^'0');
}
#ifndef LOCAL
	char buf[1<<23],*p1=buf,*p2=buf;
	#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
inline int read() {
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) x=x*10+(ch^48),ch=getchar();
    return x*f;
}
bool Mbe;
constexpr int N=1e5+100;
int T;
int ans;
int n,m;
int c[N];
struct edge{
	int to,nxt;
}v[N<<1];
int len,fir[N];
void add(int x,int y){
	++len;
	v[len]=edge{y,fir[x]};
	fir[x]=len;
}
int dfn[N],low[N],id[N],vis[N];
int cl[N];
int tot,cnt;
stack<int> st;
void dfs(int u){
	dfn[u]=low[u]=++tot;
	st.push(u);
	vis[u]=1;
	for(int i=fir[u];i;i=v[i].nxt){
		if(vis[v[i].to])low[u]=min(low[u],low[v[i].to]);
		else if(!dfn[v[i].to]){
			dfs(v[i].to);
			low[u]=min(low[u],low[v[i].to]);
		}
	}
	if(low[u]==dfn[u]){
		++cnt;
		cl[cnt]=c[u];
		while(st.top()^u){
			if(c[st.top()]^cl[cnt])ans=0;
//			cerr << c[st.top()] << ' ' << c[u] << ' ' << ans <<'\n';
			vis[st.top()]=0;
			id[st.top()]=cnt;
			st.pop();
		}
		vis[u]=0;
		id[u]=cnt;
		st.pop();
	}
}
int x[N<<1],y[N<<1];
int deg[N];
queue<int> q;
int fd[N];
void dfs(int u,int nl){
//	cerr << u << ':' << '\n';
	fd[u]=1;
	for(int i=fir[u];i;i=v[i].nxt)if(!fd[v[i].to])dfs(v[i].to,nl);
}
bool Med;
signed main(){
//	cerr << (&Med-&Mbe)*1.0/1024/1024 << 'M' << '\n';
	R(T);
	while(T--){
		R(n);R(m);
		len=tot=0;
		for(int i=1;i<=n;i++)vis[i]=low[i]=dfn[i]=id[i]=fir[i]=vis[i]=deg[i]=fd[i]=0,cl[i]=-1;
		ans=2;
		for(int i=1;i<=n;i++){
			R(c[i]);
			if(c[i]==1)ans=0;
		}
		if(ans){
			push('B');
			continue;
		}
		for(int i=1;i<=m;i++){
			R(x[i]);
			R(y[i]);
			add(x[i],y[i]);
		}
		ans=-1;cnt=0;
		for(int i=1;i<=n;i++)if(!dfn[i])dfs(i);
		if(!ans){
			push('N');
			continue;
		}
		ans=0;
		len=0;
		for(int i=1;i<=n;i++)fir[i]=0;
		for(int i=1;i<=m;i++){
//			cerr << ':' << id[x[i]] << ' ' << id[y[i]] << '\n';
//			cerr << cl[id[x[i]]] << ' ' << cl[id[y[i]]] << '\n';
			if(id[x[i]]^id[y[i]]){
				add(id[x[i]],id[y[i]]);
				deg[id[y[i]]]++;
			}
		}
		while(!q.empty())q.pop();
		for(int i=1;i<=cnt;i++)if(!deg[i]&&cl[i]!=-1){
			q.push(i);
		}
		while(!q.empty()){
			int u=q.front();
			q.pop();
			if(cl[u]){//cerr<<u<<'\n';
				int t=0;
				dfs(u,0);
				for(int i=1;i<=cnt;i++)if((cl[i]==1&&!fd[i])||(cl[i]==0&&fd[i]))t=i;//cerr<<t;
				if(t)break;
				ans=1;
				break;
			}
			for(int i=fir[u];i;i=v[i].nxt){
				deg[v[i].to]--;
				if(!deg[v[i].to])q.push(v[i].to);
			}
		}
		if(ans){
			push('A');	
			continue;
		}
		if(cnt==2&&cl[1]+cl[2]>=1)ans=2;
		if(ans==2){
			push('B');
		}else push('N');
	}
    return 0;
}
2024/7/22 14:49
加载中...