现在的位置: 首页 > 综合 > 正文

hdu2094

2014年01月30日 ⁄ 综合 ⁄ 共 1425字 ⁄ 字号 评论关闭

分析:    其实,我觉得,这题应该用并查集的,当所有的flag都指向同一个目标的时候,冠军就诞生了,冠军就是目标。    但是。。。 用并查集写出来竟然WA了?    倒是从网上看了看,只要符合没有被击败过的人的人数==1,那么就输出Yes。 按照这个思想写,过了- -I。 But。。。明显不对呀- -I。 反eg:A击败B,B击败C;同时有:D击败E、E击败F、F击败D。 后者是个循环么,根本就不可能判断出A击败了D、E、F么,暴汗- -III,只能说题意不清吧。    不过还是分到并查集类别好了,就假定下面两个代码的前者(用并查集做的)才是对的。                                                              2012-04-13

/*

#include"stdio.h"
#include"string.h"
int main()
{
	int n;
	int i;
	int flag[1111];
	char name[1111][22];
	int k;
	int k1,k2;
	char str1[22],str2[22];
	int d;
	int temp;
	while(scanf("%d",&n),n)
	{
		d=0;
		k=0;
		for(i=0;i<1111;i++)
			flag[i]=i;


		for(i=0;i<n;i++)
		{
			scanf("%s%s",str1,str2);
			for(k1=0;k1<k;k1++)
				if(strcmp(name[k1],str1)==0)
					break;
			if(k1==k)
			{
				strcpy(name[k],str1);
				k++;
			}


			for(k2=0;k2<k;k2++)
				if(strcmp(name[k2],str2)==0)
					break;
			if(k2==k)
			{
				strcpy(name[k],str2);
				k++;
			}


			flag[k2]=flag[k1];
		}


		temp=k;
		while(temp--)
		{
			for(i=0;i<k;i++)
				flag[i]=flag[flag[i]];
		}


		for(i=1;i<k;i++)
			if(flag[i]!=flag[i-1])
			{
				d=1;
				break;
			}


		if(d==1)
			printf("No\n");
		else
			printf("Yes\n");
	}
	return 0;
}

*/

#include"stdio.h"
#include"string.h"
int main()
{
	int flag[1111];
	int name[1111][22];
	int str1[22],str2[22];
	int k1,k2,k;
	int i;
	int n;
	int count;
	while(scanf("%d",&n),n)
	{
		k=0;
		memset(flag,0,sizeof(flag));
		for(i=0;i<n;i++)
		{
			scanf("%s%s",str1,str2);
			for(k1=0;k1<k;k1++)
				if(strcmp(name[k1],str1)==0)
					break;
			if(k1==k)
			{
				strcpy(name[k],str1);
				k++;
			}

			for(k2=0;k2<k;k2++)
				if(strcmp(name[k2],str2)==0)
					break;
			if(k2==k)
			{
				strcpy(name[k],str2);
				k++;
			}

			flag[k2]=1;
		}

		count=0;
		for(i=0;i<k;i++)
			if(flag[i]==0)
				count++;
		if(count==1)
			printf("Yes\n");
		else
			printf("No\n");
	}
	return 0;
}






抱歉!评论已关闭.