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]放到[ ...
随机推荐
- 解决IE6 IE7 JSON.stringify JSON 未定义问题
在项目中引入json2.js 官方http://www.json.org/ 源码地址:https://github.com/douglascrockford/JSON-js $.ajax({ url: ...
- 【HDU1232】畅通工程(并查集基础题)
裸敲并查集,很水一次AC #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- QString转换为char*
QString在Qt里相当于C++里的std::string,或者是C里的c style string.不过,QString跟编码相关,在低层想把一个QString发送出去相当麻烦,尤其对方用的不是Q ...
- 使用STS 创建spring配置文件
1.创建一个bean文件 2.输入文件名applicationContext.xml 3.这里会自动显示模板文件 4.创建后,自动填充头不定义 到这里就可以发现,我们创建spring文件时,需要的配置 ...
- 自定义 Layer 属性的动画
默认情况下,CALayer 及其子类的绝大部分标准属性都可以执行动画,无论是添加一个 CAAnimation 到 Layer(显式动画),亦或是为属性指定一个动作然后修改它(隐式动画). 但有时候 ...
- Cordova for android怎样在App中处理退出button事件
项目须要在HTML5 Android App中增加对返回键的处理,发现直接在Activity中加返回键处理代码不起作用,分析cordova源代码发现返回键已经被WebView处理掉了,所以仅仅能在js ...
- 大到可以小说的Y组合子(三)
答:关于Fix的问题你fix了吗? 问:慢着,让我想想,上次留下个什么问题来着?是说我们有了一个求不动点的函数Fix,但Fix却是显式递归的,是吧? 答:有劳你还记的这个问题. 问:Fix的参与背离了 ...
- Qt相关问题
1. Qt编译中的error: cannot find -lGL和 error: collect2: error: ld returned 1 exit status 一般见于新安装的系统,马上就直 ...
- 解决jquery和其他库的冲突
一.jquery在其他库之后导入 (1)jQuery.noConflict(); //将变量的$控制权转交给其他库. jQuery(function(){ ...
- 前端--关于客户端javascript
浏览器中的Javascript 客户端javascript就是运行在浏览器中的javascript,现代的浏览器已经有了很好的发展,虽然它是一个应用程序,但完全可以把它看作是一个简易的操作系统,因为像 ...