程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ZOJ 2833-Friendship(並查集+優化)

ZOJ 2833-Friendship(並查集+優化)

編輯:C++入門知識

ZOJ 2833-Friendship(並查集+優化)


Friendship

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


赤裸裸的並查集,一開始TLE了一次,當時是直接查某個元素所屬集合裡面的元素的個數,掃一遍需要O(n)最壞要O(m*n)。。。不TLE才怪,直接開結構體記錄每個集合元素的個數,初始化為1,合並的時候將被合並的集合的元素的個數加到另一個集合裡去,查找O(1)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef struct node
{
	int data,num;
};
node fa[100010];
void Make_set(int n)
{
	for(int i=1;i<=n;i++)
	{
		fa[i].data=i;
		fa[i].num=1;
	}
}
int Find(int x)
{
	if(x!=fa[x].data)
		fa[x].data=Find(fa[x].data);
	return fa[x].data;
}
void Union(int x,int y)
{
	int fx=Find(x);
	int fy=Find(y);
	if(fx==fy)
		return ;
	fa[fy].data=fx;
	fa[fx].num+=fa[fy].num;
}
int main()
{
	int n,m,x,y,T=1;char op;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(T!=1)
			printf("\n");
		getchar();
		printf("Case %d:\n",T++);
		Make_set(n);
		while(m--)
	   {
		 scanf("%c",&op);
		 if(op=='M')
		 {
			scanf("%d%d",&x,&y);
			getchar();
			Union(x,y);
		 }
		 else if(op=='Q')
		 {
			scanf("%d",&x);getchar();
				int ans=fa[Find(x)].num;
			printf("%d\n",ans);
		 }
		}
	}
	return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved