3713 - Astronauts
Time limit: 3.000 secondsThe 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 1n
100000 and 1
m
100000 . The number n is the number of astronauts. The next n lines specify the age of the nastronauts; 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 (1
i, j
n) 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
题意:有A,B,C三个任务分配给n个宇航员,其中每个宇航员恰好要分配一个任务。设所有宇航员的平均年龄为x,年龄大于等于x的才能分配A任务,小于x才能分配B任务,C没有限制。有m对宇航员相互讨厌,不能分配到同一任务。输出一个满足上述要求的任务分配方案。
思路:
不难发现,每个宇航员只有两种选择,年龄大于等于x只能选择A或C,小于x的只能选择B或C,我们容易想到two-sat;设大于等于x的宇航员做任务A为true,做C为false,小于x的宇航员做B为true,做C为false;对于相互讨厌的宇航员i和j,建边我们考虑两种情况(偶为真,奇为假) (1)属于同一类型的,2*i->2*j+1, 2*j->2*i+1,2*i+1->2*j, 2*j+1->2*i (2) 不同类型的, 2*i+1->2*j, 2*j+1->2*i ;之后two-sat一下。详见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=100010;
vector<int>G[2*MAXN];
int s[2*MAXN],mark[2*MAXN];
double a[MAXN];
int n,m,cnt;
void init()
{
for(int i=0;i<2*n;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[cnt++]=x;
for(int i=0;i<G[x].size();i++)
if(!dfs(G[x][i])) return false;
return true;
}
bool solve()
{
for(int i=0;i<2*n;i+=2)
if(!mark[i]&&!mark[i+1])
{
cnt=0;
if(!dfs(i))
{
while(cnt>0) mark[s[--cnt]]=false;
if(!dfs(i+1)) return false;
}
}
return true;
}
int main()
{
//freopen("text.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
{
init();
double k=0;
for(int i=0;i<n;i++)
{
scanf("%lf",&a[i]);
k+=a[i];
}
k/=n;
int u,v;
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
if(u==v) continue;
u--; v--;
if((a[u]>=k&&a[v]>=k)||(a[u]<k&&a[v]<k))
{
G[2*u].push_back(2*v+1);
G[2*v].push_back(2*u+1);
}
G[2*u+1].push_back(2*v);
G[2*v+1].push_back(2*u);
}
int flag=solve();
if(flag)
{
for(int i=0;i<2*n;i+=2)
{
if(mark[i])
{
if(a[i/2]>=k)
printf("A\n");
else
printf("B\n");
}
else
printf("C\n");
}
}
else
printf("No solution.\n");
}
return 0;
}
不过有个坑爹的地方,我把vector改为邻接表就跪了= =我只能说数据见鬼了。。
附邻接表代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100000+100;
int n,m,edge_cnt,top;
double a[MAXN];
int head[2*MAXN],mark[2*MAXN],s[2*MAXN];
struct Edge
{
int v;
int next;
}edge[2*MAXN];
void init()
{
edge_cnt=0;
memset(mark,0,sizeof(mark));
memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
edge[edge_cnt].v=v;
edge[edge_cnt].next=head[u];
head[u]=edge_cnt++;
}
bool dfs(int x)
{
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x]=true;
s[top++]=x;
for(int i=head[x];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!dfs(v))
return false;
}
return true;
}
bool solve()
{
for(int i=0;i<2*n;i+=2)
if(!mark[i]&&!mark[i+1])
{
top=0;
if(!dfs(i))
{
while(top>0) mark[s[--top]]=false;
if(!dfs(i+1)) return false;
}
}
return true;
}
int main()
{
//freopen("text.txt","r",stdin);
while(~scanf("%d%d",&n,&m) && n+m)
{
printf("%d %d\n",n,m);
init();
double sum=0;
for(int i=0;i<n;i++)
{
scanf("%lf",&a[i]);
sum+=a[i];
}
sum/=n;
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
if(u==v)
continue;
u--;v--;
if((a[u]>=sum&&a[v]>=sum)||(a[u]<sum&&a[v]<sum))
{
addedge(2*u,2*v+1); addedge(2*v,2*u+1);
}
addedge(2*u+1,2*v); addedge(2*v+1,2*u);
}
int flag=solve();
if(flag)
{
for(int i=0;i<2*n;i+=2)
{
if(mark[i])
{
if(a[i/2]>=sum)
printf("A\n");
else
printf("B\n");
}
else
printf("C\n");
}
}
else
printf("No solution.\n");
}
return 0;
}