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中元素,那么就会爆炸,输入的顺序是装车的顺序,对于每 ...
随机推荐
- em px 简单换算
大部分的网页设计者在CSS代码编写中总是先对整体定义字体尺寸,中文情况下一般为12px,而其实这样以来在通过IE顶部菜单中的“察看-文字大小”设置已无任何 作用.对字体感觉太小的浏览者而言无疑是种很不 ...
- Destoon后台修改公司会员资料信息的必填项限制修改
/template/default/member/edit.htm module\member\admin\template\member_edit.tpl.php module\member\adm ...
- 【结构型】Composite模式
组合模式意在将对象组合成树形结构以表示部分与整体的层次结构关系,并且用户对单个对象的操作以有对组合对象的操作都是一致的.即:组合对象 is-a 单个对象,同时又可以组合着 n 个的单个对象(甚至于其他 ...
- 深入浅出Java 重定向和请求转发的区别
深入浅出Java 重定向和请求转发的区别 <span style="font-family:FangSong_GB2312;font-size:18px;">impor ...
- 用Set中元素做条件查询
一个老师教许多学生,一个学生被许多老师教,一个学生有好多书,同一种书被许多同学拥有.查询教拥有书"a"的学生的老师: class teacher{ String id; ...
- jdk8永久代从方法区移除的验证
/*** 测试使用jdk8中是否仍然可以使用永久代* jvm options * -Xms20m -Xmx20m -Xmn10m -XX:PermSize=10m -XX:MaxPermSize=10 ...
- Android中的SQLiteOpenHelper类
SQLiteOpenHelper是Android提供的一个管理数据库的工具类,可用于管理数据库的创建和版本更新.一般的用法是创建SQLiteOpenHelper的子类,并扩张它的onCreate(SQ ...
- 优秀的VeriSign和混蛋的GlobalSign
由于领导不懂行,直接购买了GlobalSign的证书,结果引起了我这个开发人员痛苦的2星期之旅,说说大体情况: 目的:对买来的一个驱动程序进行签名,使之能够在Win x64情况下安装和使用 下载Win ...
- SpringMVC、Zookeeper、Dubbo使用
联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使前 ...
- 【转】三星8552 手机提示升级系统 完成后重启 开机画面一直停留在三星的LOGO 一闪一闪 怎么办
原文网址:http://ask.zol.com.cn/q/309501.html 楼主你好,手机出现这种情况的话,可以先将电池取下来重新安装,如果不能的话,可以在关机状态下按住电源键+音量下键之后进入 ...