UVA-11987
I hope you know the beautiful Union-Find structure. In this problem, you’re to implement somethingsimilar, but not identical.The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:1 p q Union the sets containing p and q. If p and q are already in the same set,ignore this command.2 p q Move p to the set containing q. If p and q are already in the same set,ignore this command.3 p Return the number of elements and the sum of elements in the set containingp.Initially, the collection contains n sets: {1}, {2}, {3}, . . . , {n}.InputThere 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 linescontains a command. For every operation, 1 ≤ p, q ≤ n. The input is terminated by end-of-file (EOF).OutputFor each type-3 command, output 2 integers: the number of elements and the sum of elements.ExplanationInitially: {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 whentaking 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 Input5 71 1 22 3 41 3 53 42 4 13 43 3Sample Output3 123 72 8
AC代码为:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 10;
int father[maxn], id[maxn], sum[maxn], cnt[maxn];
int n, m, p, q, dt, temp;
int Find(int a)
{
return a == father[a] ? a : Find(father[a]);
}
void Union_set(int a, int b)
{
int x = Find(a);
int y = Find(b);
if (x != y)
{
father[y] = x;
cnt[x] += cnt[y];
sum[x] += sum[y];
}
}
void Move_set(int a)
{
int fa = Find(id[a]);
sum[fa] -= a;
cnt[fa]--;
id[a] = ++temp;
father[id[a]] = temp;
cnt[id[a]] = 1;
sum[id[a]] = a;
}
int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
temp = n;
for (int i = 0; i <= n; i++)
{
father[i] = i;
sum[i] = i;
id[i] = i;
cnt[i] = 1;
}
while (m--)
{
cin >> dt;
if (dt == 1)
{
cin >> p >> q;
Union_set(id[p], id[q]);
}
else if (dt == 2)
{
cin >> p >> q;
int t1 = Find(id[p]);
int t2 = Find(id[q]);
if (t1 != t2)
{
Move_set(p);
Union_set(id[p], id[q]);
}
}
else
{
cin >> p;
int fat = Find(id[p]);
cout << cnt[fat] << " " << sum[fat] << endl;
}
}
}
return 0;
}
UVA-11987的更多相关文章
- UVA - 11987 Almost Union-Find[并查集 删除]
UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...
- UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...
- UVa 11987 Almost Union-Find(支持删除操作的并查集)
传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...
- 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 11987 Almost Union-Find (并检查集合)
标题效果: 三操作. 1. 合并两个集合 2.代替所述第二组的第一个元素 3.输出设置数量,并.. IDEAS: 使用p该元素的记录数,其中集合,建立并查集. #include <cstdio& ...
- UVa 11987 Almost Union-Find (虚拟点)【并查集】
<题目链接> 题目大意: 刚开始,1到n个集合中分别对应着1~n这些元素,然后对这些集合进行三种操作: 输入 1 a b 把a,b所在的集合合并 输入 2 a b 把b从b所在的旧集合移到 ...
- UVA - 11987 Almost Union-Find(带删除的并查集)
I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something s ...
- UVA 11987 Almost Union-Find (单点修改的并查集)
此题最难处理的操作就是将一个单点改变集合,而普通的并查集是不支持这种操作的. 当结点p是叶子结点的时候,直接pa[p] = root(q)是可以的, p没有子结点,这个操作对其它结点不会造成任何影响, ...
- UVA - 11987 Almost Union-Find 并查集的删除
Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...
随机推荐
- Springboot 自动配置浅析
Introduction 我们知道,SpringBoot之所以强大,就是因为他提供了各种默认的配置,可以让我们在集成各个组件的时候从各种各样的配置文件中解放出来. 拿一个最普通的 web 项目举例.我 ...
- Linux 部署Nginx反向代理服务 使用openssl自生成证书并配置https
1.安装Nginx编译所依赖的包 正常centos中可以使用yum安装一下依赖包: yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel ...
- [java] 集合的架构——1张图表示
- Mybatis精讲(二)---生命周期
目录 回顾 SqlSessionFactoryBuilder SqlSessionFactory openSessionFromDataSource Executor SqlSession Mappe ...
- pip install xxx Could not fetch URL https://pypi.org/simple/pip/
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirmingthe ssl certificate: ...
- Kotlin Coroutines不复杂, 我来帮你理一理
Coroutines 协程 最近在总结Kotlin的一些东西, 发现协程这块确实不容易说清楚. 之前的那篇就写得不好, 所以决定重写. 反复研究了官网文档和各种教程博客, 本篇内容是最基础也最主要的内 ...
- webpack3、4的基本的使用方法
webpack的基本使用 webpack的安装 webpack的使用时需要借助 node 的环境的 在 node 中自动下载了 npm 这个包管理工具,之后的操作我们需要使用npm包管理工具进行相关操 ...
- PHP安全之道学习笔记1:PHP项目安全设置
在全球范围来看,超过了80%的网站是使用php进行搭建的,由于脚本语言和早期版本设计的诸多原因,php项目存在不少安全隐患.从配置选项来看,可以做如下的优化. 1.屏蔽PHP错误输出. 在/etc/p ...
- 快速搭建 SpringCloud 微服务开发环境的脚手架
本文适合有 SpringBoot 和 SpringCloud 基础知识的人群,跟着本文可使用和快速搭建 SpringCloud 项目. 本文作者:HelloGitHub-秦人 HelloGitHub ...
- 词袋模型(BOW,bag of words)和词向量模型(Word Embedding)概念介绍
例句: Jane wants to go to Shenzhen. Bob wants to go to Shanghai. 一.词袋模型 将所有词语装进一个袋子里,不考虑其词法和语序的问题,即每个 ...