UVA11987 带删除并查集
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 带删除并查集的更多相关文章
- 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 ...
- BZOJ4358: permu(带撤销并查集 不删除莫队)
题意 题目链接 Sol 感觉自己已经老的爬不动了.. 想了一会儿,大概用个不删除莫队+带撤销并查集就能搞了吧,\(n \sqrt{n} logn\)应该卡的过去 不过不删除莫队咋写来着?....跑去学 ...
- Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理
题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★ 输入文件:marbles.in 输出 ...
- 带撤销并查集 & 可持久化并查集
带撤销并查集支持从某个元素从原来的集合中撤出来,然后加入到一个另外一个集合中,或者删除该元素 用一个映射来表示元素和并查集中序号的关系,代码中用\(to[x]\) 表示x号元素在并查集中的 id 删除 ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
随机推荐
- Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)
D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...
- docker命令(随时补充)
导入导出容器:https://blog.csdn.net/LEoe_/article/details/78685156 拷贝文件到容器内:docker cp 本地路径 容器长ID:容器路径
- Codeforces 984 扫雷check 欧几里得b进制分数有限小数判定 f函数最大连续子段
A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ...
- vim替换
:%s/mxmlElementGetAttr/xml_get_attr/g :{作用范围}s/{目标}/{替换}/{替换标志} 例如:%s/foo/bar/g会在全局范围(%)查找foo并替换为bar ...
- ipv4固定ip地址
1.vi /etc/sysconfig/network-scripts/ifcfg-enp7s0f0 ##在后面添加ip和域名解析IPADDR="192.168.130.34" ...
- app 进入后台进行模糊处理
金融类app防止信息在后台中被一些恶意截屏软件进行截屏,对进入后台的app做模糊处理 - (void)applicationWillResignActive:(UIApplication *)appl ...
- null 的应用
它是 Oracle 中非常特殊的一种类型.它表示不确定,表示没有值.并且它能转化成所有的类型. 向数据库中插入空字符串时,oracle 会把它自动转化为 null 类型.所以,在查询空字符的时候: s ...
- Rsync服务端部署流程
Rsync服务端部署流程 Rsync服务端部署流程: 一.rsync服务端配置流程 配置rsync配置文件/etc/rsyncd.conf 创建同步的本地目录/dingjian 并根据需要 ...
- pyqt5-QTDesigner--控件操作
Edit菜单 编辑小伙伴.用鼠标直接拖 编辑控件---样式等等. 点击需要编辑的控件---> 信号与槽 先用鼠标从控件往外拖---> --->选中相应的信 ...
- mysql——二级索引(辅助索引)
二级索引:叶子节点中存储主键值,每次查找数据时,根据索引找到叶子节点中的主键值,根据主键值再到聚簇索引中得到完整的一行记录. 问题: 1.相比于叶子节点中存储行指针,二级索引存储主键值会占用更多的空间 ...