hdu1824Let's go home

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1824

题意:中文题。

分析:处理清楚题目所说的逻辑关系就是一个基础的2-sat问题了。

代码:

#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef double db;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
const db eps=1e-5;
const int N=6e3+10;
const int M=3e4+10;
const ll MOD=100007;
const int mod=1000000007;
const int MAX=1000000010;
const double pi=acos(-1.0);
struct twosat {
    int n,k,d[N];
    bool mark[N];
    int tot,u[N],v[M],pre[M];
    void init(int a) {
        n=a<<1;tot=0;
        for (int i=0;i<n;i++) u[i]=-1,mark[i]=false;
    }
    void add_Edge(int a,int aval,int b,int bval) {
        a=a*2+aval;b=b*2+bval;
        v[tot]=b;pre[tot]=u[a^1];u[a^1]=tot++;
        v[tot]=a;pre[tot]=u[b^1];u[b^1]=tot++;
    }
    bool dfs(int a) {
        if (mark[a]) return true;
        if (mark[a^1]) return false;
        mark[a]=true;d[k++]=a;
        for (int i=u[a];~i;i=pre[i])
        if (!dfs(v[i])) return false;
        return true;
    }
    bool solve() {
        int i,j;k=0;
        for (i=0;i<n;i+=2)
        if (!mark[i]&&!mark[i^1]) {
            if (!dfs(i)) {
                for (j=0;j<k;j++) mark[d[j]]=false;
                k=0;if (!dfs(i^1)) return false;
            }
        }
        return true;
    }
}TS;
int main()
{
    int a,b,c,i,t,m;
    while (~scanf("%d%d", &t, &m)) {
        TS.init(3*t);
        for (i=1;i<=t;i++) {
            scanf("%d%d%d", &a, &b, &c);
            TS.add_Edge(a,0,b,0);TS.add_Edge(a,0,c,0);
        }
        for (i=1;i<=m;i++) scanf("%d%d", &a, &b),TS.add_Edge(a,1,b,1);
        if (TS.solve()) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值