思路:每次合并时,都是给一个虚拟的根。

#include<map>
#include<Set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 100010
#define Maxm 2000010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x3f3f3f3f
#define Mod 1000000007
using namespace std;
int Set[Maxn*],cnt,vi[Maxn*],n;
void init()
{
for(int i=;i<Maxn*;i++)
Set[i]=i;
memset(vi,,sizeof(vi));
cnt=n;
}
int Find(int x)
{
if(Set[x]!=x)
Set[x]=Find(Set[x]);
return Set[x];
}
void merg(int a,int b)
{
int x,y;
x=Find(a);
y=Find(b);
if(Set[x]==a&&Set[y]==b){
Set[x]=++cnt,Set[y]=cnt;
return ;
}
if(Set[x]==Set[y])
return ;
if(x<=n)
Set[x]=y;
else
Set[y]=x;
}
int main()
{
int m,i,j,u,v,Case=;
char str[];
while(scanf("%d%d",&n,&m),n||m){
init();
for(i=;i<=m;i++){
scanf("%s",str);
if(str[]=='M'){
scanf("%d%d",&u,&v);
merg(u,v);
}
else{
scanf("%d",&u);
Set[u]=u;
}
}
int ans=;
for(i=;i<n;i++){
u=Find(i);
if(!vi[u]){
ans++;
vi[u]=;
}
}
printf("Case #%d: %d\n",++Case,ans);
}
return ;
}

hdu 2473 并查集的更多相关文章

  1. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  2. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  3. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  4. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  7. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  8. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  9. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

随机推荐

  1. Castle IOC容器构建配置详解(一)

    主要内容 1.配置什么 2.几种配置方式 3.Include 介绍 4.Properties介绍 5.条件状态 一.配置什么 Castle IOC中并不像Spring.net那样贯穿着一个思想就是一切 ...

  2. C# List 中 Find 方法

    实例化一个集合 List<User> userCollection = new List<User>(); userCollection.Add(new User(1, &qu ...

  3. Could not load file or assembly 'MagickNet.dll'

    1  确定项目中bin目录下存在该DLL文件 2   安装 VC++发布组件_缩略图用_x86(1).exe

  4. C# 获取字符串中的数字

    /// <summary> /// 获取字符串中的数字(不包含小数点) /// </summary> /// <param name="str"> ...

  5. c++中智能输出文件

    首先我们要为每一时间步,设置一个文件名: ] = "; itoa(time,timestr,); std::string s; s += timestr; std::string path ...

  6. 【转】解决Fragment already active方法

    http://blog.csdn.net/u011730649/article/details/43227721 今天在项目中使用Fragment的时候出现这样的错误: 01-28 10:53:34. ...

  7. cookie标准话

    php设置cookie setcookie( * * ,'/'); setcookie( * * ,'/'); //清除cookie setcookie("loginname",' ...

  8. careercup-高等难度 18.6

    18.6 设计一个算法,给定10亿个数字,找出最小的100万个数字.假定计算机内存足以容纳全部10亿个数字. 解法: 方法1:排序 按升序排序所有的元素,然后取出前100万个数,时间复杂度为O(nlo ...

  9. Linux文件系统的几个性能测试软件小结

    曾经测试Linux系统下的分布式集群系统的性能,使用了一些测试软件,公司让我给部门同事做一次基础培训,于是翻看以前所写的记录资料挑选了其中几个,所记之处并不完全,只记录使用的功能. 1.Iozone ...

  10. 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 8.全局防护盲点的总结下篇

    0x01 背景 现在的WEB应用对SQL注入的防护基本都是判断GPC是否开启,然后使用addlashes函数对单引号等特殊字符进行转义.但仅仅使用这样的防护是存在很多盲点的,接上篇http://www ...