bzoj 1823: [JSOI2010]满汉全席

时间:2022-11-30 05:07:55
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int t,n,m,head[],next[],v[],cnt,dfn[],cnt1,low[];
int zhan[],zhan1[],top,shu[],shu1;
int du()
{
int a1;
char ch;
ch=getchar();
for(;ch!='m'&&ch!='h';ch=getchar());
scanf("%d",&a1);
if(ch=='m')
return a1*;
else
return a1*-;
}
void jia(int a1,int a2)
{
cnt++;
next[cnt]=head[a1];
head[a1]=cnt;
v[cnt]=a2;
return;
}
void dfs(int a1)
{
cnt1++;
low[a1]=dfn[a1]=cnt1;
zhan[a1]=;
top++;
zhan1[top]=a1;
for(int i=head[a1];i;i=next[i])
if(!dfn[v[i]])
{
dfs(v[i]);
low[a1]=min(low[a1],low[v[i]]);
}
else
if(zhan[v[i]])
low[a1]=min(low[a1],dfn[v[i]]);
if(low[a1]==dfn[a1])
{
shu1++;
for(int i=zhan1[top];i!=a1;top--,i=zhan1[top])
{
shu[i]=shu1;
zhan[i]=;
}
top--;
shu[a1]=shu1;
zhan[a1]=;
}
return;
}
int main()
{
scanf("%d",&t);
for(int i=;i<t;i++)
{
memset(next,,sizeof(next));
shu1=;
top=;
cnt=;
cnt1=;
scanf("%d %d",&n,&m);
for(int i=;i<=*n;i++)
head[i]=dfn[i]=;
for(int j=;j<m;j++)
{
int a1,a2,b1,b2;
a1=du();
b1=du();
if(a1%==)
a2=a1-;
else
a2=a1+;
if(b1%==)
b2=b1-;
else
b2=b1+;
jia(a2,b1);
jia(b2,a1);
} for(int i=;i<=*n;i++)
if(dfn[i]==)
dfs(i);
int kg=;
for(int i=;i<=n;i++)
if(shu[i*]==shu[i*-])
{
kg=;
printf("BAD\n");
break;
}
if(kg)
printf("GOOD\n");
}
return ;
}

2-set问题,根据评委需求建边,比如h1,h2那m2向h1建边,m1向h2建边。求2-set时用tarjin找环。