LA 3713

时间:2024-01-01 08:44:27

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 1LA 3713nLA 3713100000 <tex2html_verbatim_mark>and 1LA 3713mLA 3713100000 <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>(1LA 3713ijLA 3713n) <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.
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector> using namespace std; const int MAX_N = 1e5 + ;
int N,M;
int low[MAX_N * ],pre[MAX_N * ],cmp[MAX_N * ];
int age[MAX_N];
vector <int> G[ * MAX_N];
stack <int> S;
int dfs_clock,scc_cnt; void dfs(int u) {
pre[u] = low[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); ++i) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
low[u] = min(low[u],low[v]);
} else if(!cmp[v]) {
low[u] = min(low[u],pre[v]);
}
} if(pre[u] == low[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} bool scc() {
dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i <= * N; ++i) {
if(!pre[i]) dfs(i);
} for(int i = ; i <= N; ++i) {
if(cmp[i] == cmp[N + i]) return false;
} return true;
}
int main()
{
freopen("sw.in","r",stdin);
while(~scanf("%d%d",&N,&M) && (N || M)) {
int sum = ;
for(int i = ; i <= N; ++i) {
scanf("%d",&age[i]);
sum += age[i];
}
for(int i = ; i <= * N; ++i) G[i].clear(); for(int i = ; i <= M; ++i) {
int u,v;
scanf("%d%d",&u,&v);
if(age[u] * N >= sum && age[v] * N < sum
|| age[u] * N < sum && age[v] * N >= sum) {
G[v + N].push_back(u);
G[u + N].push_back(v);
} else { G[u].push_back(v + N);
G[v].push_back(u + N);
G[v + N].push_back(u);
G[u + N].push_back(v);
} } if(!scc()) {
printf("No solution.\n");
} else {
for(int i = ; i <= N; ++i) {
if(age[i] * N >= sum) {
printf("%c\n",cmp[i] < cmp[i + N] ? 'A' : 'C');
} else {
printf("%c\n",cmp[i] < cmp[i + N] ? 'B' : 'C');
}
}
}
} return ;
}