UVa Self-Assembly(topo sort)[待AC]

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
using namespace std;
int G[52][52],c[52];
bool topo(int u)
{
//	printf("%c ",u%26+'A');
	c[u]=-1;   //visiting;
	for(int v=0;v<52;v++)
	{
	 
		if(G[u][v]){
//			printf("%c %c\n",u%26+'A',v%26+'A');
			int p=v<26?v+26:v-26;
			if(c[p]<0) return true;
			else if(!c[p]&&topo(p)) return true;
		}
	}
	c[u]=1;
	return false;
}
void print()
{
	for(int i=0;i<52;i++)
	cout<<c[i]<<' ';
	cout<<endl;
}
bool check()
{
	
	for(int u=0;u<52;u++)
	{
		if(!c[u]) 
		{
//			cout<<u<<endl;
			if(topo(u)) {
				return true;
		}
//			print();
		}
	}

	return false;
}
int main()
{
	int n;
	while(scanf("%d",&n)==1&&n)
	{
		char s[10];
		memset(G,0,sizeof(G));
		memset(c,0,sizeof(c));
		for(int i=0;i<n;i++)
		{
			scanf("%s",s);
			int letter[3]={0},sign[3]={0},p=0; 
			for(int i=0;i<8;i+=2)
			{
				if(isdigit(s[i])) continue;
				letter[p]=s[i]-'A';
				sign[p]=s[i+1]=='+'?0:26;
				p++; 
			}
			for(int i=0;i<3;i++)
			{
				int l=letter[i]+sign[i];
//				c[l]=-1;   //existed
				for(int j=1;j<=2;j++)
				{
					int m=letter[(i+j)%3]+sign[(i+j)%3];
					G[l][m]=1;
	//				printf("%c %c\n",m%26+'A',l%26+'A');
				}
			}
		}
		if(check())	printf("un");
		printf("bounded\n"); 
	}
	return 0;
}

你可能感兴趣的:(UVa Self-Assembly(topo sort)[待AC])