restructuringcompany和almostunion-find并查集的区间合并与并查集的删除

博客就叫Molex好了 博客就叫Molex好了     2022-09-09     709

关键词:

Restructuring Company

 

Even the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let‘s consider the following model of a company.

There are n people working for the Large Software Company. Each person belongs to some department. Initially, each person works on his own project in his own department (thus, each company initially consists of n departments, one person in each).

However, harsh times have come to the company and the management had to hire a crisis manager who would rebuild the working process in order to boost efficiency. Let‘s use team(person) to represent a team where person person works. A crisis manager can make decisions of two types:

  1. Merge departments team(x) and team(y) into one large department containing all the employees of team(x) and team(y), where x and y (1?≤?x,?y?≤?n) — are numbers of two of some company employees. If team(x) matches team(y), then nothing happens.
  2. Merge departments team(x),?team(x?+?1),?...,?team(y), where x and y (1?≤?x?≤?y?≤?n) — the numbers of some two employees of the company.

At that the crisis manager can sometimes wonder whether employees x and y (1?≤?x,?y?≤?n) work at the same department.

Help the crisis manager and answer all of his queries.

Input

The first line of the input contains two integers n and q (1?≤?n?≤?200?000, 1?≤?q?≤?500?000) — the number of the employees of the company and the number of queries the crisis manager has.

Next q lines contain the queries of the crisis manager. Each query looks like type x y, where 技术分享. If type?=?1 or type?=?2, then the query represents the decision of a crisis manager about merging departments of the first and second types respectively. If type?=?3, then your task is to determine whether employees xand y work at the same department. Note that x can be equal to y in the query of any type.

Output

For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.

Example

Input
8 6
3 2 5
1 2 5
3 2 5
2 4 7
2 1 2
3 1 7
Output
NO
YES
YES

Almost Union-Find

I hope you know the beautiful Union-Find structure. In this problem, you‘re to implement something similar, but not identical.

The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:

1 p q

Union the sets containing p and q. If p and q are already in the same set, ignore this command.

2 p q

Move p to the set containing q. If p and q are already in the same set, ignore this command

3 p

Return the number of elements and the sum of elements in the set containing p.

Initially, the collection contains n sets: {1}, {2}, {3}, ..., {n}.

Input

There are several test cases. Each test case begins with a line containing two integers n and m (1<=n,m<=100,000), the number of integers, and the number of commands. Each of the next m lines contains a command. For every operation, 1<=p,q<=n. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each type-3 command, output 2 integers: the number of elements and the sum of elements.

Sample Input

5 7
1 1 2
2 3 4
1 3 5
3 4
2 4 1
3 4
3 3

Output for the Sample Input

3 12
3 7
2 8

Explanation

Initially: {1}, {2}, {3}, {4}, {5}

Collection after operation 1 1 2: {1,2}, {3}, {4}, {5}

Collection after operation 2 3 4: {1,2}, {3,4}, {5} (we omit the empty set that is produced when taking out 3 from {3})

Collection after operation 1 3 5: {1,2}, {3,4,5}

Collection after operation 2 4 1: {1,2,4}, {3,5}


Rujia Liu‘s Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

 

ac代码:

 

#include<stdio.h>
int f[200005],next[200005];
int find(int x)
{
    return f[x]==x?x:f[x]=find(f[x]);
}
void join(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy) f[fy]=fx;
}
int main()
{
    int n,q,x,y,z,i,j;
    scanf("%d%d",&n,&q);
    for(i=1;i<=n;i++){
        f[i]=i;
        next[i]=i+1;
    }
    for(i=1;i<=q;i++){
        scanf("%d%d%d",&x,&y,&z);
        if(x==1) join(y,z);
        else if(x==2){
            int fz=find(z);
            for(j=y;j<=z;){
                f[find(j)]=fz;
                int t=j;
                j=next[j];
                next[t]=next[z];    //区间合并
            }
        }
        else{
            if(find(y)==find(z)) printf("YES
");
            else printf("NO
");
        }
    }
    return 0;
}
#include<stdio.h>
int f[200005],id[200005],c[200005],sum[200005];
int dex;
int find(int x)
{
    return f[x]==x?x:f[x]=find(f[x]);
}
void join(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy){
        f[fy]=fx;
        c[fx]+=c[fy];
        sum[fx]+=sum[fy];
    }
}
void del(int x)
{
    int fx=find(id[x]);
    c[fx]--;
    sum[fx]-=x;
    id[x]=++dex;
    f[dex]=dex;
    c[dex]=1;
    sum[dex]=x;    //并查集删除操作
}
int main()
{
    int n,q,x,y,z,i;
    while(~scanf("%d%d",&n,&q)){
        dex=n;
        for(i=1;i<=n;i++){
            f[i]=i;
            id[i]=i;
            c[i]=1;
            sum[i]=i;
        }
        for(i=1;i<=q;i++){
            scanf("%d",&x);
            if(x==1){
                scanf("%d%d",&y,&z);
                join(id[y],id[z]);
            }
            else if(x==2){
                scanf("%d%d",&y,&z);
                int fy=find(id[y]);
                int fz=find(id[z]);
                if(fy!=fz){
                    del(y);
                    join(id[y],id[z]);
                }
            }
            else{
                scanf("%d",&y);
                int fy=find(id[y]);
                printf("%d %d
",c[fy],sum[fy]);
            }
        }
    }
    return 0;
}

 

 













1.(ajaxStart 和 ajaxSend)和 2.(ajaxStop 和 ajaxComplete)有啥区别?

】1.(ajaxStart和ajaxSend)和2.(ajaxStop和ajaxComplete)有啥区别?【英文标题】:What\'sthedifferencebetween:1.(ajaxStartandajaxSend)and2.(ajaxStopandajaxComplete)?1.(ajaxStart和ajaxSend)和2.(ajaxStop和ajaxComplete)有什么区别?【发布时间】:2011-04-1317:56 查看详情

比较和交换与测试和设置

】比较和交换与测试和设置【英文标题】:compareandswapvstestandset【发布时间】:2011-04-0905:30:21【问题描述】:有人可以向我解释一下上述操作在多线程中的工作原理和区别吗?【问题讨论】:【参考方案1】:测试和设置对位进行... 查看详情

java基础8---面向对象代码块和继承和this和super和重写和重载和final

...Java中,使用括起来的代码被称为代码块。根据其位置和声明的不同,可以分为局部代码块,构造代码块,静态代码块,同步代码块。局部代码块在方法中出现;限定变量生命周期,及早释放,提高... 查看详情

setabout和setout和setaside和setback和setoff和setup区别,

参考技术Asetabout/setout都有着手干某事的意思setaboutdoingsth/setouttodosth/setoutonsth都是开始做某事的意思.另外,setoutforaplace/setoutforaplace都有动身到某地的意思,setsthout或setoutsth有陈述,阐明,安排,陈列的意思.setaside意思是留出时间、钱;... 查看详情

CRC和校验和有啥区别?

】CRC和校验和有啥区别?【英文标题】:WhatthedifferencebetweenCRCandchecksum?CRC和校验和有什么区别?【发布时间】:2011-03-2209:16:18【问题描述】:CRC和校验和有什么区别?【问题讨论】:【参考方案1】:CRC(CyclicRedundancyCheck)是checksum... 查看详情

Spring Security 'Roles' 和 'Privileges' 和 Thymeleaf 'hasRole' 和 'hasAuthority'

】SpringSecurity\\\'Roles\\\'和\\\'Privileges\\\'和Thymeleaf\\\'hasRole\\\'和\\\'hasAuthority\\\'【英文标题】:SpringSecurity\'Roles\'and\'Privileges\'andThymeleaf\'hasRole\'and\'hasAuthority\'SpringSecurity\'Roles\'和\'Privileges\' 查看详情

求1-50的偶数和,和奇数和

publicstaticvoidmain(Stringargs[]){intdoubleNumber=0;int singleNumber=0;inti=1; while(i<=50){if(i%2==0){doubleNumber+=i;}else{singNumber+=i; }i++;}System.out.println(doubeNumber);Sys 查看详情

计算1-100所有的数字和,偶数和,奇数和,被7整除的数字和

intsum=0; intouShu=0; intjiShu=0; intn=0; for(inti=1;i<=100;i++){   sum+=i;   if(i%2==0){     ouShu+=i;   }   if(i%2!=0){     jiShu+=i;   }   if(i%7==0){     n+=i;   } } 查看详情

Viewsets `create()` 和 `update()` 和 Serializers `create()` 和 `update()` 有啥区别?

】Viewsets`create()`和`update()`和Serializers`create()`和`update()`有啥区别?【英文标题】:What\'sthedifferencebetweenaViewsets`create()`and`update()`andaSerializers`create()`and`update()`?Viewsets`create()`和`update()`和Serializers`creat 查看详情

inspring和goout和falloff和afterlu

参考技术Ainspring和goout和falloff和afterlu的意思分别为:春上、走出。熄灭。结束。下降,散开,分开。午饭后。午餐后。饭后。午餐後。午饭之后。inspring和inthespring的区别,两者都可以,只是用法不同。按照语法,季节,星期,... 查看详情

使用距离和纬度和经度计算位置

】使用距离和纬度和经度计算位置【英文标题】:Calculatingalocationusingdistanceandlatitudeandlongitude【发布时间】:2021-05-1003:56:05【问题描述】:我有五个纬度和经度以及它们与某个位置的距离。问题是,有什么方法可以使用这些信息... 查看详情

自动装箱和==和equals

一,"=="和equals首先我们明确一下"=="和equals方法的作用。==如果是基本数据类型,则直接对值进行比较基本类型和包装类比较,进行自动拆箱操作如果是引用数据类型,则是对他们的地址进行比较但是只能比较相同类型的对象,... 查看详情

进程和线程之间共享和更新列表

】进程和线程之间共享和更新列表【英文标题】:SharingandUpdatingListbetweenProcessesandThreads【发布时间】:2022-01-1614:48:36【问题描述】:尝试在主进程-主线程和子进程-子线程之间共享和更新列表。目前看来,主进程和主线程共享一... 查看详情

get和post请求及进程和线程及cookie和session的区别

1)get和post请求的区别2)进程和线程的区别3)cookie和session的区别 查看详情

信源编码信源译码和信道编码和译码和加密和解密数字调制和解调和同步

                    信源编码: (1)减少码元数据和降低码元速率   (2) A/D     查看详情

学习和使用svn和github——开篇

  本文是学习和使用SVN和GitHub之开篇,主要讲的有四点内容:  一,介绍基层程序员对SVN功能的认识和使用程度  二,阐述我要学习和使用SVN和GitHub的原因  三,简要描述SVN和GitHub在使用上的区别和简... 查看详情

mysql服务启动和关闭和mysql登陆和退出

Mysql服务启动和关闭第一种方式:右键计算机-->选择管理-->找到服务可以在这里找到MySQL右键启动和停止第二种方式:在开始搜索cmd-->右键使用管理员运行-->输入命令netstartmysql:启动mysql的服务netstopmysql:关闭mysql服务Mys 查看详情

Xamarin:NSMutableUrlRequest 和 NSUrlRequest 与 POST 方法和标头和数据?

】Xamarin:NSMutableUrlRequest和NSUrlRequest与POST方法和标头和数据?【英文标题】:Xamarin:NSMutableUrlRequestandNSUrlRequestwithPOSTmethodandheadersanddata?【发布时间】:2019-08-2102:27:19【问题描述】:我有一个Xamarin项目,我可以在没有标头的情况下... 查看详情