zoukankan      html  css  js  c++  java
  • hdu3062(two-sat)

    传送门:Party

    题意:有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?

    分析:two-sat裸题,对于有仇恨的两对夫妻u,v,连边u->v'和v->u';建好图直接tarjan缩点判断每对夫妻[i,i']是否属于一个强连通块内即可。

    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 100000000
    #define inf 0x3f3f3f3f
    #define eps 1e-6
    #define N 2010
    #define FILL(a,b) (memset(a,b,sizeof(a)))
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define PII pair<int,int>
    using namespace std;
    struct edge
    {
        int v,next;
        edge() {}
        edge(int v,int next):v(v),next(next) {}
    } e[N*N/2];
    int n,m,scc,step,top,tot;
    int head[N],dfn[N],low[N],belong[N],Stack[N];
    bool instack[N];
    void init()
    {
        tot=0;step=0;
        scc=0;top=0;
        FILL(head,-1);
        FILL(dfn,0);
        FILL(low,0);
        FILL(instack,false);
    }
    void addedge(int u,int v)
    {
        e[tot]=edge(v,head[u]);
        head[u]=tot++;
    }
    void tarjan(int u)
    {
        int v;
        dfn[u]=low[u]=++step;
        Stack[top++]=u;
        instack[u]=true;
        for(int i=head[u]; ~i; i=e[i].next)
        {
            v=e[i].v;
            if(!dfn[v])
            {
                tarjan(v);
                low[u]=min(low[u],low[v]);
            }
            else if(instack[v])
            {
                low[u]=min(low[u],dfn[v]);
            }
        }
        if(dfn[u]==low[u])
        {
            scc++;
            do
            {
                v=Stack[--top];
                instack[v]=false;
                belong[v]=scc;
            }
            while(v!=u);
        }
    }
    
    void solve()
    {
        for(int i=0; i<2*n; i++)
            if(!dfn[i])tarjan(i);
        bool flag=true;
        for(int i=0; i<n; i++)
        {
            if(belong[i<<1]==belong[i<<1^1])
            {
                flag=false;
                break;
            }
        }
        if(flag)puts("YES");
        else puts("NO");
    }
    int main()
    {
        int a,b,u,v;
        while(scanf("%d%d",&n,&m)>0)
        {
            init();
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d%d",&a,&b,&u,&v);
                addedge(2*a+u,2*b+v^1);
                addedge(2*b+v,2*a+u^1);
            }
    
            solve();
        }
    }
    View Code
  • 相关阅读:
    NOI2017 退役记
    bzoj2590[Usaco2012 Feb]Cow Coupons
    bzoj2215[POI2011]Conspiracy
    bzoj2115[WC2011]Xor
    bzoj4754[JSOI2016]独特的树叶
    SQL外连接与条件 left outer join + WHERE/AND 区别
    IBM Datastage
    无法在MS SQL SERVER MANAGEMENT中打开SSIS实例-解决办法
    SSIS 教学教程
    SQL SERVER 2019 电子书下载
  • 原文地址:https://www.cnblogs.com/lienus/p/4283845.html
Copyright © 2011-2022 走看看