1823: [JSOI2010]满汉全席

题目链接

题目大意:n个条件,选a||b,判断可行性

题解:2-sat

我的收获:第一个2-sat……本题只需要求可行性,tarjan后判断一下就好了
Orz

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int M=1005;

int T,n,m,t;
int scnt,tim,tp;
int head[M],dfn[M],low[M],st[M],col[M];
bool ins[M];

struct edge{int to,nex;}e[M*4];
void add(int u,int v){e[t].to=v,e[t].nex=head[u],head[u]=t++;}

int get(){
    int p;char c;
    for(c=getchar();c!='m'&&c!='h';c=getchar());
    scanf("%d",&p);p*=2;
    return c=='m'?p:p+1;
}

void tarjan(int x){
    int now=0;
    dfn[x]=low[x]=++tim;
    st[++tp]=x;ins[x]=1;
    for(int i=head[x];i!=-1;i=e[i].nex){
        int v=e[i].to;
        if(!dfn[v]){tarjan(v);if(low[v]<low[x]) low[x]=low[v];}
        else if(ins[v]&&dfn[v]<low[x]) low[x]=dfn[v];
    }
    if(low[x]==dfn[x]){
        scnt++;
        while(x!=now){
            now=st[tp--];
            ins[now]=0;col[now]=scnt;
        }
    }
}

void work()
{
    for(int i=1;i<=n*2+1;i++) 
        if(!dfn[i]) tarjan(i);
    for(int i=1;i<=n;i++)
        if(col[i*2]==col[i*2+1])
            {puts("BAD");return ;}
    puts("GOOD");
}

void init()
{
    int x,y,xh,yj;
    t=0;memset(head,-1,sizeof(head));
    for(int i=1;i<=M;i++) dfn[i]=low[i]=col[i]=0;
    tim=scnt=tp=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        x=get();y=get();
        xh=x^1,yj=y^1;
        add(xh,y);add(yj,x);
    }
}

int main()
{
    cin>>T;
    while(T--){
        init();
        work();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值