题解:

代码:

#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的更多相关文章

  1. HDU2473 Junk-Mail Filter 【可删除的并查集】

    HDU2473 Junk-Mail Filter Problem Description Recognizing junk mails is a tough task. The method used ...

  2. 【uva11987】带删除的并查集

    题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...

  3. UVA - 11987 Almost Union-Find(带删除的并查集)

    I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something s ...

  4. [CF1303F] Number of Components - 并查集,时间倒流

    有一个 \(n \times m\) 矩阵,初态下全是 \(0\). 如果两个相邻元素(四连通)相等,我们就说它们是连通的,且这种关系可以传递. 有 \(q\) 次操作,每次指定一个位置 \((x_i ...

  5. UVa 11987 Almost Union-Find(支持删除操作的并查集)

    传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...

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

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

  7. 并查集(删除) UVA 11987 Almost Union-Find

    题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...

  8. [HDOJ2473]Junk-Mail Filter(并查集,删除操作,马甲)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2473 给两个操作:M X Y:将X和Y看成一类. S X:将X单独划归成一类. 最后问的是有多少类. ...

  9. *HDU2473 并查集

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

随机推荐

  1. ping 返回的TTL数值代表什么?

    ping 返回的TTL数值代表什么? 1 [root@standby ~]# dig @202.106.0.20 www.iqiyi.com 2 3 ; <<>> DiG 9. ...

  2. Dojo与jQuery综合比较分析

    最近一直都在参与项目,无法抽空写些或者看些东西,周末抽了点时间看了下关于Dojo和Jquery的东西,在CSDN上看到一篇两个框架进行对比的文章,感觉写的不错,就拿了过来,没有别的意思,一来想保留下来 ...

  3. 7.SpringBoot 之 Web

    添加资源处理 package org.springframework.boot.autoconfigure.web.servlet. public class WebMvcAutoConfigurat ...

  4. Mac下使用Wine安装PowerDesigner15

    下载: (链接: https://pan.baidu.com/s/1bpEYyIV 密码: 5ymj) 安装: 1.安装Wine 参考:http://www.cnblogs.com/EasonJim/ ...

  5. SQL——Hibernate SQL增删改查

    1.查询list数据 实例:user login public String userLogin(){ Session session = HibernateSessionFactory.getSes ...

  6. 第16月第12天 CABasicAnimation 旋转加速

    1. ; double duration = 10.0f; ; i<count; i++) { //旋转动画 CABasicAnimation *anima3 = [CABasicAnimati ...

  7. B - 集合选数 (状压DP)

    题目链接:https://cn.vjudge.net/contest/281960#problem/B 题目大意:中文题目 具体思路: 我们通过构造矩阵, x , 3x,9x,27x 2x,6x,18 ...

  8. A - 地精部落 (DP)

    题目链接:https://cn.vjudge.net/contest/281960#problem/A 题目大意:中文题目. 具体思路:首先,如果有一段是山谷的话,那么这一段中也能用来表示山峰,只要将 ...

  9. 《C#数据结构和算法》-排序

    7.7 各种排序方法的比较与讨论 排序在计算机程序设计中非常重要,上面介绍的各种排序方法各有优缺点, 适用的场合也各不相同.在选择排序方法时应考虑的因素有: ( )待排序记录的数目 n 的大小: ( ...

  10. Linux用户组相关指令

    ⒈增加用户组 ①groupadd 用户组名 ⒉删除用户组 ①groupdel 用户组名 ⒊修改用户所在的用户组 ①usermod -g 用户组 用户名 ★用户和用户组的相关文件 ①/etc/passw ...