frindship(优化并查集)

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.



/*此代码可以运算结果,但是我交上去之后,<span style="color: blue;">Time Limit Exceeded,因为在查找父节点时用的                 是for循环,导致超时,引用一个结构体(初始的记录个数的数组要置为一)将父节点相同的集                 合个数存起来,并且在每次合并两个新的集合时更新父节点集合个数。在本代码之后我附上了                  优化后的代码,大家可以对比只看*/</span>

#include<stdio.h>
#include<string.h>

int father[200010];

int find_set(int x)
{
	if (father[x] != x)
	{
		father[x] = find_set(father[x]);
	}

	return father[x];
}
void Union(int x, int y)
{
	int fx = find_set(x);
	int fy = find_set(y);

	if (fx == fy)
	{
		return;
	}

	father[fy] = fx;
}
int main()
{
	int  a, b, i, j, k, c;
	long int n,m;
	char str[10];
	k = 1;


	while (scanf("%d%d", &n, &m) != EOF)
	{
	    for (int i = 1; i <= n; i++)
	{
		father[i] = i;
	}
        if(k!=1)
            printf("\n");



		printf("Case %d:\n", k);
		k++;


		for (i = 1; i <= m; i++)
		{


			scanf("%s", str);

			if (strcmp(str, "M") == 0)
			{

				scanf("%d%d", &a, &b);
				Union(a, b);


			}

			if (strcmp(str, "Q") == 0)
			{
				int y = 0;

				scanf("%d", &c);

				for (j = 1; j <= n; j++)
				{
					if (find_set(c) == find_set(j))
					{
						y++;
					}

				}

				printf("%d\n", y);







			}


		}





	}

	return 0;
}
/*优化后的代码*/
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

typedef struct node
{
    int date,num;
};
node father[200010];
void make_set(int n)
{
    for(int i=1; i<=n; i++)
    {

        father[i].date=i;
        father[i].num=1;

    }
    return;

}
int find_set(int x)
{
    if (father[x].date != x)
    {
        father[x].date = find_set(father[x].date);
    }

    return father[x].date;
}
void Union(int x, int y)
{
    int fx = find_set(x);
    int fy = find_set(y);

    if (fx == fy)
    {
        return;
    }

    father[fy].date = fx;
    father[fx].num=father[fx].num+father[fy].num;
}
int main()
{
    int  a, b, i, j, k, c;
    long int n,m;
    char str[10];
    k = 1;


    while (scanf("%d%d", &n, &m) != EOF)
    {
        make_set(n);
        if(k!=1)
            printf("\n");

        printf("Case %d:\n", k);
        k++;


        for (i = 1; i <= m; i++)
        {


            scanf("%s", str);

            if (strcmp(str, "M") == 0)
            {

                scanf("%d%d", &a, &b);
                Union(a, b);


            }

            if (strcmp(str, "Q") == 0)
            {
                int y = 0;

                scanf("%d", &c);
                y=find_set(c);



                printf("%d\n", father[y].num);
            }


        }


     }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值