UVALive3713-Astronauts 2-SAT

来源:互联网 发布:网络看电视直播 编辑:程序博客网 时间:2024/06/05 10:31

题目链接:https://vjudge.net/problem/UVALive-3713

题意:有A、B、C 3个任务分配给n个宇航员,其中每个宇航员恰好分配一个任务。假设n个宇航员的平均年龄

为x,只有年龄大于x的才能领取A任务;只有年龄严格小于x的才能领取B任务,而任务C没有限制。有m对宇

航员相互讨厌,因此不能分配同一任务。求出是否能找出符合的任务方案。

解法:

用xi表示第i个宇航员的分配方案。年龄大于等于x的可以选择A(xi = true)和C(xi+1 = false),年龄小雨x的可

以选择B(xi = true)和C(xi+1 = false)。考虑一对互相讨厌的宇航员的话,当不属于同一类时,可以为xi V xj,

即两个之中要有一个为真;当属于同一类时,要用两个语句表示xi V xj、~xi V ~xj,即前者表示一个为true,

后者表示一个为false。

///UVALIVE 3211#include <bits/stdc++.h>using namespace std;const int maxn = 4000010;struct TwoSAT{    int n;    vector<int>G[maxn*2];    bool mark[maxn*2];    int S[maxn*2], c;    bool dfs(int x){        if(mark[x^1]) return false;        if(mark[x]) return true;        mark[x]=1;        S[c++]=x;        for(int i=0; i<G[x].size(); i++){            if(!dfs(G[x][i])) return false;        }        return true;    }    void init(int n){        this->n = n;        for(int i=0; i<n*2; i++) G[i].clear();        memset(mark, 0, sizeof(mark));    }    void addedge(int x, int xval, int y, int yval){        x = x*2+xval;        y = y*2+yval;        G[x^1].push_back(y);        G[y^1].push_back(x);    }    bool solve(){        for(int i=0; i<n*2; i+=2){            if(!mark[i]&&!mark[i+1]){                c=0;                if(!dfs(i)){                    while(c>0) mark[S[--c]]=false;                    if(!dfs(i+1)) return false;                }            }        }        return true;    }}twosat;TwoSAT solver;int n, m, age[maxn], sumage;int check(int x){    return age[x]*n<sumage;}int main(){    while(scanf("%d%d",&n,&m)!=EOF){        if(n==0&&m==0) break;        sumage=0;        for(int i=0; i<n; i++){            scanf("%d", &age[i]);            sumage+=age[i];        }        solver.init(n);        for(int i=0; i<m; i++){            int a, b;            scanf("%d%d", &a,&b);            a--,b--;            if(a==b) continue;            solver.addedge(a,1,b,1);            if(check(a)==check(b)){                solver.addedge(a,0,b,0);            }        }        if(!solver.solve()){            printf("No solution.\n");        }        else{            for(int i=0; i<n; i++){                if(solver.mark[i*2]) printf("C\n");                else if(check(i)) printf("B\n");                else printf("A\n");            }        }    }    return 0;}
0 0