小样例过了,大样例手推不出来。求一个简单的hack
#include<bits/stdc++.h>
#define _F(x,y,z) for(int x=y;x<=z;x++)
#define F_(x,y,z) for(int x=y;x>=z;x--)
using namespace std;
typedef const int ci;
typedef long long ll;
typedef double dou;
ci maxn=1e5+10;
int n,m,t,c,res,ans=0;
int tot,head[maxn],to[maxn],nex[maxn],vl[maxn];
int v1[maxn],v2[maxn],fa[maxn],root[maxn],fav[maxn],cnt,val[maxn],siz[maxn];
int opt[maxn],f;
void add(int x,int y,int z)
{
to[++tot]=y;
nex[tot]=head[x];
head[x]=tot;
vl[tot]=z;
}
bool dfs1(int x,int tg)
{
if(v1[x]==tg)
{
res=f=0;
return 1;
}
v1[x]=tg;
bool v=0;
for(int i=head[x];i;i=nex[i])
if(dfs1(to[i],tg))
{
v=1;
res^=vl[i];
if(val[to[i]]<=1)
f=1;
// printf("%d %d %d\n",val,res,to[i]);
}
return v;
}
void dfs2(int x,int tg,int lst)
{
if(v2[x]==tg)return ;
v2[x]=tg;
if(val[x]>1)
val[x]=lst;
for(int i=head[x];i;i=nex[i])
dfs2(to[i],tg,val[x]);
return ;
}
int dfs3(int x,int k)
{
int y=0;
if(v1[x]&&x==root[k])return 0;
v1[x]=1;
if(val[x]==-1||opt[k])
y++,printf("%d ",x);
for(int i=head[x];i;i=nex[i])
y+=dfs3(to[i],k);
// printf("%d %d\n",x,y);
return y;
}
int main()
{
freopen("out.txt","w",stdout);
scanf("%d%d",&c,&t);
while(t--)
{
tot=cnt=ans=0;
memset(head,0,sizeof head);
scanf("%d%d",&n,&m);
memset(v1,0,sizeof v1);
memset(fa,0,sizeof fa);
memset(root,0,sizeof root);
memset(val,0x3f,sizeof val);
memset(siz,0,sizeof siz);
memset(v2,0,sizeof v2);
memset(opt,0,sizeof opt);
memset(vl,0,sizeof vl);
_F(i,1,m)
{
char op[3];
int x,y;
scanf("%s",op+1);
if(op[1]=='T'||op[1]=='F'||op[1]=='U')
{
scanf("%d",&x);
fa[x]=0;
if(op[1]=='U')
val[x]=-1;
if(op[1]=='T')
val[x]=1;
if(op[1]=='F')
val[x]=0;
}
else
{
scanf("%d%d",&x,&y);
fa[x]=y;
fav[x]=(op[1]=='-');
if(val[y]>1){ val[x]=114514;continue;}
if(val[y]==-1)
val[x]=val[y];
else if(op[1]=='-')
val[x]=!val[y];
else
val[x]=val[y];
}
}
// puts("<***********debug************>");
_F(i,1,n)
{
if(fa[i])
{
add(fa[i],i,fav[i]);
// printf("%d %d\n",fa[i],i);
}
}
_F(i,1,n)
{
res=f=0;
if(!v1[i]&&dfs1(i,i))
{
cnt++,root[cnt]=i;
opt[cnt]|=res&&(!f);
dfs2(i,i,0);
}
}
_F(i,1,n)
{
res=f=0;
if(!fa[i])
{
cnt++,root[cnt]=i;
dfs1(i,i),dfs2(i,i,0);
}
}
// _F(i,1,n)
// if(fa[i])
// add(i,fa[i]);
_F(i,1,cnt)
{
v1[root[i]]=0;
ans+=dfs3(root[i],i);
printf("\n");
}
// puts("<***********debug************>");
printf("%d\n",ans);
}
return 0;
}