UVA - 11987 Almost Union-Find[并查集 删除]
| UVA - 11987 |
I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something similar, but not identical.
The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:
|
1pq |
Union the sets containing p and q. If p and q are already in the same set, ignore this command. |
|
2pq |
Move p to the set containing q. If p and q are already in the same set, ignore this command. |
|
3p |
Return the number of elements and the sum of elements in the set contain- ing p. |
Initially, the collection contains n sets: {1}, {2}, {3}, . . . , {n}. Input
There are several test cases. Each test case begins with a line containing two integers n and m (1 ≤ n, m ≤ 100, 000), the number of integers, and the number of commands. Each of the next m lines contains a command. For every operation, 1 ≤ p, q ≤ n. The input is terminated by end-of-file (EOF).
Output
For each type-3 command, output 2 integers: the number of elements and the sum of elements.
Explanation
Initially: {1}, {2}, {3}, {4}, {5}
Collection after operation 1 1 2: {1,2}, {3}, {4}, {5}
Collection after operation 2 3 4: {1,2}, {3,4}, {5} (we omit the empty set that is produced when
taking out 3 from {3})
Collection after operation 1 3 5: {1,2}, {3,4,5}
Collection after operation 2 4 1: {1,2,4}, {3,5}
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
37
28
白书
删除操作
用一个id,删除时给被删除的点重新分配id,旧的父亲和新的父亲分别更新行了
注意本题权值就是自己
沙茶的查询忘加id了.............
//
// main.cpp
// uva11987
//
// Created by Candy on 10/12/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=1e5+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,op,p,q;
int fa[N],sum[N],id[N],cnt[N],num=;
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
inline void unn(int x,int y){
int f1=find(id[x]),f2=find(id[y]);
if(f1!=f2){
fa[f1]=f2;
sum[f2]+=sum[f1];
cnt[f2]+=cnt[f1];
}
}
inline void move(int x,int y){
int f1=find(id[x]),f2=find(id[y]);
if(f1!=f2){
sum[f1]-=x;
cnt[f1]--;
id[x]=++num;
sum[f2]+=x;
cnt[f2]++;
fa[id[x]]=f2;
}
}
int main(int argc, const char * argv[]) {
while(scanf("%d%d",&n,&m)!=EOF){
num=n;
for(int i=;i<=n;i++) {fa[i]=sum[i]=id[i]=i;cnt[i]=;}
for(int i=;i<=m;i++){
op=read();
if(op==){
p=read();q=read();
unn(p,q);
}else if(op==){
p=read();q=read();
move(p,q);
}else{
p=read();
p=find(id[p]);
printf("%d %d\n",cnt[p],sum[p]);
}
}
} return ;
}
UVA - 11987 Almost Union-Find[并查集 删除]的更多相关文章
- UVA 11987 Almost Union-Find (并查集+删边)
开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...
- 并查集(删除) UVA 11987 Almost Union-Find
题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- UVa 1455 Kingdom 线段树 并查集
题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- UVA 1493 Draw a Mess(并查集+set)
这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
随机推荐
- 在DOS使用SVN之执行命令整理(TortoiseProc.exe)
原文链接: http://www.cnblogs.com/andrew-blog/archive/2012/08/21/SVN_DOS_Commands.html TortoiseSVN因为所有的命令 ...
- SqlServer-无限递归树状图结构设计和查询
在现实生活中,公司的部门设计会涉及到很多子部门,然后子部门下面又存在子部门,形成类似判断的树状结构,比如说评论楼中楼的评论树状图,职位管理的树状图结构等等,实现类似的树状图数据结构是在开发中经常出现的 ...
- 关于在云服务器上部署tomcat笔记
tomcat无法启动的原因有:1.由于在tomcat里JVM没有配好,他默认的是批向c:\program file\Java下的那一个,如果你确认 这没有删掉,JVM指向你自己安装的jdk\jre试试 ...
- 用python来个百度关键词刷排名脚本
目的:写个脚本来提升百度排名 我一个seo届前辈的朋友找我,他说,seo事无巨细,自己主观方面能做的几乎都能做了,提升百度等搜索引擎中的排名往往效果不佳或者起效周期慢.能不能人为去干预下呢? 获得排名 ...
- 第一弹:Java 中创建对象的4种方式
Java 是面向对象的语言,不可避免的,"对象"这个概念是 Java 语言的核心部分,这里来简单讨论一下在 Java 中创建一般对象的方法. 总结下来有以下4种创建对象的方法: 使 ...
- SSH整合(struts2.3.24+hibernate3.6.10+spring4.3.2+mysql5.5+myeclipse8.5+tomcat6+jdk1.6)
终于开始了ssh的整合,虽然现在比较推崇的是,ssm(springmvc+spring+mybatis)这种框架搭配确实比ssh有吸引力,因为一方面springmvc本身就是遵循spring标准,所以 ...
- HTML思维导图
- Netty(二)入门
在上篇<Netty(一)引题>中,分别对AIO,BIO,PIO,NIO进行了简单的阐述,并写了简单的demo.但是这里说的简单,我也只能呵呵了,特别是NIO.AIO(我全手打的,好麻烦). ...
- 【再探backbone04】单页应用的基石-路由处理
前言 首先发一点牢骚,互联网公司变化就是快,我去一个团队往往就一年时间该团队就得解散,这不,公司居然把无线团队解散了,我只能说,我那个去!!! 我去年还到处让人来呢,一个兴兴向荣的团队说没就没了啊!我 ...
- Windows TCP连接数限制解决
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters" /v "M ...