首页 > 代码库 > UVALive 3713 Astronauts
UVALive 3713 Astronauts
2sat....可看出每个宇航员可以有两种任务选择 (A/B) 或 C
对于每一对有矛盾的宇航员,如果属于同一年龄段 那么这两个人选择的任务不应该相同
如果属于不同的年龄段,则俩个人不能同时选择任务C
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
[Submit] [Go Back] [Status]
Description
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 considered young if their age is less than the average age of the astronauts and an astronaut is senior 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 integers 1n100000 and 1m100000 . The number n is the number of astronauts. The next n lines specify the age of the n astronauts; each line contains a single integer number between 0 and 200. The next m lines contains two integers each, separated by a space. A line containing i and j(1i, jn) means that the i -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‘. The i -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 20 21 22 23 24 25 26 27 28 101 102 103 104 105 106 107 108 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 10 2 9 3 12 4 11 5 14 6 13 7 16 8 15 1 12 1 13 3 16 6 15 0 0
Sample Output
B C C B C B C B A C C A C A C A
Source
[Submit] [Go Back] [Status]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=200200; struct Edge { int to,next; }edge[maxn*4]; int Adj[maxn],Size; void init() { Size=0; memset(Adj,-1,sizeof(Adj)); } void Add_Edge(int u,int v) { edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++; } int n,m; int age[maxn],sum; int vis[maxn],Stack[maxn],top; bool dfs(int u) { if(vis[u^1]) return false; if(vis[u]) return true; vis[u]=true; Stack[top++]=u; for(int i=Adj[u];~i;i=edge[i].next) { if(!dfs(edge[i].to)) return false; } return true; } bool two_sat() { memset(vis,0,sizeof(vis)); for(int i=0;i<2*n;i+=2) { if(vis[i]||vis[i^1]) continue; top=0; if(!dfs(i)) { while(top) vis[Stack[--top]]=0; if(!dfs(i^1)) return false; } } return true; } bool kind(int x) { return age[x]*n<sum; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) break; init();sum=0; for(int i=0;i<n;i++) { scanf("%d",age+i); sum+=age[i]; } for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); a--;b--; if(kind(a)==kind(b)) { Add_Edge(a<<1,b<<1|1); Add_Edge(b<<1,a<<1|1); } Add_Edge(a<<1|1,b<<1); Add_Edge(b<<1|1,a<<1); } if(two_sat()) { for(int i=0;i<2*n;i+=2) { if(!vis[i]) { puts("C"); continue; } if(kind(i/2)) puts("B"); else puts("A"); } } else puts("No solution."); } return 0; }