hdu1814 Peaceful Commission

hdu1814 Peaceful Commission

题意:2-sat裸题,打印字典序最小的



我写了三个

  1. 染色做法,正解
  2. scc做法,不管字典序
  3. scc做法,错误的字典序贪心
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e4+5, M = 1e5+5;

inline int read() {
    int x = 0, f = 1; char c = getchar();
    while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
    while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
    return x * f;
}

int n, m;
struct edge {int v, ne;} e[M];
int cnt, h[N];
inline void ins(int u, int v) {
    e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
}
inline int id(int x) {return ((x-1)^1)+1;}
int col[N], st[N], top;
bool dfs(int u) {
    if(col[u]) return true;
    if(col[id(u)]) return false;
    col[u] = 1; st[top++] = u;
    for(int i=h[u]; i; i=e[i].ne) {
        int v = e[i].v;
        if(!dfs(v)) return false;
    }
    return true;
}
bool check(int u) {
    top = 1;
    return dfs(u);
}
int main() {
    freopen("in", "r", stdin);
    while(cin >> n) {
        cnt = 0;
        memset(h, 0, sizeof(h));
        memset(col, 0, sizeof(col));
        m = read();
        for(int i=1; i<=m; i++) {
            int a = read(), b = read();
            ins(a, id(b));
            ins(b, id(a));
        }
        int flag =  0;
        for(int i=1; i<=n<<1; i+=2) if(!col[i] && !col[id(i)]) {
            if(!check(i)) {
                while(top) col[ st[top--] ] = 0;
                //for(int i=1; i<=n; i++) printf("col %d %d\n", i, col[i]);
                if(!check(id(i))) {
                    puts("NIE"), flag = 1;
                    break;
                }
            }
        }
        if(flag) continue;
        for(int i=1; i<=n<<1; i+=2) {
            if(col[i]) printf("%d\n", i);
            else printf("%d\n", i+1);
        }
    }
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e4+5, M = 1e5+5;

inline int read() {
    int x = 0, f = 1; char c = getchar();
    while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
    while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
    return x * f;
}

int n, m;
struct edge {int v, ne;} e[M];
int cnt, h[N];
inline void ins(int u, int v) {
    e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
}
int dfn[N], dfc, scc, belong[N], low[N];
int st[N], top;
void dfs(int u) {
    dfn[u] = low[u] = ++dfc;
    st[++top] = u;
    for(int i=h[u]; i; i=e[i].ne) {
        int v = e[i].v;
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!belong[v]) low[u] = min(low[u], dfn[v]);
    }
    if(dfn[u] == low[u]) {
        scc++;
        while(true) {
            int x = st[top--];
            belong[x] = scc;
            if(x == u) break;
        }
    }
}
namespace G {
    edge e[M];
    int cnt, h[N], ind[N];
    inline void ins(int u, int v) {
        e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
        ind[v] ++;
    }
    int q[N], head = 1, tail = 1;
#define pii pair<int, int>
#define fir first
#define sec second
    //priority_queue<pii, vector<pii>, greater<pii> > q;
    int col[N], opp[N];
    void dfs_color(int u) {
        if(col[u]) return;
        col[u] = -1;
        for(int i=h[u]; i; i=e[i].ne) dfs_color(e[i].v);
    }
    bool check() {
        for(int i=1; i<=n; i++) if(belong[i] == belong[i+n]) return false;
        return true;
    }
    void topo_sort() {
        head = tail = 1;
        for(int i=1; i<=scc; i++) if(!ind[i]) q[tail++] = i;
        while(head != tail) {
            int u = q[head++]; printf("uuu %d %d\n", u, col[u]);
            if(col[u]) continue;
            col[u] = 1;
            dfs_color(opp[u]);
            for(int i=h[u]; i; i=e[i].ne) {
                int v = e[i].v;
                ind[v] --;
                if(ind[v] == 0) q[tail++] = v;
            }
        }
    }
}
int main() {
    freopen("in", "r", stdin);
    while(cin >> n) {
        m = read();
        for(int i=1; i<=m; i++) {
            int a = read(), b = read();
            ins(a, b+n);
            ins(b, a+n);
        }
        for(int i=1; i<=n<<1; i++) if(!dfn[i]) dfs(i);
        if(!G::check()) {
            puts("NIE");
            continue;
        }
        for(int u=1; u<=n<<1; u++) {
            int a = belong[u];
            for(int i=h[u]; i; i=e[i].ne) {
                int b = belong[e[i].v];
                if(a != b) ins(b, a);
            }
        }
        for(int i=1; i<=n; i++) {
            int a = belong[2*i-1], b = belong[2*i];
            G::opp[a] = b;
            G::opp[b] = a;
            printf("hi %d   %d %d\n", i, belong[a], belong[b]);
        }
        G::topo_sort();
        for(int i=1; i<=n<<1; i++)
            if(G::col[belong[i]] == 1) printf("%d\n", i);
    }
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 2e4+5, M = 1e5+5;

inline int read() {
    int x = 0, f = 1; char c = getchar();
    while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
    while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
    return x * f;
}

int n, m;
struct edge {int v, ne;} e[M];
int cnt, h[N];
inline void ins(int u, int v) {
    e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
}
int dfn[N], dfc, scc, belong[N], low[N];
int st[N], top;
void dfs(int u) { //printf("dfs %d\n", u);
    dfn[u] = low[u] = ++dfc;
    st[++top] = u;
    for(int i=h[u]; i; i=e[i].ne) {
        int v = e[i].v;
        if(!dfn[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
        } else if(!belong[v])
            low[u] = min(low[u], dfn[v]);
    }
    if(dfn[u] == low[u]) {
        scc++;
        while(true) {
            int x = st[top--];
            belong[x] = scc;
            if(x == u) break;
        }
    }
}
inline int id(int x) {
    int t = ((x-1) >> 1) + 1;
    if(x == t<<1) return x-1;
    else return x+1;
}
int mn[N];
namespace G {
    edge e[M];
    int cnt, h[N], ind[N];
    inline void ins(int u, int v) {
        e[++cnt] = (edge) {v, h[u]}; h[u] = cnt;
        ind[v] ++;
    }
    //int q[N], head = 1, tail = 1;
#define pii pair<int, int>
#define fir first
#define sec second
    priority_queue<pii, vector<pii>, greater<pii> > q;
    int col[N], opp[N];
    void dfs_color(int u) {
        if(col[u]) return;
        col[u] = -1;
        for(int i=h[u]; i; i=e[i].ne) dfs_color(e[i].v);
    }
    bool check() {
        for(int i=1; i<=n; i++) if(belong[(i<<1)-1] == belong[i<<1]) return false;
        return true;
    }
    void topo_sort() {
        for(int i=1; i<=scc; i++) if(!ind[i]) q.push(make_pair(mn[i], i));
        while(!q.empty()) {
            int u = q.top().sec; q.pop(); printf("uuu %d  %d\n", u, mn[u]);
            if(col[u]) continue;
            col[u] = 1;
            dfs_color(opp[u]);
            for(int i=h[u]; i; i=e[i].ne) {
                int v = e[i].v;
                ind[v] --;
                if(ind[v] == 0) q.push(make_pair(mn[v], v));
            }
        }
    }
}
int main() {
    freopen("in", "r", stdin);
    while(cin >> n) {
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(belong, 0, sizeof(belong));

        dfc = scc = 0;
        cnt = 0; G::cnt = 0;
        memset(h, 0, sizeof(h));
        memset(G::h, 0, sizeof(G::h));
        memset(G::col, 0, sizeof(G::col));
        memset(G::ind, 0, sizeof(G::ind));
        memset(mn, 0x3f, sizeof(mn));

        m = read();
        for(int i=1; i<=m; i++) {
            int a = read(), b = read();
            ins(a, id(b));
            ins(b, id(a));
            //printf("id %d %d\n", id(a), id(b));
        }
        for(int i=1; i<=n<<1; i++) if(!dfn[i]) dfs(i);
        if(!G::check()) {
            puts("NIE");
            continue;
        }
        for(int u=1; u<=n<<1; u++) {
            int a = belong[u];
            mn[a] = min(mn[a], u);
            for(int i=h[u]; i; i=e[i].ne) {
                int b = belong[e[i].v];
                if(a != b) G::ins(b, a);
            }
        }
        //for(int i=1; i<=n<<1; i++) printf("belong %d %d  %d\n", i, belong[i], mn[belong[i]]);
        for(int i=1; i<=n; i++) {
            int a = belong[(i<<1)-1], b = belong[i<<1];
            G::opp[a] = b;
            G::opp[b] = a;
            //printf("hi %d   %d %d\n", i, belong[a], belong[b]);
        }
        G::topo_sort();
        //for(int i=1; i<=n<<1; i++) printf("col %d %d  %d\n", i, belong[i], G::col[i]);
        for(int i=1; i<=n<<1; i+=2) {
            if(G::col[belong[i]] == 1) printf("%d\n", i);
            else printf("%d\n", id(i));
        }
    }
}

原文地址:https://www.cnblogs.com/candy99/p/9279525.html

时间: 2024-10-12 10:52:56

hdu1814 Peaceful Commission的相关文章

hdu1814 Peaceful Commission,2-sat

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

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

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

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个党派. 任务 写一程序: 从文本文件读入党派的数量和关系不友好的代表对, 计算决

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