ZOJ 2833 Friendship(并查集)

Friendship

Time Limit: 3000 msMemory 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.


题后de废话

这题真的很水但是注意输入输出啊啊啊啊啊啊,疯掉了,Case大写卡了我一上午,最后一直WA哭辽...


啊啊啊啊上水码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
 
int father[100010];

int find(int x){
	int r=x;
	while(father[r]>=0) r=father[r];
	while(x!=r){
		int ans=father[x];
		father[x]=r;
		x=ans;
	}//路径压缩 
	return r;
}
 
void merge(int x,int y){//降维合并 
	int fx,fy;
	fx=find(x);
	fy=find(y);
	int temp=father[fx]+father[fy];
	if(fx!=fy){
		if(father[fx]>father[fy]){//由于树根的节点是负值,所以值越大则对应的集合元素越少 
			father[fy]=temp;
			father[fx]=fy;
		}
		else{
			father[fx]=temp;
			father[fy]=fx;
		}
	}
} 
 
int main()
{
	int n,m,a,b; 
	int num=0;
	while(scanf("%d%d",&n,&m)!=EOF){
		memset(father,-1,sizeof(int)*(n+1));//初始化 
		if(num) printf("\n");
		printf("Case %d:\n",++num);
		while(m--){
			char ch;	
			cin>>ch;
			if(ch=='M') {
				scanf("%d%d",&a,&b);
				merge(a,b);
			}			
			else if(ch=='Q'){
				scanf("%d",&a);		
				printf("%d\n",-father[find(a)]);			
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43838723/article/details/106827924
ZOJ