标题效果:

三操作。

1. 合并两个集合

2.代替所述第二组的第一个元素

3.输出设置数量,并。。

IDEAS:

使用p该元素的记录数,其中集合,建立并查集。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring> using namespace std;
typedef long long LL;
int set[111111];
int cnt[111111];
LL sum[111111];
int p[111111]; int find(int x)
{
if(x!=set[x])
set[x]=find(set[x]);
return set[x];
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)set[i]=i,cnt[i]=1,sum[i]=i,p[i]=i; while(m--)
{
int op;
scanf("%d",&op);
int l,r;
if(op==1)
{
scanf("%d%d",&l,&r);
int xx=find(p[l]);
int yy=find(p[r]);
if(xx==yy)continue;
else
{
set[xx]=yy; cnt[yy]+=cnt[xx];
sum[yy]+=sum[xx];
}
}
else if(op==2)
{
scanf("%d%d",&l,&r);
int xx=find(p[l]);
int yy=find(p[r]);
if(xx==yy)continue;
else {
sum[xx]-=l;
cnt[xx]--;
sum[yy]+=l;
cnt[yy]++;
p[l]=yy;
}
}
else
{
scanf("%d",&l);
printf("%d %lld\n",cnt[find(p[l])],sum[find(p[l])]);
}
}
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

uva 11987 Almost Union-Find (并检查集合)的更多相关文章

  1. UVA - 11987 Almost Union-Find[并查集 删除]

    UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...

  2. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

  3. HDU 1272 小希迷宫(并检查集合)

    意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...

  4. UNION(并集)集合运算

    在集合论中,两个集合(记为集合A和B)的并集是一个包含集合A和B中所有元素的集合.换句话说,如果一个元素属于任何一个输入集合,那么它也属于结果集. 在T-SQL中,UNION 集合运算可以将两个输入查 ...

  5. UVa 11987 Almost Union-Find(支持删除操作的并查集)

    传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...

  6. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  7. URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)

    意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...

  8. CodeForces 277A Learning Languages (并检查集合)

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...

  9. UVa 11987 Almost Union-Find (虚拟点)【并查集】

    <题目链接> 题目大意: 刚开始,1到n个集合中分别对应着1~n这些元素,然后对这些集合进行三种操作: 输入 1 a b 把a,b所在的集合合并 输入 2 a b 把b从b所在的旧集合移到 ...

随机推荐

  1. (五岁以下儿童)NS3样本演示:桥模块演示样品csma-bridge.cc凝视程序

    (五岁以下儿童)NS3:桥模块演示样品csma-bridge.cc凝视程序 1.Ns3 bridge模csma-bridge.cc演示示例程序的目光 // Network topology // // ...

  2. ZOJ Monthly, October 2010 ABEFI

    ZOJ 3406 Another Very Easy Task #include <cstdio> #include <cstring> const int N = 10000 ...

  3. UVa 12683 Odd and Even Zeroes(数论+数字DP)

    意甲冠军: 要求 小于或等于n号码 (0<=n <= 1e18)尾数的数的阶乘0数为偶数 思考:当然不是暴力,因此,从数论.尾数0数为偶数,然后,它将使N阶乘5电源是偶数.(二指数肯定少5 ...

  4. 【C语言探索之旅】 第一部分第八课:第一个C语言小游戏

    ​ 内容简介 1.课程大纲 2.第一部分第八课:第一个C语言小游戏 3.第一部分第九课预告: 函数 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写 ...

  5. 【iOS】UIViewController生命周期

    UIViewController有2周期: 在UIViewController中,View存在两个循环:载入循环和卸载循环. 载入循环 1>程序请求controller的view. 2>假 ...

  6. 【PLSQL】变量声明,结构语句,cursor游标

    ************************************************************************   ****原文:blog.csdn.net/clar ...

  7. 如何识别SQL Server中的CPU瓶颈

    原文:如何识别SQL Server中的CPU瓶颈 原文出自: http://www.mssqltips.com/sqlservertip/2316/how-to-identify-sql-server ...

  8. Atitit.web三编程模型 Web Page Web Forms 和 MVC

    Atitit.web三编程模型 Web Page    Web Forms 和 MVC 1. 编程模型是 Web Forms 和 MVC (Model, View, Controller). 2. W ...

  9. Android使用代码消除App数据并重新启动设备

    /** * 使用代码消除App数据 * 我们不寻常的清除App数据,中找到相应的App * 然后选择其清除数据.以下给出代码实现. * * 注意事项: * 1 设备须要root * 2 该演示样例中删 ...

  10. MongoDB(两)mongoDB基本介绍

    MongoDB介绍 MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库其中功能最丰富,最像关系数据库的.他支持的数据结构很的松散,是类似json的bjson格式,因此能够存储比 ...