UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find
题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和
思路:并查集,关键在于操作2,对于并查集,要去除掉一个结点,假设该结点不是根那就好办了,那么就多开n个结点,每一个结点初始父亲都是它的i + n,这样在移动的时候,就不用操心他是根结点了剩下就是普通的带权并查集了
代码:
#include <cstdio>
#include <cstring> const int N = 200005; int n, m, parent[N], num[N], sum[N]; int find(int x) {
return x == parent[x] ? x : parent[x] = find(parent[x]);
} void init() {
for (int i = 0; i <= n; i++) {
parent[i] = parent[i + n] = i + n;
sum[i] = sum[i + n] = i;
num[i] = num[i + n] = 1;
}
} int main() {
while (~scanf("%d%d", &n, &m)) {
int q, a, b;
init();
while (m--) {
scanf("%d", &q);
if (q == 1) {
scanf("%d%d", &a, &b);
int pa = find(a);
int pb = find(b);
if (pa == pb) continue;
parent[pa] = pb;
num[pb] += num[pa];
sum[pb] += sum[pa];
}
else if (q == 2) {
scanf("%d%d", &a, &b);
int pa = find(a);
int pb = find(b);
if (pa == pb) continue;
parent[a] = pb;
num[pa]--;
num[pb]++;
sum[pa] -= a;
sum[pb] += a;
}
else {
scanf("%d", &a);
int pa = find(a);
printf("%d %d\n", num[pa], sum[pa]);
}
}
}
return 0;
}
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 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列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
- <算法><Union Find并查集>
Intro 想象这样的应用场景:给定一些点,随着程序输入,不断地添加点之间的连通关系(边),整个图的连通关系也在变化.这时候我们如何维护整个图的连通性(即判断任意两个点之间的连通性)呢? 一个比较简单 ...
- UVA 1329 Corporative Network【并查集】
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
随机推荐
- 【原创】leetCodeOj --- Interleaving String 解题报告
题目地址: https://oj.leetcode.com/problems/interleaving-string/ 题目内容: Given s1, s2, s3, find whether s3 ...
- How to fix Column 'InvariantName' is constrained to be unique 解决办法!
Introduction When you build a web project that uses Enterprise Library Community for the Application ...
- 北邮iptv用WindowsMediaplayer打不开的解决的方法
前言:之前我的iptv能够用,可是有次我安装了realplayer,它就偷偷把iptv文件的默认打开方式给篡改了,卸载了 realplayer之后,iptv不能直接用 ...
- 返璞归真 asp.net mvc (4) - View/ViewEngine
原文:返璞归真 asp.net mvc (4) - View/ViewEngine [索引页] [源码下载] 返璞归真 asp.net mvc (4) - View/ViewEngine 作者:web ...
- Simple Automated Backups for MongoDB Replica Sets
There are a bunch of different methods you can use to back up your MongoDB data, but if you want to ...
- RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools
Lab 10 Understanding the Configuration Tools Sequence 1: Configuring the Network with system-config- ...
- 查看oracle数据库的连接数以及用户
查看oracle数据库的连接数以及用户 11.查询oracle的连接数2select count(*) from v$session;32.查询oracle的并发连接数4select count(*) ...
- Freedur为什么免费?
难道没有人看到他们官方网站权? Freedur倒闭了...... 一个中国人.Chris Lee,作为Freedur的会计师.窃取了公司的银行帐号.并将Freedur的官方站点指向自己的空间. 而且声 ...
- UVA 12206 - Stammering Aliens(后缀数组)
UVA 12206 - Stammering Aliens 题目链接 题意:给定一个序列,求出出现次数大于m,长度最长的子串的最大下标 思路:后缀数组.搞出height数组后,利用二分去查找就可以 这 ...
- CSS——(2)与标准流盒模型
部分博客<CSS--(1)基础>中简介了CSS的概念和几种用法,如今主要是介绍其的核心内容. 盒子模型 为了理解盒子模型,我们能够先从生活中的盒子入手.盒子是用来放置物品的,内部除了有物品 ...