支持删除的并查集 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 ...
随机推荐
- Forth 语言概要 - Forth: An underview
Forth 语言概要 - Forth: An underview 原作者 Peter Knaggs 原文标题< Forth: An underview > 译者: 未知 译文链接: htt ...
- python 的正则表达式指北
正则表达式用来拆分字符串 >>> s = 'one1two2three3four4' >>> pattern = re.compile(r'\d+') >&g ...
- JMS学习(八)-ActiveMQ Consumer 使用 push 还是 pull 获取消息
ActiveMQ是一个消息中间件,对于消费者而言有两种方式从消息中间件获取消息: ①Push方式:由消息中间件主动地将消息推送给消费者:②Pull方式:由消费者主动向消息中间件拉取消息.看一段官网对P ...
- Mac下使用Wine安装PowerDesigner15
下载: (链接: https://pan.baidu.com/s/1bpEYyIV 密码: 5ymj) 安装: 1.安装Wine 参考:http://www.cnblogs.com/EasonJim/ ...
- CRLF攻击的一篇科普:新浪某站CRLF Injection导致的安全问题(转)
转:https://www.leavesongs.com/PENETRATION/Sina-CRLF-Injection.html 新浪某站CRLF Injection导致的安全问题 PHITHON ...
- ping不同网站总结
用高级设置法预防Ping 用网络防火墙阻隔Ping 启用IP安全策略防Ping IP安全机制(IP Security)即IPSec策略,用来配置IPSec安全服务.这些策略可为多数现有网络中地多数通信 ...
- Nginx GZIP 压缩
[ HTTP 开启gzip ] gzip on; // 开启 nginx在线实时压缩数据流: gzip_min_length 1k; // 允许压缩的页面最小字节 gzip_buffers 32k; ...
- Linux - openssl 加密
openssl rand 15 -base64 # 口令生成 openssl sha1 filename # 哈希算法校验文件 openssl md5 filename # MD5校验文件 opens ...
- 永久修改MySQL字符集(适用Mysql5.5、Mysql5.6、Mysql5.7以上)
在my.ini文件里添加三个参数: 1.在[client]下添加 default-character-set=utf8 2.在[mysqld]下添加 character-set-server=utf8 ...
- ServiceMesh了解一下
http://www.servicemesh.cn/?/article/70 https://zhuanlan.zhihu.com/p/33196550