Hdu1814 peaceful Commission, 2-Sat

Source: Internet
Author: User


Question:One country has n parties, each of which has two representatives in the Parliament. Now we want to form a Peace Committee. We want to select one from the representatives of each party in the Parliament, A total of n members form the Peace Committee. It is known that there is hatred between some representatives. That is to say, they cannot be elected as members of the Peace Commission at the same time. Do you want to determine whether a peace commission meeting the requirements can be created? If yes, please provide any solution.


2-SAT problems


#include <cstdio>#include <cstring>#include <vector>#include <iostream>#include <algorithm>using namespace std;const int maxn = 10005;int n, m;vector<int> G[maxn*2];bool mark[maxn*2];int S[maxn*2], top;bool dfs(int x){    if(mark[x^1]) return false;    if(mark[x]) return true;    mark[x] = true;    S[top++] = x;    for(int i=0; i<G[x].size(); ++i)        if(!dfs(G[x][i])) return false;    return true;}bool TwoSat(){    memset(mark, 0, sizeof mark );    for(int i=0; i<n; i += 2) {        if(!mark[i] && !mark[i^1]) {            top = 0;            if(!dfs(i)) {                while(top) mark[S[--top]] = false;                if(!dfs(i^1)) return false;            }        }    }    return true;}int main(){    int u, v;    while(~scanf("%d%d", &n, &m)) {        n *= 2;        for(int i=0; i<n; ++i) G[i].clear();        while(m--) {            scanf("%d%d", &u, &v);            u--;            v--;            G[u].push_back(v^1);            G[v].push_back(u^1);        }        if(TwoSat()) {            for(int i=0; i<n; i+=2)                if(mark[i])                    printf("%d\n", i+1);                else                    printf("%d\n", (i^1)+1);        } else printf("NIE\n");    }    return 0;}


Hdu1814 peaceful Commission, 2-Sat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.