LA3713--Astronauts(2-sat)

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 1$ \le$n$ \le$100000 and 1$ \le$m$ \le$100000 . 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 (1$ \le$ij$ \le$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对宇航员相互讨厌,因此不能分配到同一任务。编程找出一个满足上述所有要求的任务分配方案。
思路:显然宇航员分为两类。第一类可以做任务A,C。第二类可以做任务B,C。
     对于同类的宇航员,只能一个做A一个做C。
     对于异类的宇航员,只要不都做C即可。
     vis[2*u] = 1表示做A或B任务(具体看他对应类型)
     vis[2*u+1] = 1表示做C任务。
     2-sat对应该连的边:
     if(Type[u] == Type[v])
     {
          AddEdge(2*u,2*v+1);
          AddEdge(2*u+1,2*v);
          AddEdge(2*v,2*u+1);
          AddEdge(2*v+1,2*u);
     }
     else 
     {
          AddEdge(2*u+1,2*v);
          AddEdge(2*v+1,2*u);
     }
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define maxn 200080
#define maxm 1000080
int first[maxn];
int vv[maxm],nxt[maxm],S[maxm];
int e,c,n,m;
bool vis[maxn];
bool Type[maxn];
int key[maxn];
void AddEdge(int u,int v)
{
	vv[e] = v;	nxt[e] = first[u];	first[u] = e++;
}

bool dfs(int u)//现在要将u置1
{
	if(vis[u^1])	return 0;
	if(vis[u])	return 1;
	vis[u] = 1;
	S[c++] = u;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(!dfs(v))	return false;
	}
	return true;
}

bool Judge()
{
	for(int i = 0;i < n*2;i+=2)
	{
		if(!vis[i] && !vis[i+1])
		{
			c = 0;
			if(!dfs(i))
			{
				while(c > 0)	vis[S[--c]] = 0;
				if(!dfs(i+1))	return false;
			}
		}
	}
	return true;
}

int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%d%d",&n,&m)==2 && (n|m))
	{
		memset(first,-1,sizeof(first));
		memset(vis,0,sizeof(vis));
		e = 0;
		double average = 0;
		for(int i = 0;i < n;i++)
		{
			scanf("%d",&key[i]);
			average += key[i];
		}
		average /= n;
		for(int i = 0;i < n;i++)	if(key[i] >= average)	Type[i] = 0;
		else Type[i] = 1;
		for(int i = 0;i < m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			u--;v--;
			if(Type[u] == Type[v])
			{
				AddEdge(2*u,2*v+1);
				AddEdge(2*u+1,2*v);
				AddEdge(2*v,2*u+1);
				AddEdge(2*v+1,2*u);
			}
			else 
			{
				AddEdge(2*u+1,2*v);
				AddEdge(2*v+1,2*u);
			}
		}
		if(!Judge())	puts("No solution.");
		else 
		{
			for(int i = 0;i < 2*n;i+=2)
			{
				if(!vis[i])	puts("C");
				else if(!Type[i/2])	puts("A");
				else puts("B");
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值