并查集+路径压缩(poj1988)
http://poj.org/problem?id=1988
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 19122 | Accepted: 6664 | |
Case Time Limit: 1000MS |
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
题意:可以把题目中的意思抽象为栈集合的合并,题目中有两个操作
M 把包含x的栈放在包含y的栈的上面;
C统计包含x的栈中x下面有多少个元素;
分析:此题是并查集路径压缩的典型题目,与前面的种类并查集还有些不同;题目中用到f[],num[],dis[]三个数组,f数组记录i的父节点,num数组记录以i为根的集合有多少个元素,dis记录i到根节点的距离,那么i下面的元素就是x=finde(i);num[x]-dis[i]-1个元素;
程序:
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#define M 30009
#define eps 1e-10
#define inf 1000000000
#define mod 2333333
using namespace std;
int f[M],num[M],dis[M];
int finde(int x)
{
if(x!=f[x])
{
int t=f[x];
f[x]=finde(f[x]);
dis[x]+=dis[t];
}
return f[x];
}
void make(int a,int b)
{
int x=finde(a);
int y=finde(b);
if(x!=y)
{
f[y]=x;
dis[y]+=num[x];
num[x]+=num[y];
}
}
int main()
{
int n,i,a,b;
char str[3];
while(scanf("%d",&n)!=-1)
{
for(i=1;i<=M+1;i++)
{
f[i]=i;
num[i]=1;
dis[i]=0;
}
while(n--)
{
scanf("%s",str);
if(str[0]=='M')
{
scanf("%d%d",&a,&b);
make(a,b);
}
else
{
scanf("%d",&a);
int x=finde(a);
printf("%d\n",num[x]-dis[a]-1);
}
}
}
}
并查集+路径压缩(poj1988)的更多相关文章
- hdu 1558 线段相交+并查集路径压缩
Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction
http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...
- 并查集 + 路径压缩(经典) UVALive 3027 Corporative Network
Corporative Network Problem's Link Mean: 有n个结点,一开始所有结点都是相互独立的,有两种操作: I u v:把v设为u的父节点,edge(u,v)的距离为ab ...
- HDOJ 3635 并查集- 路径压缩,带秩合并
思路来源:http://blog.csdn.net/niushuai666/article/details/6990421 题目大意: 初始时,有n个龙珠,编号从1到n,分别对应的放在编号从1到n的城 ...
- LA 并查集路径压缩
题目大意:有n个节点,初始时每个节点的父亲节点都不存在.有两种操作 I u v:把点节点u的父亲节点设为v,距离为|u-v|除以1000的余数.输入保证执行指令前u没有父亲节点. E u:询问u到根节 ...
- snnu(1110) 传输网络 (并查集+路径压缩+离线操作 || 线段树)
1110: 传输网络 Time Limit: 3 Sec Memory Limit: 512 MBSubmit: 43 Solved: 18[Submit][Status][Web Board] ...
- - > 并查集+路径压缩(详解)(第一节)
先举一个友爱的例子解释一下并查集: 话说江湖上散落着各式各样的大侠,有上千个之多. 他们没有什么正当职业,整天背着剑在外面走来走去,碰到和自己不是一路人的,就免不了要打一架.但大侠们有一个优点就是讲义 ...
- PAT甲级1013题解——并查集+路径压缩
题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...
- HDU 3635 并查集+路径压缩+记录每个点移动次数
题意: 给定n个点 oper个操作 每个点有1个龙珠 下面2种操作: T u v 把u点所有龙珠搬到v Q u 问u点当前所在城市 u点所在城市有几个龙珠 u点被移动几次 思路: 并查集可以求出 u ...
随机推荐
- ASP.NET MVC 使用 Datatables (2)
在服务器端实现分页,排序,获取当前页面数据 在上篇的基础上进行改造(datatables的客户端实现) 1.修改View页面代码如下: <div class="row"> ...
- C# json字符串格式
[{\"MenId\":\"1\",\"MenName\":\"内容管理\",\"ParentId\" ...
- 不可错过的10个超棒jQuery表单操作代码片段
jQuery 绝对是一个伟大的开源javascript类库,是帮助我们快速和高效开发前端应用的利器.可能大家在日常的开发过程中常常会处理表单相关的 javascript,在今天这篇代码片段分享文章中, ...
- Stay hungry, stay foolish. 求知若饥,虚心若愚。
如果留意我博客的朋友,应该都有看到这句话在我的自我介绍栏目存在了好长一段时间了,另外,我的 QQ.旺旺.MSN等都有这个签名.Stay hungry, stay foolish.(求知若饥,虚心若愚) ...
- Ubuntu下启动/重启/停止apache服务器
Task: Start Apache 2 Server /启动apache服务# /etc/init.d/apache2 startor$ sudo /etc/init.d/apache2 start ...
- error:2014 Commands out of sync; you can't run this command now
如下错误: 分析原因: 前端ajax请求后台,共用同一个链接. 搜索别人的解决方案:http://blog.csdn.net/grass_ring/article/details/3499402 用m ...
- 浏览器Chrome对WebGL支持判断
1.开启方式: 第一种:打开cmd,切换到Chorme的安装目录,敲入chrome.exe --enable -webgl,回车就会打开一个chrome浏览器窗口: 第二种:找到Chrome浏览器的快 ...
- 【RF库测试】Variable Should not Exist & variable should exist
Variable Should not Exist variable should exist
- 未配置jdk环境变量,cmd环境能运行java -version命令
我的情况是C:\Windows\System32路径下有java.exe.javaw.exe.javaws.exe三个文件,将三个文件删除后配置自己的jdk环境变量 可能原因参考帖子:https:// ...
- 开始iOS 7中自动布局教程(一)
本文转载至 http://www.cocoachina.com/industry/20131203/7462.html 到目前为止,如果你的设计相当的复杂,那么你必须编写大量的代码来适应这样的布局.你 ...