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列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
随机推荐
- ArcGIS10.2 应用服务器搭建
操作系统:Windows Server2012R2 DataCenter 软件环境:ArcGIS Desktop10.2,ArcSDE10.2,ArcGIS Server10.2,win64_11gR ...
- luogg_java学习_06_面向对象特性之封装和继承
这篇博客总结了1天,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 , 因为前不久偶然发现某网站直接复制粘贴我的博客,交谈之后他们修改 ...
- Scalaz(57)- scalaz-stream: fs2-多线程编程,fs2 concurrency
fs2的多线程编程模式不但提供了无阻碍I/O(java nio)能力,更为并行运算提供了良好的编程工具.在进入并行运算讨论前我们先示范一下fs2 pipe2对象里的一些Stream合并功能.我们先设计 ...
- OpenSAML
一.背景知识: SAML即安全断言标记语言,英文全称是Security Assertion Markup Language.它是一个基于XML的标准,用于在不同的安全域(security domain ...
- Windows下安装Nginx+php+mysql环境
系统:Windows 7 64位系统 安装之前,首先下载软件: Nginx: http://nginx.org/en/download.html PHP Stable PHP 5.6.26: http ...
- 使用 Sticky-Kit 实现基于 jQuery 的元素固定效果
元素固定效果在网页中应用得很多,比较常见的使用场景有改进导航,显示广告.Sticky-Kit 是一个非常方便的 jQuery 插件,简化了创建/管理粘元素,有复杂的使用功能.这些功能包括:处理多个固定 ...
- div+css背景渐变色代码示例
用CSS使DIV背景颜色渐变,适用于IE和Chrome等浏览器. 从黄到红示例:http://keleyi.com/keleyi/phtml/divcss/2.htm 代码: <style ty ...
- JavaScript基本语法(三)
上篇博文说到JS的运算符,这次说说JS程序流程控制. 1. 条件语句 if 语法: if(condition) statements1 else statement2 当括号里的条件成立的时候,执行i ...
- iOS之微信支付
前言:下面介绍微信支付的开发流程的细节,图文并茂,你可以按照我的随笔流程过一遍代码.包你也学会了微信支付.而且支付也是面试常问的内容. 正文: 1.首先在开始使用微信支付之前,有一些东西是开发者必须要 ...
- React Native之FlexBox介绍和使用
# 前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会 ...