并查集 fjutoj 2144

题目链接 点击打开链接

Input

输入多组数据

每组数据第一行是两个整数n(1<=n<=10^6),m(1<=m<=10^6)。分别表示元素数、操作数(初始时每个元素以自己为一个集合,元素编号是1-n)

接下来m行,每行有如下几种输入:

union x y ——表示将x所在的集合和y所在的集合合并为一个集合。

same x y ——询问x和y是否为同一个集合、为同一个集合输出1,不同集合输出0

num x ——询问x所在的集合共有多少个元素

max x ——询问x所在的集合中元素编号最大是多少

setnum ——询问现在总共有多少个集合

Output

对于每个same、num、max、setnum操作输出一行,用一个整数表示答案。

SampleInput
 
  
5 10
setnum
same 1 2
union 1 2
same 1 2
union 2 3
same 1 3
union 4 5
setnum
max 1
num 4
SampleOutput
 
  
5
0
1
1
2
3
2

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int pre[maxn],maxx[maxn],num[maxn];
int Find(int x)
{
    int p,tmp;
    p = x;
    while(x != pre[x])
        x =  pre[x];
    while(p!= x)
    {
        tmp= pre[p];
        pre[p] = x;
        p = tmp;
    }
    return x;
}
void join (int x,int y)
{
    int p,q;
    p = Find(x);
    q = Find(y);
    if(p!=q)
    {
        pre[p] = q;
        maxx[q] = max(maxx[q],maxx[p]);
        num[q] += num[p];
    }
}
int main()
{
    int n,m;
    char str[10];
    while(~scanf("%d%d",&n,&m)){
        for(int i = 1;i<= n;i++)
        {
            pre[i] = i;
            num[i] = 1;
            maxx[i] = i;
        }
        int Set = n;
        while(m--)
        {
            getchar();
            int x,y;
            scanf("%s",str);
            if(str[0]=='u'){
                scanf("%d%d",&x,&y);
                if(Find(x)!=Find(y)){
                join(x,y);
                Set--;}
            }
            if(str[0]=='s'&&str[1]=='a'){
                scanf("%d%d",&x,&y);
                if(Find(x) == Find(y)) puts("1");
                else puts("0");
            }
            if(str[0]=='n'){
                scanf("%d",&x);
                printf("%d\n",num[Find(x)]);
            }
            if(str[0]=='m'){
                scanf("%d",&x);
                printf("%d\n",maxx[Find(x)]);
            }
            if(str[0]=='s'&&str[1]=='e') printf("%d\n",Set);
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值