Description
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
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
思路:虽然是第一次做这样的题,虽然A了十多次,但是我还想以大神的话气说:咳咳,这就是简单的并查集思想,菜鸟就是要在失败中提升自己,没错思路很简单,下面看代码:
#include<stdio.h>
#include<string.h>
int pre[100110],count[100110],r;
int find(int x)//这个函数是来找他的根的
{
while(x!=pre[x])
x=pre[x];
return x;
}
void merge(int x,int y)//将两个根不同的元素合并
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx==fy)
return;
if(count[fx]<count[fy])//重点在这里,如果不比较两个根孩子的多少直接将其合并,就会超时,一定是小的合并到大的上,具体点击打开链接
{
r=fx;
fx=fy;
fy=r;
}
pre[fy]=fx;
count[fx]=count[fx]+count[fy];
}
int main()
{
int i,j,m,n,c,d,e,k=1;
char a[10],b;
while(scanf("%d%d",&n,&m)!=EOF)//下面就简单啦,调用函数就行
{
if(k!=1)
printf("\n");//注意这里
printf("Case %d:\n",k++);
for(j=1;j<=n;j++)
{
pre[j]=j;
count[j]=1;
}
for(i=0;i<m;i++)
{
scanf("%s",&a);
if(a[0]=='M')
{
scanf("%d%d",&c,&d);
merge(c,d);
}
else
{
scanf("%d",&e);
printf("%d\n",count[find(e)]);
}
}
}
return 0;
}