支持删除的并查集 hdu2473
题解:
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=;
int fa[maxn],id,vis[maxn];
int find(int x)
{
if (x!=fa[x]) fa[x]=find(fa[x]); return(fa[x]);
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y);
if (fx!=fy) fa[fx]=fy;
}
void del(int x)
{
fa[x]=++id;
}
int main()
{
freopen("noip.in","r",stdin);
freopen("noip.out","w",stdout);
int n,m,Case=;
while (scanf("%d%d",&n,&m),n+m)
{
for (int i=;i<=n;i++) fa[i]=i+n;
for (int i=n+;i<=n+n+m;i++) fa[i]=i;
id=n+n;
int a,b;char ch[];
for (int i=;i<=m;i++)
{
cin>>ch;
if (ch[]=='M')
{
cin>>a>>b;
merge(a,b);
}
else
{
cin>>a;
del(a);
}
}
int ans=;
memset(vis,,sizeof(vis));
for (int i=;i<=n;i++)
{
int x=find(i);
if (!vis[x]) ans++,vis[x]=;
}
printf("Case #%d: %d\n",++Case,ans);
}
return ;
}
支持删除的并查集 hdu2473的更多相关文章
- HDU2473 Junk-Mail Filter 【可删除的并查集】
HDU2473 Junk-Mail Filter Problem Description Recognizing junk mails is a tough task. The method used ...
- 【uva11987】带删除的并查集
题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...
- UVA - 11987 Almost Union-Find(带删除的并查集)
I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something s ...
- [CF1303F] Number of Components - 并查集,时间倒流
有一个 \(n \times m\) 矩阵,初态下全是 \(0\). 如果两个相邻元素(四连通)相等,我们就说它们是连通的,且这种关系可以传递. 有 \(q\) 次操作,每次指定一个位置 \((x_i ...
- UVa 11987 Almost Union-Find(支持删除操作的并查集)
传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...
- hdu2473 Junk-Mail Filter 并查集+删除节点+路径压缩
Description Recognizing junk mails is a tough task. The method used here consists of two steps: 1) ...
- 并查集(删除) UVA 11987 Almost Union-Find
题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...
- [HDOJ2473]Junk-Mail Filter(并查集,删除操作,马甲)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2473 给两个操作:M X Y:将X和Y看成一类. S X:将X单独划归成一类. 最后问的是有多少类. ...
- *HDU2473 并查集
Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- Javaweb学习笔记——(四)——————JavaScript基础&DOM目录
1.案例一:在末尾添加节点 第一个:获取到ul标签 第二部:创建li标签 document.createElement("标签名称")方法 第三步:创建文本 document.cr ...
- Linux 重启网卡失败 Job for network.service failed because the control process exited with error code. See "systemctl status network.service" and "journalctl -xe" for details.
linux下重启网卡使用命令 : service network restart 时报错: [root@slave01 hadoop]# service network restart Startin ...
- PhoneUtil
package cn.fraudmetrix.octopus.horai.biz.utils; import org.springframework.util.StringUtils; import ...
- swift 计算100000以内的 回文数
... { var rep = var aa = a repeat{ rep = rep * + aa % aa = aa / }) if(rep == a) { print("\(a)是回 ...
- SpringBoot整合MyBatis(XML)
(1).添加依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...
- UML和模式应用4:初始阶段(6)--迭代方法中如何使用用例
1.前言 用例是UP和其他众多迭代方法的核心.UP提倡用例驱动开发. 2. 迭代方法中如何使用用例 功能需求首先定义在用例中 用例是迭代计划的重要部分,迭代是通过选择一些用例场景或整个用例来定义的 用 ...
- Linux 文件系统扩展属性【转】
转自:https://blog.csdn.net/ganggexiongqi/article/details/7661024 扩展属性(xattrs)提供了一个机制用来将<键/值>对永久地 ...
- FarBox的建站过程
FarBox的建站过程 本文转自:http://mosir.org/html/y2012/How-to-build-your-website-by-farbox.html 作者: mosir 时间: ...
- OA协同办公软件
OA协同办公软件: 泛微软件. 九思软件. 华天动力. 万户OA.:北京万户网络技术有限公司创立于1998年2月,是北京华宇软件股份有限公司(股票简称:“华宇软件”,股票代码:300271)的全资子公 ...
- OLAP和OLTP的区别(基础知识) 【转】
联机分析处理 (OLAP) 的概念最早是由关系数据库之父E.F.Codd于1993年提出的,他同时提出了关于OLAP的12条准则.OLAP的提出引起了很大的反响,OLAP作为一类产品同联机事务处理 ( ...