UVA1391 Astronauts

每个宇航员只能根据年龄情况,分配A,B中的一个任务,和C任务。
考虑两个有矛盾的宇航员:
1.如果他们在不同年龄区段,那么他们只要不同时在C即可。(即,若其中一人在C,另一个必须在A或B)
2.如果他们在同一年龄区段,那么,当其中一人在A或B时,另一人必须去C。多了两个限制条件。
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,x,y,sum,a[N];
int now,top,col,dfn[N*2],low[N*2],sta[N*2],color[N*2];
int cnt,head[N*2];
struct edge{
    
    int next,to;}e[N*4];

inline void add(int u,int v)
{
    
    
	cnt++;
	e[cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
void tarjan(int u)
{
    
    
	dfn[u]=low[u]=++now;
	sta[++top]=u;
	for (register int i=head[u]; i; i=e[i].next)
	{
    
    
		if (!dfn[e[i].to])
		{
    
    
			tarjan(e[i].to);
			low[u]=min(low[u],low[e[i].to]);
		}
		else if (!color[e[i].to]) low[u]=min(low[u],low[e[i].to]);
	}
	if (dfn[u]==low[u])
	{
    
    
		col++;
		while (sta[top]!=u) color[sta[top]]=col,top--;
		color[sta[top]]=col,top--;
	}
}

int main(){
    
    
	scanf("%d%d",&n,&m);
	while (n && m)
	{
    
     
		sum=0;
		for (register int i=1; i<=n; ++i) scanf("%d",&a[i]),sum+=a[i];
		for (register int i=1; i<=n; ++i) a[i]=(a[i]*n>=sum);
		cnt=0; for (register int i=1; i<=2*n; ++i) head[i]=0;
		for (register int i=1; i<=m; ++i)
		{
    
    
			scanf("%d%d",&x,&y);
			add(x+n,y); add(y+n,x);
			if (a[x]==a[y])
			{
    
    
				add(x,y+n);
				add(y,x+n);
			}
		}
		now=top=col=0;
		for (register int i=1; i<=2*n; ++i) dfn[i]=low[i]=color[i]=0;
		for (register int i=1; i<=2*n; ++i) if (!dfn[i]) tarjan(i);
		bool jay=true;
		for (register int i=1; i<=n; ++i) if (color[i]==color[i+n]) {
    
    jay=false; break;}
		if (!jay) puts("No solution.");
		else
		{
    
    
			for (register int i=1; i<=n; ++i)
			{
    
    
				if (color[i]<color[i+n])
				{
    
    
					if (a[i]) putchar('A');
					else putchar('B');
				}
				else putchar('C');
				putchar('\n');
			}
		}
		scanf("%d%d",&n,&m);	
	}
return 0;
}

猜你喜欢

转载自blog.csdn.net/Dove_xyh/article/details/108576487