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[并查集 删除]的更多相关文章

  1. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  2. 并查集(删除) UVA 11987 Almost Union-Find

    题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...

  3. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  4. 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 ...

  5. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  6. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

  7. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  8. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  9. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

随机推荐

  1. Netbeans 8.2关于PHP的新特性

    Netbeans 8.2在这个国庆期间终于发布了,其与PHP相关的新特性主要有: 支持PHP 7 详见前面翻译的一篇文章:Netbeans 8.2将支持PHP 7 编辑器功能增强 文档好像没有明确说明 ...

  2. 【shell 大系】Linux Shell常用技巧

    在最近的日常工作中由于经常会和Linux服务器打交道,如Oracle性能优化.我们数据采集服务器的资源利用率监控,以及Debug服务器代码并解决其效率和稳定性等问题.因此这段时间总结的有关Linux ...

  3. 如何使 WebAPI 自动生成漂亮又实用在线API文档

    1.前言 1.1 SwaggerUI SwaggerUI 是一个简单的Restful API 测试和文档工具.简单.漂亮.易用(官方demo).通过读取JSON 配置显示API. 项目本身仅仅也只依赖 ...

  4. 改善SQL语句(转)

    二.改善SQL语句          很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如:   select * from ta ...

  5. CSS手动改变DIV高宽

    本实例代码可以使DIV可以手动改变大小 效果体验:http://hovertree.com/code/css/resize.htm 代码如下: <!DOCTYPE html> <ht ...

  6. React入门最好的学习实例-TodoList

    前言 React 的核心思想是:封装组件,各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件. 最近前端界闹的沸沸扬扬的技术当属react了,加上项目需要等等原因,自己也决定花些时间来好 ...

  7. C#图片处理常见方法性能比较

    C#图片处理常见方法性能比较 来自:http://www.cnblogs.com/sndnnlfhvk/archive/2012/02/27/2370643.html   在.NET编程中,由于GDI ...

  8. android FrameLayout详解

    首先看演示: FrameLayout框架布局是最简单的布局形式.所有添加到这个布局中的视图都以层叠的方式显示.第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆 ...

  9. Mac版PhpStorm之XAMPP整合apache服务器配置

    版权声明:本文为博主原创文章,未经博主允许不得转载. 选择在PhpStorm集成apache服务器,下面是我自己的亲测的步骤. 1.如何修改apache默认端口 xampp apache默认的http ...

  10. php关键词替换的类(避免重复替换,保留与还原原始链接)

    转载:http://www.169it.com/blog_article/601549531.html 本节主要内容:一个关键词替换的类 主要可以用于关键词过滤,或关键词查找替换方面. 实现过程分析: ...