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. Python修炼之路-模块

    模块 模块与包 模块:用来从逻辑上组织python代码(可以定义变量.函数.类.逻辑:实现一个功能),本质就是.py结尾的python文件. 例如,文件名:test.py,对应的模块名为:test 包 ...

  2. Vue框架使用sass

    引入: cnpm install node-sass --save-dev //安装node-sass cnpm install sass-loader@7.3.1 --save-dev cnpm i ...

  3. 集合综合练习<二>

    package com.JiHeTotal; import java.util.Map; public class Student { int id; String name; Map<Stri ...

  4. C++常用速查

    int main() { int arr[2][5] = { {1,8,12,20,25}, {5,9,13,24,26} }; } void f(double p[][10]) { } #inclu ...

  5. Java基础——面试、笔试

    网址来源: http://www.nowcoder.com/discuss/5949?type=0&order=0&pos=4&page=2 参考资料:(java方面的一些面试 ...

  6. DOM操作元素

    DOM 操作元素 JavaScript的DOM操作可以改变网页内容.结构和样式.我们可以利用DOM操作元素来改变元素里面的内容.属性等. DOM操作元素: 一.操作元素:(一)innerText .( ...

  7. BZOJ 4897: [Thu Summer Camp2016]成绩单 动态规划

    Description 期末考试结束了,班主任L老师要将成绩单分发到每位同学手中.L老师共有n份成绩单,按照编号从1到n的顺序叠 放在桌子上,其中编号为i的成绩单分数为w_i.成绩单是按照批次发放的. ...

  8. codevs 1002 搭桥x

    题目描述 Description 有一矩形区域的城市中建筑了若干建筑物,如果某两个单元格有一个点相联系,则它们属于同一座建筑物.现在想在这些建筑物之间搭建一些桥梁,其中桥梁只能沿着矩形的方格的边沿搭建 ...

  9. dell笔记本 win10 下安装 ubuntu16.04 踩坑记录

    硬件配置情况: dell笔记本-灵越-5577 —— I5七代(带有集显),8G内存条DDR4,GTX1050,128G固态硬盘,1T机械硬盘. 固态硬盘划分为3部分,100GB给win10的C盘,1 ...

  10. 苹果cms开启防红跳转后,提示模板文件不存在解决方法

    1,苹果cms开启防红跳转后,提示模板文件不存在(如下图)这是因为你使用的模板里面缺少苹果cms自带的防红跳转模板导致,遇到这种状况后需要把苹果cms默认自带的( template/default_p ...