POJ1988 并查集的使用
Cube Stacking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 21157 | Accepted: 7395 | |
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
#include<stdio.h>
#define N 30001 int count[N], num[N], pre[N]; void inite()
{
for(int i = ; i < N; i++)
{
count[i] = ;
num[i] = ;
pre[i] = i;
}
} int find(int x)
{
if(pre[x] == x)
return x; int t = find(pre[x]);
count[x] += count[pre[x]];
pre[x] = t;
return t; }
void Union(int x, int y)
{
int i = find(x);
int j = find(y);
if(i == j)
{
return;
}
count[i] = num[j];
num[j] += num[i];
pre[i] = j;
} int main()
{
int i, x, y, n;
char s[];
scanf("%d",&n);
inite();
for(i = ; i < n; i++)
{
scanf("%s",s);
if(s[] == 'M')
{
scanf("%d%d",&x,&y);
Union(x,y);
}
else if(s[] == 'C')
{
scanf("%d",&x);
int c = find(x);
printf("%d\n",count[x]);
}
}
return ;
}
POJ1988 并查集的使用的更多相关文章
- poj1988(并查集)
题目链接:http://poj.org/problem?id=1988 题意:有n个箱子,初始时每个箱子单独为一列: 接下来有p行输入,M, x, y 或者 C, x: 对于M,x,y:表示将x箱子所 ...
- poj1988 简单并查集
B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:30000KB 64bit ...
- POJ1988 Cube Stacking 【并查集】
题目链接:http://poj.org/problem?id=1988 这题是教练在ACM算法课上讲的一道题,当时有地方没想明白,现在彻底弄懂了. 题目大意:n代表有n个石头,M a, b代表将a石头 ...
- poj1988 Cube Stacking 带权并查集
题目链接:http://poj.org/problem?id=1988 题意:有n个方块,编号为1-n,现在存在两种操作: M i j 将编号为i的方块所在的那一堆方块移到编号为j的方块所在的那 ...
- bzoj3376/poj1988[Usaco2004 Open]Cube Stacking 方块游戏 — 带权并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3376 题目大意: 编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方 ...
- 并查集+路径压缩(poj1988)
http://poj.org/problem?id=1988 Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submiss ...
- 并查集——poj1988(带权并查集中等)
一.题目回顾 题目链接:Cube Stacking 题意:有n个箱子,初始时每个箱子单独为一列:接下来有p行输入,M, x, y 或者 C, x: 对于M,x,y:表示将x箱子所在的一列箱子搬到y所在 ...
- POJ1988(Cube Stacking)--并查集
题目链接:http://poj.org/problem?id=1988 题意:有n个元素,开始每个元素各自在一个栈中,有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 第二种操作是询问 ...
- poj1988 Cube Stacking(并查集
题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[ ...
随机推荐
- Axure设计分析作业-实例解析
本文转载自人人都谁产品经理,作者完全使用Axure做了这一个产品需求文档.文档地址:http://1passwordmanager.sinaapp.com/ 大家可以先睹为快.这个PRD完全使用axu ...
- Centos中安装code blocks
CentOS下面安装Codeblocks不像Ubuntu下面那样轻松,可以直接在软件中心安装.这里好多信赖我们要自己安装,也不是很麻烦. 1.先安装gcc和gcc++,这个可以直接安装 # yum i ...
- 用特殊字体来实现矢量ICON
用特殊字体来实现矢量ICON tips:其实每个icon都是一个unicode字符,所以,可以通过改变font-size实现icon的矢量放大:可以通过改变color实现多色系.
- thinkjs与Fine Uploader的邂逅
最近在做一个内部系统,需要一个无刷新的上传功能,找了许久,发现了一个好用的上传工具-Fine Uploader,网上也有不少关于它的介绍,对我有不少的启发,结合我的使用场景简单的介绍一下它与t ...
- 第12讲- Android之消息提示Toast
第12讲 Android之消息提示Toast .Toast Toast 是一个 View 视图,在应用程序上浮动显示少量的信息给用户,它永远不会获得焦点,不影响用户的输入等操作,主要用于向用户显示一些 ...
- 0,null,empty,空,false,isset
<?php header("Content-type: text/html; charset=utf-8"); $a=0; //1. if($a==0) { echo $a; ...
- NavMeshAgent 动态加载障碍物
如果你想让游戏人物绕开一些物体, 这些物体动态生成出来的.只需要给物体添加NavMeshObstacle组件即可 1. 绿色方块添加NavMeshObstacle组件 2. 红色方块没有添加NavMe ...
- OpenStack中给wsgi程序写单元測试的方法
在 OpenStack 中, 针对web应用, 有三种方法来写单元測试 1) 使用webob生成模拟的request from __future__ import print_function imp ...
- Window.open 实现导航与打开窗口,导航到一个特定链接地址,也可以打开一个新的浏览器窗体
语法 window.open(strUrl,strWindowName,strWindowFeatures ,replace) strUrl: 打开资源的地址 strWindowName: 表示窗体名 ...
- javascript 模仿回车键事件
<script> $(function(){ var _login = function (){ var _name = $('#name'); var _password = $('#p ...