HDU 1814 Peaceful Commission(2-SAT)

来源:互联网 发布:网络热点事件2017 编辑:程序博客网 时间:2024/04/30 14:08

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1814
题意:
要建立一个和平委员会,要满足以下条件:
每个党派都在委员会中恰有1个代表,
如果2个代表彼此厌恶,则他们不能都属于委员会

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<algorithm>#include<queue>#include<stack>#include<vector>#include<map>#include<set>#include<list>#define mem(x,y)memset(x,y,sizeof(x))#define max(a,b)(a)>(b)?(a):(b)#define min(a,b)(a)<(b)?(a):(b)#define eps 1e-10#define M 100000007#define INF 0x1f1f1f1fusing namespace std;typedef long long int LL;typedef __int64 ll;const int maxn=100005;const int MAXN = 20020;const int MAXM = 100010;struct Edge{    int to,next;} edge[MAXM];int head[MAXN],tot;void init(){    tot = 0;    memset(head,-1,sizeof(head));}void addedge(int u,int v){    edge[tot].to = v;    edge[tot].next = head[u];    head[u] = tot++;}bool vis[MAXN];//染色标记,为true表示选择int S[MAXN],top;//栈bool dfs(int u){    if(vis[u^1])return false;    if(vis[u])return true;    vis[u] = true;    S[top++] = u;    for(int i = head[u]; i != -1; i = edge[i].next)        if(!dfs(edge[i].to))            return false;    return true;}bool Twosat(int n){    memset(vis,false,sizeof(vis));    for(int i = 0; i < n; i += 2)    {        if(vis[i] || vis[i^1])continue;        top = 0;        if(!dfs(i))        {            while(top)vis[S[--top]] = false;            if(!dfs(i^1)) return false;        }    }    return true;}int main(){    int n,m;    int u,v;    while(scanf("%d%d",&n,&m) == 2)    {        init();        while(m--)        {            scanf("%d%d",&u,&v);            u--;            v--;            addedge(u,v^1);            addedge(v,u^1);        }        if(Twosat(2*n))        {            for(int i = 0; i < 2*n; i++)                if(vis[i])                    printf("%d\n",i+1);        }        else printf("NIE\n");    }    return 0;}
0 0
原创粉丝点击