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连续签到
require "./global.php"; $act = isset($_GET['act']) ? $_GET['act'] : "error"; // ...
- NET MVC+EF6+Bootstrap
开源:ASP.NET MVC+EF6+Bootstrap开发框架 前言 我在博客园潜水两三年了,在这里看过很多大神的文章,也学到了很多东西.可以说我是汲取着博客园的营养成长的. 想当年,我也是拿1 ...
- Linux_service cloudera-scm-server start failed
see log : /var/log/cloudera-scm-server/cloudera-scm-server.log
- 無心插柳的Linux學習者代言人——蔡德明
誰是「蔡德明」恐怕沒有多少人知道,不過提到「鳥哥」這個稱號,在臺灣的Linux社群幾乎是無人不知無人不曉,蔡德明正是鳥哥的本名.鳥哥究竟多有名? 如果你是有意學習Linux的初學者,卻不知如何下手,1 ...
- 编程的宗派(OOP与FP孰优孰劣)--王垠
编程的宗派 总是有人喜欢争论这类问题,到底是"函数式编程"(FP)好,还是"面向对象编程"(OOP)好.既然出了两个帮派,就有人积极地做它们的帮众,互相唾骂和鄙 ...
- jQueryMobile之Popup
效果: (1):Tooltip (2):Menu (3):NestedMenu (4):Login ------ 源码: <!DOCTYPE html> <html lang=&qu ...
- 认识Android
安卓的特点开放性平等性无界性方便性硬件的丰富性 Android操作系统之中,一共将体系结构划分为四层:应用层(Application).应用框架层(Application Framework).系统运 ...
- C51 的重入问题 WARNING L15: MULTIPLE CALL TO SEGMENT
WARNING L15: MULTIPLE CALL TO SEGMENT 这个问题必须注意,可能引起程序冲突,假设你用于自动化领域,则可能导致信号产生尖峰. 产生这一警告的一个根源是:你在主循环里调 ...
- /dev/urandom
/dev/urandom则是一个非阻塞的发生器: dev/random的一个副本是/dev/urandom (”unlocked”,非阻塞的随机数发生器),它会重复使用熵池中的数据以产生伪随机数据.这 ...
- 五大P2P平台费用一览
我看版上谈P2P的挺多的,正好我也投了点P2P, 今天看到一个不错的图 欢迎版上朋友讨论 -- 不忧不惧