hdu 2473 并查集
思路:每次合并时,都是给一个虚拟的根。
#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 并查集的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
随机推荐
- 有关ListBox
如何拿到Source:从SQL,从XML file SQL:一个是ObjectDataProvider //用linq方法拿到SQL data,wrap到一个IEnumerable<Custom ...
- Server-U与IIS端口占用问题解决
在新版的ftp软件Server-U(11.X)中,由于其默认设置中监听了80端口,经常会导致IIS不能服务不能正常启用,下面记录如何修改server-u的80端口. 打开Server-U软件, ...
- 如何选择PDA的操作系统? Android OR WINCE?
PDA(Personal Digital Assistant),又称为掌上电脑,可以帮助我们完成在移动中工作,学习,娱乐等.按使用来分类,分为工业级PDA和消费品PDA.当我们需要购买PDA的时候, ...
- 用一个I/O口控制1个三色指示灯, 2个单色指示灯
http://www.baiheee.com/Documents/081207/081207184434.htm http://www.baiheee.com/Documents/081207/081 ...
- ST10 Bootstrap Loader
Bootstrap Loader The built-in bootstrap loader (BSL) of the ST10F269 provides a mechanism to load th ...
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1
3.Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1 http://blog.csdn.net/sunbow0 ...
- cocos2dx动画Animation介绍
一.帧动画 你能够通过一系列图片文件,像例如以下这样,创建一个动画: [cpp] CCAnimation *animation = CCAnimation::create(); //从本 ...
- oc-24-点语法
/** 点语法的本质是方法的调用,而不是访问成员变量,当使用点语法时, 编译器会自动展开成相应的方法.切记点语法的本质是转换成相应的set和get方法, 如果没有set和get方法,则不能使用点语法. ...
- 自学Linux命令的四种方法
自学Linux命令的四种方法 导读 童鞋们刚接触linux时,在学习过程中中会遇到不少问题,学习linux摸不着头脑,那么下面介绍四种linux的学习方法,特别适合新手. 方法一:终端"每日 ...
- windows下搭建svn服务端、客户端
1.安装SVN服务器subversion以及客户端TortoiseSVN,在网上下载windows版的subversion,TortoiseSVN并安装,比如我的服务端安装在了D:\Program F ...