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. springMVC项目访问URL链接时遇到某一段然后忽略后面的部分

    背景:一个链接URL:http:localhost/tq/asf/218732,配置URL使遇到/asf后直接跳转不识别/asf后面的218732 因为是在ssm框架下做的项目,所以用spring的注 ...

  2. es6 Object.assign(target, ...sources)

    Object.assign() 方法用于将所有可枚举属性(对象属性)的值从一个或多个源对象复制到目标对象.它将返回目标对象. 语法 Object.assign(target, ...sources) ...

  3. 从mysql8.0.15升级到8.0.16

    从mysql8.0.15升级到8.0.16 环境简介 操作系统:Centos 6.10 64位 目前版本:8.0.15 MySQL Community Server 二进制 目的:升级为8.0.16 ...

  4. python语言特性简要记载

    1.python是解释型语言,而c,c++等是编译型语言. 2.python是动态类型语言,这意味着你不需要在声明变量时指定类型. 3.Python是面向对象语言,所有允许定义类并且可以继承和组合.P ...

  5. 【leetcode】Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  6. sh_06_break

    sh_06_break i = 0 while i < 10: # break 某一条件满足时,退出循环,不再执行后续重复的代码 # i == 3 if i == 3: break print( ...

  7. Linux root用户密码重置,远程登陆,文件基本属性

    Linux root用户密码重置,远程登陆,文件基本属性 忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码 ...

  8. Java 生成二进制加减法题目

    日常算数,有益身心健康. int a; int b; int result; int symbol; int count = 50; Random random = new Random(); for ...

  9. spring cloud:config-eureka-refresh-bus-rabbitmq

    config-server-eureka-bus-rabbitmq 1. File-->new spring starter project 2.add dependency <paren ...

  10. python正则之match search findall

    match:只匹配一次,开头匹配不上,则不继续匹配 a,b,\w+ match(a,"abcdef") 匹配a >>> re.match("a" ...