题解:

代码:

#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. Neural Networks and Deep Learning(week4)Deep Neural Network - Application(图像分类)

    Deep Neural Network for Image Classification: Application 预先实现的代码,保存在本地 dnn_app_utils_v3.py import n ...

  2. spring集成ActiveMQ居然要依赖这么多包

    做spring和ActiveMQ的集成,作maven依赖的时候有感(以前都不在乎,现在不一样了........省略) <!-- https://mvnrepository.com/artifac ...

  3. mysql alter 用法,修改表,字段等信息

    一: 修改表信息 1.修改表名 alter table test_a rename to sys_app; 2.修改表注释 alter table sys_application comment '系 ...

  4. C++ 输入输出八进制、十进制、十六进制

    默认进制 cin or cout在默认情况下按照十进制输入输出 八进制 要使输入为八进制数,首先切换至八进制输入 cin >> oct; //接下里就可以输入八进制数了 cin >& ...

  5. VUE2.0 饿了吗视频学习笔记(二):新版本添加路由和显示Header

    https://gitee.com/1981633/vue_study.git 源码下载地址,随笔记动态更新中 webpack.dev.conf.js中添加两段代码 'use strict' cons ...

  6. The provider is not compatible with the version of Oracle client

    保留下安装文件夹里的所有文件.然后把我那个很小应用软件(需要访问远程的oracle数据库)放在这个文件夹里.删除一下直接感觉无用的文件,先抽取可 能用不着的文件,保留放在另一个备用的文件夹里,然后运行 ...

  7. Listbox的操作,数据源变化时要及时更新listbox要先把数据源置空,在给数据源绑定数据

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 列表视图QlistView

    列表视图QlistView要配合setModel模型一起使用 例子一 QStringListModel()  字符串列表模型 import sys from PyQt5.QtWidgets impor ...

  9. luogu P3707 [SDOI2017]相关分析

    传送门 对于题目要求的东西,考虑拆开懒得拆了 ,可以发现有\(\sum x\sum y\sum x^2\sum xy\)四个变量影响最终结果,考虑维护这些值 下面记\(l,r\)为区间两端点 首先是区 ...

  10. Python 优雅获取本机 IP 方法【转】

    转自:https://www.cnblogs.com/lfxiao/p/9672975.html 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 ...