poj1988 简单并查集
Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%lld & %llu
Description
Input
Output
Sample Input
Sample Output
Hint
Description
moves and counts.
* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.
* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.
Write a program that can verify the results of the game.
Input
* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a 'M' for a move operation or a 'C' for a count operation. For move operations, the line also contains two integers: X and Y.For count operations, the line also contains a single integer: X.
Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.
Output
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
Sample Output
1
0
2
解析见代码:
/*
poj1988 ,并查集
题目大意:初始时有N块木块,编号1-N,分开放置,
然后对这些木块开始执行P条操作,M a,b是将a所在的
木块堆移到b所在的木块堆上,C a是查询编号为a的木块在
其之下有多少个木块
思路分析:如果查询的是a号木块所在的木块堆一共有多
少堆,那么这道题就是简单的维护下集合内元素数量就可以,但是现在
问的是它下面有多少块,所以还需要维护的就是它下面的木块数目
初始化under[i]=0,然后对于under数组的更新有两个地方,对于
一个子集的根节点(最下面的木块)在merge时就可以更新了,其他
的木块则是要在压缩路径的时候进行更新,under[i]=under[father[i]]
注意一定要先更新后 压缩路径,要不然就没有更新的效果
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
const int maxn=+;
int father[maxn];
int sum[maxn];
int under[maxn];
int findroot(int x)
{
if(x==father[x]) return x;
int t=findroot(father[x]);
under[x]+=under[father[x]];//先更新under数组,后压缩路径
father[x]=t;
return father[x];
}
void merge(int a,int b)//根是在下面的木块
{
int pa=findroot(a);
int pb=findroot(b);
if(pa==pb) return;
father[pb]=pa;
under[pb]=sum[pa];
sum[pa]+=sum[pb];
}
int main()
{
for(int i=;i<=maxn;i++)
{
father[i]=i;
sum[i]=;
under[i]=;
}
int p;
scanf("%d",&p);
char s[];
int a,b;
while(p--)
{
scanf("%s",s);
if(s[]=='M')
{
scanf("%d%d",&a,&b);
merge(b,a);
}
else
{
scanf("%d",&a);
findroot(a);//祖先节点可能发生了 变化 ,因此需要进行更新
printf("%d\n",under[a]);
}
}
}
poj1988 简单并查集的更多相关文章
- POJ 2524 (简单并查集) Ubiquitous Religions
题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...
- poj1611 简单并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 32781 Accepted: 15902 De ...
- 1213 How Many Tables(简单并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- ACM_“打老虎”的背后(简单并查集)
“打老虎”的背后 Time Limit: 2000/1000ms (Java/Others) Problem Description: “习大大”自担任国家主席以来大力反腐倡廉,各地打击贪腐力度也逐步 ...
- UVA - 1197 (简单并查集计数)
Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...
- poj 2524 Ubiquitous Religions 一简单并查集
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 22389 Accepted ...
- ACM_城市交通线(简单并查集)
城市交通线 Time Limit: 2000/1000ms (Java/Others) Problem Description: A国有n座城市,编号为1~n,这n个城市之间没有任何交通线路,所以不同 ...
- LA3644简单并查集判环
题意: 有n个化合物,每个化合物是两种元素组成,现在要装车,但是一旦车上的化合物中的某几个化合物组成这样一组关系,有n个化合物正好用了n中元素,那么就会爆炸,输入的顺序是装车的顺序,对于每 ...
随机推荐
- 一个php小白找工作的历程
一个php小白找工作的历程其实对新工作还是有点忐忑的,对于我这样一个有着特殊工作经历的来说更是如此.为了更好的迎接未来,不得不总结下过去.在经历一段时间的职业生涯探索期后,还是觉得自己更适合做程序员这 ...
- Windows坐标系
.逻辑坐标 逻辑坐标是独立于设备的,它与设备点的大小无关.使用逻辑单位,是实现"所见即所得"的基础.当程序员在调用一个画线的GDI函数LineTo,画出25.4mm(1英寸) 长的 ...
- SwitchySharp怎样设置 ( proxy switch!的设置与使用方法)
规则列表URL https://autoproxy-G{过}F{滤}Wlist.googlecode.com/svn/trunk/G{过}F{滤}Wlist.txt 注:不同的代{过}{滤}理 相 ...
- 转:SRIO错误侦测和管理机制
http://www.deyisupport.com/question_answer/dsp_arm/c6000_multicore/f/53/t/22879.aspx http://www.deyi ...
- Gridview中修改某列的背景色
Gridview中状态列的值是1,某列的背景是是绿色状态字段是:archivesStatus protected void gvInfo_RowDataBound(object sender, Gri ...
- mono环境变量
mono环境变量 2013-05-11 01:14:33| 分类: mono|举报|字号 订阅 下载LOFTER我的照片书 | Name mono - Mono's ECMA-CL ...
- 排序功能实现 jQuery实现排序 上移 下移
效果 思路, 跟相邻元素,互换sort. 前提是每一个元素都有自己的sort值,不为零. <tr id="{sh:$vo.id}"> <td> <sp ...
- COJ 0560 4015划分数
4015 划分数 难度级别:B: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 有n个无区别的物品,将他们划分成不超过m组,求出划分方法数模 ...
- 【转】如何判断CPU是大端还是小端模式
原文网址:http://blog.csdn.net/ysdaniel/article/details/6617458 如何判断CPU是大端还是小端模式 http://blog.sina.com.cn/ ...
- DP #1 Singleton Pattern线程安全问题
单例模式确保一个类只有一个实例,自行提供这个实例并向整个系统提供这个实例. 其中涉及到最主要的问题就是在多线程并发时线程安全问题. 单例模式的实现也有一个循序渐进的过程:1.最基本要求:每次从getI ...