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 <tex2html_verbatim_mark>and 1m100000 <tex2html_verbatim_mark>. The number n <tex2html_verbatim_mark>is the number of astronauts. The next n <tex2html_verbatim_mark>lines specify the age of the n<tex2html_verbatim_mark>astronauts; each line contains a single integer number between 0 and 200. The next m <tex2html_verbatim_mark>lines contains two integers each, separated by a space. A line containing i <tex2html_verbatim_mark>and j <tex2html_verbatim_mark>(1i, jn) <tex2html_verbatim_mark>means that the i <tex2html_verbatim_mark>-th astronaut and the j <tex2html_verbatim_mark>-th astronaut hate each other.
The input is terminated by a block with n = m = 0 <tex2html_verbatim_mark>.
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 <tex2html_verbatim_mark>-th line describes which mission the i <tex2html_verbatim_mark>-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
拆成两个分组走2-sat.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <stack> 6 #include <vector> 7 8 using namespace std; 9 10 const int MAX_N = 1e5 + 5; 11 int N,M; 12 int low[MAX_N * 2],pre[MAX_N * 2],cmp[MAX_N * 2]; 13 int age[MAX_N]; 14 vector <int> G[2 * MAX_N]; 15 stack <int> S; 16 int dfs_clock,scc_cnt; 17 18 void dfs(int u) { 19 pre[u] = low[u] = ++dfs_clock; 20 S.push(u); 21 for(int i = 0; i < G[u].size(); ++i) { 22 int v = G[u][i]; 23 if(!pre[v]) { 24 dfs(v); 25 low[u] = min(low[u],low[v]); 26 } else if(!cmp[v]) { 27 low[u] = min(low[u],pre[v]); 28 } 29 } 30 31 if(pre[u] == low[u]) { 32 ++scc_cnt; 33 for(;;) { 34 int x = S.top(); S.pop(); 35 cmp[x] = scc_cnt; 36 if(x == u) break; 37 } 38 } 39 } 40 41 bool scc() { 42 dfs_clock = scc_cnt = 0; 43 memset(cmp,0,sizeof(cmp)); 44 memset(pre,0,sizeof(pre)); 45 46 for(int i = 1; i <= 2 * N; ++i) { 47 if(!pre[i]) dfs(i); 48 } 49 50 for(int i = 1; i <= N; ++i) { 51 if(cmp[i] == cmp[N + i]) return false; 52 } 53 54 return true; 55 } 56 int main() 57 { 58 freopen("sw.in","r",stdin); 59 while(~scanf("%d%d",&N,&M) && (N || M)) { 60 int sum = 0; 61 for(int i = 1; i <= N; ++i) { 62 scanf("%d",&age[i]); 63 sum += age[i]; 64 } 65 for(int i = 1; i <= 2 * N; ++i) G[i].clear(); 66 67 for(int i = 1; i <= M; ++i) { 68 int u,v; 69 scanf("%d%d",&u,&v); 70 if(age[u] * N >= sum && age[v] * N < sum 71 || age[u] * N < sum && age[v] * N >= sum) { 72 G[v + N].push_back(u); 73 G[u + N].push_back(v); 74 } else { 75 76 G[u].push_back(v + N); 77 G[v].push_back(u + N); 78 G[v + N].push_back(u); 79 G[u + N].push_back(v); 80 } 81 82 } 83 84 if(!scc()) { 85 printf("No solution. "); 86 } else { 87 for(int i = 1; i <= N; ++i) { 88 if(age[i] * N >= sum) { 89 printf("%c ",cmp[i] < cmp[i + N] ? 'A' : 'C'); 90 } else { 91 printf("%c ",cmp[i] < cmp[i + N] ? 'B' : 'C'); 92 } 93 } 94 } 95 } 96 97 return 0; 98 }