ZOJ 2833 Friendship 并查集

来源:互联网 发布:淘宝窗帘上门安装服务 编辑:程序博客网 时间:2024/06/13 15:03

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


这个并查集是在《图论算法理论、实现及应用》(王桂平 王 衍 任嘉辰主编)p91——p95上看到的。。。。。

#include <stdio.h>#include <string.h>#define MAX 100010int f[MAX];int nfind(int x){    int r = x,q;    while(f[x] >= 0)        x = f[x];    while(r != x)    {        q = f[r];        f[r] = x;        r = q;    }    return x;}void lj(int x,int y){    int fx = nfind(x);    int fy = nfind(y);    if(fx != fy)    {        int tmp = f[fx] + f[fy];        if(f[fx] > f[fy])        {            f[fx] = fy;            f[fy] = tmp;        }        else        {            f[fy] = fx;            f[fx] = tmp;        }    }}int main(){    int n,m,ca = 1,x,y;    char str[10];    while(~scanf("%d%d",&n,&m))    {        if(ca > 1)            printf("\n");        printf("Case %d:\n",ca++);        for(int i = 1; i <= n; i++)            f[i] = -1;        while(m--)        {            scanf("%s",str);            if(str[0] == 'M')            {                scanf("%d%d",&x,&y);                lj(x,y);            }            else if(str[0] == 'Q')            {                scanf("%d",&x);                printf("%d\n",-f[nfind(x)]);            }        }    }    return 0;}

这个和上面差不多。。。。。

#include <stdio.h>#include <string.h>#define maxn 100010struct node{    int num;    int cnt;} f[maxn];int nfind(int x){    if(f[x].num != x)        f[x].num = nfind(f[x].num);    return f[x].num;}void lj(int x,int y){    int fx = nfind(x);    int fy = nfind(y);    if(f[fx].num != fy)    {        f[fx].num = fy;        f[fy].cnt += f[fx].cnt;    }}int main(){    char cmd[10];    int n,m,a,b,ca = 0;    while(~scanf("%d%d",&n,&m))    {        for(int i = 0; i <= n; i++)        {            f[i].num = i;            f[i].cnt = 1;        }        if(ca)            printf("\n");        printf("Case %d:\n",++ca);        while(m--)        {            scanf("%s",cmd);            if(cmd[0] == 'M')            {                scanf("%d%d",&a,&b);                lj(a,b);            }            else if(cmd[0] == 'Q')            {                scanf("%d",&a);                printf("%d\n",f[nfind(a)].cnt);            }        }    }    return 0;}





0 0
原创粉丝点击