hdu1814 Peaceful Commission,2-sat

题目大意:一国有n个党派,每个党派在议会中都有2个代表,现要组建和平委员会,要从每个党派在议会的代表中选出1人,一共n人组成和平委员会。已知有一些代表之间存在仇恨,也就是说他们不能同时被选为和平委员会的成员,现要你判断满足要求的和平委员会能否创立?如果能,请任意给出一种方案。

2-sat问题

#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;
}
时间: 2024-10-11 01:56:53

hdu1814 Peaceful Commission,2-sat的相关文章

hdu1814 Peaceful Commission

hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int N = 2e4+5,

HDU 1814 Peaceful Commission

Peaceful Commission Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 181464-bit integer IO format: %I64d      Java class name: Main The Public Peace Commission should be legislated in Parliament of The Democra

hdu 1814 Peaceful Commission (2-sat 输出字典序最小路径)

Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1948    Accepted Submission(s): 560 Problem Description The Public Peace Commission should be legislated in Parliament of Th

hdoj1814 Peaceful Commission【2-set】

题目:hdoj1814 Peaceful Commission 讲解:这里 这是这个题目要输出字典序最小的解,刚好第一种暴力的解法输出来的就是原题目的解,因为每次染色的时候先染字典序小的,所以肯定对. AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <cmath> #include <stack> #in

HDU 1814 Peaceful Commission(2-sat)

HDU 1814 Peaceful Commission 题目链接 题意: 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条件: 每个党派都在委员会中恰有1个代表, 如果2个代表彼此厌恶,则他们不能都属于委员会. 每个党在议会中有2个代表.代表从1编号到2n. 编号为2i-1和2i的代表属于第I个党派. 任务 写一程序: 从文本文件读入党派的数量和关系不友好的代表对, 计算决

HDOJ 1814 Peaceful Commission

经典2sat裸题,dfs的2sat能够方便输出字典序最小的解... Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1578    Accepted Submission(s): 406 Problem Description The Public Peace Commission should

HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is t

HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is t

【HDU】1814 Peaceful Commission

http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:n个2人组,编号分别为2n和2n+1,每个组选一个人出来,且给出m条关系(x,y)使得选了x就不能选y,问是否能从每个组选出1人.且输出字典序最小的答案.(n<=8000, m<=20000) #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #in