• POJ 3905 Perfect Election (2-Sat)


    Perfect Election
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 438   Accepted: 223

    Description

    In a country (my memory fails to say which), the candidates {1, 2 ..., N} are running in the parliamentary election. An opinion poll asks the question "For any two candidates of your own choice, which election result would make you happy?". The accepted answers are shown in the table below, where the candidates i and j are not necessarily different, i.e. it may happen that i=j. There are M poll answers, some of which may be similar or identical. The problem is to decide whether there can be an election outcome (It may happen that all candidates fail to be elected, or all are elected, or only a part of them are elected. All these are acceptable election outcomes.) that conforms to all M answers. We say that such an election outcome is perfect. The result of the problem is 1 if a perfect election outcome does exist and 0 otherwise.

    Input

    Each data set corresponds to an instance of the problem and starts with two integral numbers: 1≤N≤1000 and 1≤M≤1000000. The data set continues with M pairs ±i ±j of signed numbers, 1≤i,j≤N. Each pair encodes a poll answer as follows: 

    Accepted answers to the poll question Encoding
    I would be happy if at least one from i and j is elected. +i +j
    I would be happy if at least one from i and j is not elected. -i -j
    I would be happy if i is elected or j is not elected or both events happen. +i -j
    I would be happy if i is not elected or j is elected or both events happen. -i +j

    The input data are separated by white spaces, terminate with an end of file, and are correct.

    Output

    For each data set the program prints the result of the encoded election problem. The result, 1 or 0, is printed on the standard output from the beginning of a line. There must be no empty lines on output.

    Sample Input

    3 3  +1 +2  -1 +2  -1 -3 
    2 3  -1 +2  -1 -2  +1 -2 
    2 4  -1 +2  -1 -2  +1 -2  +1 +2 
    2 8  +1 +2  +2 +1  +1 -2  +1 -2  -2 +1  -1 +1  -2 -2  +1 -1

    Sample Output

    1
    1
    0
    1

    Hint

    For the first data set the result of the problem is 1; there are several perfect election outcomes, e.g. 1 is not elected, 2 is elected, 3 is not elected. The result for the second data set is justified by the perfect election outcome: 1 is not elected, 2 is not elected. The result for the third data set is 0. According to the answers -1 +2 and -1 -2 the candidate 1 must not be elected, whereas the answers +1 -2 and +1 +2 say that candidate 1 must be elected. There is no perfect election outcome. For the fourth data set notice that there are similar or identical poll answers and that some answers mention a single candidate. The result is 1.

    Source

    大致题意:

        有n个候选人,m组要求,每组要求关系到候选人中的两个人,“+i +j”代表i和j中至少有一人被选中,“-i -j”代表i和j中至少有一人不被选中。“+i -j”代表i被选中和j不被选中这两个事件至少发生一个,“-i +j”代表i不被选中和j被选中这两个事件至少发生一个。问是否存在符合所有m项要求的方案存在。
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int VM=5010;
    const int EM=4000010;
    
    struct Edge{
        int to,nxt;
    }edge[EM<<1];
    
    int n,m,cnt,dep,top,atype,head[VM];
    int dfn[VM],low[VM],vis[VM],belong[VM];
    int stack[VM];
    
    void Init(){
        cnt=0,  atype=0,    dep=0,  top=0;
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        memset(low,0,sizeof(low));
        memset(dfn,0,sizeof(dfn));
        memset(belong,0,sizeof(belong));
    }
    
    void addedge(int cu,int cv){
        edge[cnt].to=cv;    edge[cnt].nxt=head[cu];     head[cu]=cnt++;
    }
    
    void Tarjan(int u){
        dfn[u]=low[u]=++dep;
        stack[top++]=u;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(!dfn[v]){
                Tarjan(v);
                low[u]=min(low[u],low[v]);
            }else if(vis[v])
                low[u]=min(low[u],dfn[v]);
        }
        int j;
        if(dfn[u]==low[u]){
            atype++;
            do{
                j=stack[--top];
                belong[j]=atype;
                vis[j]=0;
            }while(u!=j);
        }
    }
    
    int abs(int x){
        return x<0?-x:x;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&m)){
            Init();
            int u,v;
            for(int i=0;i<m;i++){
                scanf("%d%d",&u,&v);
                int a=abs(u),   b=abs(v);
                if(u>0 && v>0){
                    addedge(a+n,b);
                    addedge(b+n,a);
                }
                if(u<0 && v<0){
                    addedge(a,b+n);
                    addedge(b,a+n);
                }
                if(u>0 && v<0){
                    addedge(a+n,b+n);
                    addedge(b,a);
                }
                if(u<0 && v>0){
                    addedge(a,b);
                    addedge(b+n,a+n);
                }
            }
            for(int i=1;i<=2*n;i++)
                if(!dfn[i])
                    Tarjan(i);
            int ans=1;
            for(int i=1;i<=n;i++)
                if(belong[i]==belong[i+n]){
                    ans=0;
                    break;
                }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    mouseover 有一个多次触发的问题
    2019牛客多校第一场 H.XOR
    luoguP4570 [BJWC2011]元素(线性基)
    线性基
    2019牛客多校第七场 E.Find the median
    2019牛客多校第七场
    2019牛客多校第五场
    支配树
    2019牛客多校第四场
    Codeforces 1195E OpenStreetMap(单调队列)
  • 原文地址:https://www.cnblogs.com/jackge/p/3180865.html
Copyright © 2020-2023  润新知