1~n,n个数,初始每个数独自作为一个集合,然后进行m次操作。
操作有三种:
1 p q :把 p 所在的集合合并到 q 所在的集合

2 p q :把 p 从 p 的集合中拿出,放到 q 的集合里

3 p :输出 p 所在的集合的元素个数和元素之和

Sample Input
5 7
1 1 2
2 3 4
1 3 5
3 4
2 4 1
3 4
3 3

Sample Output
3 12
3 7
2 8

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define clc(a,b) memset(a,b,sizeof(a))
#define fora(i,a,b) for(i=a;i<b;i++)
#define fors(i,a,b) for(i=a;i>b;i--)
#define fora2(i,a,b) for(i=a;i<=b;i++)
#define fors2(i,a,b) for(i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f typedef long long LL;
typedef long long LD;
using namespace std;
const int maxn= 100000+11;
map<int,int>ma;
int cou[maxn],sum[maxn];
int fa[maxn];
int n,m;
void init()
{
ma.clear();
int i;
fora2(i,1,n)
{
fa[i]=i;//第i个点的父亲是i
cou[i]=1;//第i个集合个数为1
sum[i]=i;//第i个集合总和为i
ma[i]=i;//第i个点属于第i个集合
}
}
int findx(int x)
{
if(x==fa[x])return x;
return fa[x]=findx(fa[x]);
}
int main()
{
int kcase=0;
while(~scanf("%d%d",&n,&m))
{
init();
while(m--)
{
int op;
scanf("%d",&op);
if(op==3) //输出 p 所在的集合的元素个数和元素之和
{
int x;
scanf("%d",&x);
int fx=findx(ma[x]);
printf("%d %d\n",cou[fx],sum[fx]);
continue;
}
int x,y;
scanf("%d%d",&x,&y);
int fx=findx(ma[x]),fy=findx(ma[y]);
if(fx==fy)continue;
if(op==1) //1 p q :把 p 所在的集合合并到 q 所在的集合
{
//合并连通分支fx和fy
fa[fx]=fy;
cou[fy]+=cou[fx];
sum[fy]+=sum[fx];
//清空fx
cou[fx]=0;
sum[fx]=0;
continue;
}
//把x从集合ma[x]拿出来
//2 p q :把 p 从 p 的集合中拿出,放到 q 的集合里
sum[fx]-=x;
cou[fx]--;
//把x放到集合ma[y]
ma[x]=ma[y];
cou[fy]++;
sum[fy]+=x; }
}
return 0;
}

  

UVA11987 带删除并查集的更多相关文章

  1. UVA11987 Almost Union-Find [带权并查集]

    洛谷传送门 Almost Union-Find 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 5 7 1 1 2 2 3 4 1 3 5 3 4 2 4 1 3 4 3 ...

  2. BZOJ4358: permu(带撤销并查集 不删除莫队)

    题意 题目链接 Sol 感觉自己已经老的爬不动了.. 想了一会儿,大概用个不删除莫队+带撤销并查集就能搞了吧,\(n \sqrt{n} logn\)应该卡的过去 不过不删除莫队咋写来着?....跑去学 ...

  3. Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理

    题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★   输入文件:marbles.in   输出 ...

  4. 带撤销并查集 & 可持久化并查集

    带撤销并查集支持从某个元素从原来的集合中撤出来,然后加入到一个另外一个集合中,或者删除该元素 用一个映射来表示元素和并查集中序号的关系,代码中用\(to[x]\) 表示x号元素在并查集中的 id 删除 ...

  5. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  6. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  7. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  8. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  9. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

随机推荐

  1. (转) weblogic 域,管理服务器,受管服务器,集群和机器的基本知识

    weblogic 域,管理服务器,受管服务器,集群和机器的基本知识 1.域(Domain) •它是什么? –是一个逻辑上管理的WebLogic Server组,这些组从管理上当作一个整体来操作 •域里 ...

  2. ng-model 和ng-bind的区别

    也就是说 ng-model是绑定html输入的值-->到控制器的变量,输入值变了,控制器对应的变量message的值页变了,这样,在其他地方就可以使用这个变化后的值 ng-bind 是绑定控制器 ...

  3. bootstrap-table(2)问题集

    参考;https://www.cnblogs.com/landeanfen/p/4993979.html 1.分页参数sidePagination 如果是服务端分页,返回的结果必须包含total.ro ...

  4. 清华集训2014 sum

    清华集训2014sum 求\[∑_{i=1}^{n}(-1)^{⌊i√r⌋}\] 多组询问,\(n\leq 10^9,t\leq 10^4, r\leq 10^4\). 吼题解啊 具体已经讲得很详细了 ...

  5. 【UOJ#450】[集训队作业2018] 复读机

    题目链接 题目描述 群里有\(k\)个不同的复读机.为了庆祝平安夜的到来,在接下来的\(n\)秒内,它们每秒钟都会选出一位优秀的复读机进行复读.非常滑稽的是,一个复读机只有总共复读了\(d\)的倍数次 ...

  6. Vue全局混入

    混入 (mixins) 是一种分发 Vue 组件中可复用功能的非常灵活的方式.混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项. 数据对象合并 数据对象在 ...

  7. vue作业2

    """ 2.现有以下成绩单数据 scores = [ { name: 'Bob', math: 97, chinese: 89, english: 67 }, { nam ...

  8. MySQL报错:Cause: java.sql.SQLException: Incorrect string value: '\xE6\x9D\xA8","...' for column 'obj_value' at row 1

    1.插入MySQL表时,报错:Cause: java.sql.SQLException: Incorrect string value: '\xE6\x9D\xA8","...' ...

  9. 工作笔记--js-点赞按钮和踩踩按钮互斥??怎么写?

    效果图: html: css: .an{ margin-top:0px; position: relative; .popzframe,.popcframe{ display: none; word- ...

  10. 获取当前国家与ip地址

    JS获取当前国家示例: <script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"& ...