pku 2580 约束扩散

 

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

long n, m;
char str[30];
vector < vector < vector <long> > > V;
long arr[110];
bool vist[110], isCan[110][110];

void solution()
{
	if (m == 0)
	{
		puts("YES");
		return;
	}
	
	long i, j, k, havekey = 0;
	memset(vist, 0, sizeof(vist));
	vist[m] = true;
	havekey |= arr[m];

	bool flag = true;
	while (flag)
	{
		flag = false;
		for (i = 0; i < n; ++i)
		{
			if (vist[i])
			{
				for (j = 0; j < n; ++j)
				{
					if (isCan[i][j] && !vist[j])
					{
						for (k = 0; k < V[i][j].size(); ++k)
						{
							if ((havekey & V[i][j][k]) == V[i][j][k] && !vist[j])
							{
								havekey |= arr[j];
								vist[j] = true;
								flag = true;
								if (j == 0)
								{
									puts("YES");
									return;
								}
							}
						}
					}
				}
			}
		}
	}

	puts("NO");
}

int main()
{
	long i;
	char ch;
	while (scanf("%s", str) != EOF)
	{
		if (!strcmp(str, "ENDOFINPUT"))
		{
			break;
		}

		memset(isCan, 0, sizeof(isCan));
		memset(arr, 0, sizeof(arr));
		scanf("%ld %ld", &m, &n);
		V.clear();
		V.resize(n);
		for (i = 0; i < n; ++i)
		{
			V[i].resize(n);
		}
		getchar();
		
		for (i = 0; i < n; ++i)
		{
			long num = 0, state = 0;
			bool flag = false;
			while (scanf("%c", &ch))
			{
				if (ch == ' ' || ch == '\n')
				{
					if (flag)
					{
						V[i][num].push_back(state);
						V[num][i].push_back(state);
						isCan[i][num] = isCan[num][i] = true;
						num = state = 0;
						flag = false;
					}
				}
				else if (ch >= '0' && ch <= '9')
				{
					flag = true;
					num = num*10+ch-'0';
				}
				else if (ch >= 'A' && ch <= 'Z')
				{
					state |= (1<<(ch-'A'));
				}
				if (ch == '\n')
				{
					break;
				}
			}
		}
		
		for (i = 0; i < n; ++i)
		{
			while (scanf("%c", &ch))
			{
				if (ch >= 'A' && ch <= 'Z')
				{
					arr[i] |= (1<<(ch-'A'));
				}
				if (ch == '\n')
				{
					break;
				}
			}
		}
		gets(str);
		
		solution();
	}
	return 0;
}

 

posted on 2010-05-18 21:11  ZAFU_VA  阅读(176)  评论(0编辑  收藏  举报

导航