uva1391 - Astronauts 2-SAT

来源:互联网 发布:scada数据库 编辑:程序博客网 时间:2024/06/08 02:18

The Bandulu Space Agency (BSA) has plans for the following three space missions:

  • Mission A: Landing on Ganymede, the largest moon of Jupiter.
  • Mission B: Landing on Callisto, the second largest moon of Jupiter.
  • Mission C: Landing on Titan, the largest moon of Saturn.

Your task is to assign a crew for each mission. BSA has trained a number of excellent astronauts; everyone of them can be assigned to any mission. However, if two astronauts hate each other, then it is not wise to put them on the same mission. Furthermore, Mission A is clearly more prestigious than Mission B; who would like to go to the second largest moon if there is also a mission to the largest one? Therefore, the assignments have to be done in such a way that only young, inexperienced astronauts go to Mission B, and only senior astronauts are assigned to Mission A. An astronaut is consideredyoung if their age is less than the average age of the astronauts and an astronaut issenior if their age is at least the averageage. Every astronaut can be assigned to Mission C, regardless of their age (but you must not assign two astronauts to the same mission if they hate each other).

Input 

The input contains several blocks of test cases. Each case begins with a line containing two integers1$ \le$n$ \le$100000 and 1$ \le$m$ \le$100000. The number n is the number of astronauts. The nextn lines specify the age of the n astronauts; each line contains a single integer number between 0 and 200. The nextm lines contains two integers each, separated by a space. A line containingi and j (1$ \le$i,j$ \le$n) means that thei-th astronaut and the j-th astronaut hate each other.

The input is terminated by a block with n = m = 0.

Output 

For each test case, you have to output n lines, each containing a single letter. This letter is either `A', `B', or `C'. Thei-th line describes which mission the i-th astronaut is assigned to. Astronauts that hate each other should not be assigned to the same mission, only young astronauts should be assigned to Mission B and only senior astronauts should be assigned to Mission A. If there is no such assignment, then output the single line `No solution.' (without quotes).

Sample Input 

16 2021222324252627281011021031041051061071081 23 45 6 7 89 1011 1213 1415 161 102 93 124 115 146 13 7 168 151 121 133 166 150 0

Sample Output 

BCCBCBCBACCACACA

  有A,B,C 3个任务分配给N个人,每个人分配一个,年龄大于等于平均年龄的只能分配A或C,否则只能分配B或C。有M对人相互讨厌,相互讨厌的人不能分配到一样的任务。

  每个人只有2种选择,这就变成了一个2-SAT问题。把年龄大于等于平均年龄的人设为第1类,年龄小于平均年龄的设为第2类。第1类设分配A为真,分配C为假,第2类人分配B为真,C为假。相互讨厌的两个人u,v如果属于同一类,那么就要增加两次边,u真或v真,u假或v假,如果两个人不是同一类,就只需要增加一次边,u真或v真。

  2-SAT在搜索过程中记录选择情况,最后再根据每个人的类型输出对应的答案。

#include<iostream>#include<queue>#include<cstring>#include<cstdio>#include<cmath>#include<set>#include<map>#include<vector>#include<stack>#include<algorithm>#define INF 0x3f3f3f3f#define eps 1e-9#define MAXN 100010#define MAXM 2000010#define MAXNODE 105#define MOD 100000#define SIGMA_SIZE 4typedef long long LL;using namespace std;int T,N,M,sum,age[MAXN],ans[MAXN],type[MAXN];struct TwoSAT{    int n,c,S[MAXN*2];    bool mark[MAXN*2];    vector<int> G[MAXN*2];    void init(int n){        this->n=n;        for(int i=0;i<n*2;i++) G[i].clear();        memset(mark,0,sizeof(mark));    }    bool dfs(int x){        if(mark[x^1]) return false;        if(mark[x]) return true;        mark[x]=true;        S[c++]=x;        int L=G[x].size();        for(int i=0;i<L;i++) if(!dfs(G[x][i])) return false;        return true;    }    void add_clause(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;            }            while(c>0) ans[S[--c]>>1]=S[c]%2;        }        return true;    }}solver;int main(){    freopen("in.txt","r",stdin);    while(scanf("%d%d",&N,&M)!=EOF&&(N||M)){        sum=0;        for(int i=0;i<N;i++){            scanf("%d",&age[i]);            sum+=age[i];        }        for(int i=0;i<N;i++){            if(age[i]*N>=sum) type[i]=1;            else type[i]=2;        }        solver.init(N);        int u,v;        while(M--){            scanf("%d%d",&u,&v);            u--;            v--;            if(type[u]==type[v]){                solver.add_clause(u,1,v,1);                solver.add_clause(u,0,v,0);            }            else solver.add_clause(u,1,v,1);        }        if(!solver.solve()) printf("No solution.\n");        else for(int i=0;i<N;i++){            if(!ans[i]) printf("C\n");            else if(type[i]==1) printf("A\n");            else printf("B\n");        }    }    return 0;}


0 0
原创粉丝点击