题目传送门

题意:训练指南P246

分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] = ++pos(可多次),这样id[p]就相当于是新的一个点,那么在Find(x)寻找祖先时要用x的马甲来寻找,即Find (id[x])。

#include <bits/stdc++.h>
using namespace std; const int N = 2e5 + 5;
int rt[N], sz[N], sum[N], id[N/2];
int n, m; void init() {
memset (rt, -1, sizeof (rt));
for (int i=1; i<N; ++i) {
sz[i] = 1; sum[i] = i;
}
for (int i=1; i<=n; ++i) id[i] = i;
} int Find(int x) {
return rt[x] == -1 ? x : rt[x] = Find (rt[x]);
} int main(void) {
while (scanf ("%d%d", &n, &m) == 2) {
init ();
int op, p, q, pos = n;
for (int i=1; i<=m; ++i) {
scanf ("%d%d", &op, &p);
if (op == 3) {
int fp = Find (id[p]);
printf ("%d %d\n", sz[fp], sum[fp]);
}
else {
scanf ("%d", &q);
int fp = Find (id[p]), fq = Find (id[q]);
if (op == 1) {
if (fp != fq) {
rt[fp] = fq;
sz[fq] += sz[fp];
sum[fq] += sum[fp];
}
}
else {
if (fp != fq) {
sz[fp]--; sum[fp] -= p;
id[p] = ++pos; //马甲!
rt[id[p]] = fq;
sz[fq]++; sum[fq] += p;
}
}
}
}
} return 0;
}

  

并查集(删除) UVA 11987 Almost Union-Find的更多相关文章

  1. UVA - 11987 Almost Union-Find[并查集 删除]

    UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...

  2. 第十四届华中科技大学程序设计竞赛 C Professional Manager【并查集删除/虚点】

    题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Thus a pro ...

  3. Hdu 2473(并查集删除操作) Junk-Mail Filter

    有木有非常吊 加强 加强版   啊  ,看了都不敢做了   .后来先做了食物链这个我还是看过的.但还是A不掉,没明确神魔意思 .总而言之.大牛的博客是个好东西.我就那么看了一下,还是不懂怎莫办啊,哎, ...

  4. HDU 2473 Junk-Mail Filter 【并查集删除】

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩

    Description Recognizing junk mails is a tough task. The method used here consists of two steps:  1) ...

  6. HDU 2473 Junk-Mail Filter 并查集删除(FZU 2155盟国)

    http://acm.hdu.edu.cn/showproblem.php?pid=2473 http://acm.fzu.edu.cn/problem.php?pid=2155 题目大意: 编号0~ ...

  7. 并查集(不相交集)的Union操作

    在并查集(不相交集)中附加操作\(Deunion\),它实现的功能是取消最后一次\(Union\)的操作. 实现思想 初始化一个空栈,将每一次的\(Union\)操作的两个集合的根和其值\(Push\ ...

  8. 并查集(UVA 1106)

    POINT: 把每个元素看成顶点,则一个简单化合物就是一条无向边,若存在环(即k对组合中有k种元素),则危险,不应该装箱,反之,装箱: 用一个并查集维护连通分量集合,每次得到一种化合物(x, y)时检 ...

  9. UVA11987Almost Union-Find(并查集删除节点)

    题目链接 题意:n个数(即1-n)和m个操作: 1表示把x和y合并,2表示把x移到y集合里面,3表示统计x集合的元素个数 1,3好说,关键是2操作,可以先把2删除掉,删除的操作可以找一个其他的数字来取 ...

随机推荐

  1. Jquery 提示还可以输入的字数,将多余的字数截取掉

    js代码: $(function () { var counter = $("#divform textarea").val().length; //获取文本域的字符串长度 $( ...

  2. php 时间倒计时

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Android -- java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

    使用Bundle 的getParcelableArray 出现了以下错误: Class not found when unmarshallingjava.lang.ClassNotFoundExcep ...

  4. MVC4 WEBAPI(一)使用概述

    所谓概述,也就是总结一些WEB API常用的使用用法.MVC APIWEB是一个轻量级的服务接口,完全符合RestFul框架设计,每个URL代表一种资源,使用方便,没有WCF那么庞大,但是麻雀虽小五脏 ...

  5. python多线程之Condition(条件变量)

    #!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...

  6. java中文乱码解决方法汇总

    public static void main(String[] argv){ try {                 System.out.println(“中文”);//1           ...

  7. JqueryDemoTools-用于整理jQueryDemo 分类: C# 公共资源 2014-12-02 16:50 224人阅读 评论(1) 收藏

    应用背景: 在学习js时,为了熟悉某个功能,或使用某个插件,往往需要写一个Demo来测试:一些好的Demo也可以整理积累下来,方便以后查阅: 写了一个编写jQuery Demo的辅助工具.界面很简单, ...

  8. Centos 上使用Mono+MVC5+WebApi+Sqlite

    鉴于现在网上很多Mono安装Jexus的方法已经过时,你打开百度搜索基本是几个前辈写的文字,很多其实是过去式了.踩的坑多自然使人望而生畏,而方便快捷的方法百度排名却太低,这里就安利下笔者刚成功使用的方 ...

  9. static 类成员变量 和 static const类成员变量

    1.使用static类的优点: (1)避免与其他类的成员或者全局变量冲突 (2)可以封装 (3)阅读性好 2.static 数据成员独立于该类的任意对象而存在 static数据成员的类型可以是该成员所 ...

  10. Linux使用du和df查看磁盘和文件夹占用空间

    df df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力. df -lh 参数 -h 表示使用「Human-readable」输出,也就是使用 GB.MB 等易读的格式. $ ...