码迷,mamicode.com
首页 > 其他好文 > 详细

zoj2833Friendship

时间:2017-06-14 22:20:55      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:etc   zoj   put   start   bsp   sep   math   time   host   

Time Limit: 3 Seconds      Memory Limit: 32768 KB

A friend is like a flower,
a rose to be exact,
Or maybe like a brand new gate
that never comes unlatched.

A friend is like an owl,
both beautiful and wise.
Or perhaps a friend is like a ghost,
whose spirit never dies.

A friend is like a heart that goes
strong until the end.
Where would we be in this world
if we didn‘t have a friend?

                       - By Emma Guest

Now you‘ve grown up, it‘s time to make friends. The friends you make in university are the friends you make for life. You will be proud if you have many friends.

Input

There are multiple test cases for this problem.

Each test case starts with a line containing two integers N, M (1 <= N <= 100‘000, 1 <= M <= 200‘000), representing that there are totally N persons (indexed from 1 to N) and M operations, then M lines with the form "M a b" (without quotation) or "Q a" (without quotation) follow. The operation "M a b" means that person a and b make friends with each other, though they may be already friends, while "Q a" means a query operation.

Friendship is transitivity, which means if a and b, b and c are friends then a and c are also friends. In the initial, you have no friends except yourself, when you are freshman, you know nobody, right? So in such case you have only one friend.

Output

For each test case, output "Case #:" first where "#" is the number of the case which starts from 1, then for each query operation "Q a", output a single line with the number of person a‘s friends.

Separate two consecutive test cases with a blank line, but Do NOT output an extra blank line after the last one.

Sample Input

3 5
M 1 2
Q 1
Q 3
M 2 3
Q 2
5 10
M 3 2
Q 4
M 1 2
Q 4
M 3 2
Q 1
M 3 1
Q 5
M 4 2
Q 4

Sample Output

Case 1:
2
1
3

Case 2:
1
1
3
1
4

Notes

This problem has huge input and output data, please use ‘scanf()‘ and ‘printf()‘ instead of ‘cin‘ and ‘cout‘ to avoid time limit exceed.



这道题不知道怎么了,我感觉有毛病。代码和别人的一模一样,执行一模一样,可是我的一直wa,别人的一下就a了;

附上ac代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int per[110000],m,n,num[110000];
void init()
{
	for(int i=1;i<=m;i++)
	{
		per[i]=i;
		num[i]=1;
	}
	
}
int find(int x)
{
	int t=x,i=x,j;
	while(t!=per[t])
	t=per[t];
	while(i!=t)
	{
		j=per[i];
		per[i]=t;
		i=j;
	}
	return t;
}
void join(int x ,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		num[fy]+=num[fx];
		return ;
	}
	return ;
}
int main()
{
	int i,x,y,z,cot=0;
	
	char a;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		init();
		getchar();
		if(cot++)
			printf("\n");
		printf("Case %d:\n",cot);
		for(i=0;i<n;i++)
		{
			scanf("%c",&a);
			if(a==‘M‘)
			{
				scanf("%d%d",&x,&y);
				getchar();
				join(x,y);
			}
			else if(a==‘Q‘)
			{
				scanf("%d",&z);
				getchar();
				printf("%d\n",num[find(z)]);
			}
		}
	}
	return 0;
}

再附一个一直wa的代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int per[110000];
int sum[1100000];
int find(int x)
{
	int r=x;
	while(r!=per[x])
	r=per[r];
	return r;
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		per[fx]=fy;
		sum[fy]+=sum[fx];
		sum[fx]=sum[fy];
	}
}
int main()
{
	int a,b,i,m,n;
	char c;
	int flag=0;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		if(flag++)
		printf("\n");
		printf("Case %d:\n",flag);
		for(i=0;i<110000;i++)
		{
			per[i]=i;
			sum[i]=1;
		}
		getchar();
		for(i=0;i<n;i++)
		{
			scanf("%c",&c);
			if(c==‘M‘)
			{
			scanf("%d%d",&a,&b);
			join(a,b);
			}
			else if(c==‘Q‘)
			{
				scanf("%d",&a);
				printf("%d\n",sum[find(a)]);
			}
			getchar();
		}
	}
	return 0;
}


zoj2833Friendship

标签:etc   zoj   put   start   bsp   sep   math   time   host   

原文地址:http://www.cnblogs.com/ljbguanli/p/7011051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!