POJ——T 1988 Cube Stacking
http://poj.org/problem?id=1988
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 25865 | Accepted: 9044 | |
| 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
Source
#include <algorithm>
#include <cstdio> using namespace std; const int N();
int fa[N],sum[N],beh[N]; int find(int x)
{
if(fa[x]==x) return x;
int dad=find(fa[x]);
beh[x]+=beh[fa[x]];
return fa[x]=dad;
}
inline void combine(int x,int y)
{
x=find(x),y=find(y);
if(x==y) return ;
beh[x]+=sum[y];
sum[y]+=sum[x];
fa[x]=y;
} int main()
{
for(int i=;i<N;i++)
fa[i]=i,sum[i]=;
int p,u,v; scanf("%d",&p);
for(char ch;p--;)
{
scanf("\n%c%d",&ch,&u);
if(ch=='M')
{
scanf("%d",&v);
combine(u,v);
}
else find(u),printf("%d\n",beh[u]);
}
return ;
}
POJ——T 1988 Cube Stacking的更多相关文章
- POJ 1988 Cube Stacking(并查集+路径压缩)
题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的 ...
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- poj.1988.Cube Stacking(并查集)
Cube Stacking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Submi ...
- POJ 1988 Cube Stacking(带权并查集)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 23678 Accepted: 8299 Ca ...
- HDU 1988 Cube Stacking (数据结构-并检查集合)
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 18834 Accepted: 6535 Ca ...
- [POJ 1988] Cube Stacking (带值的并查集)
题目链接:http://poj.org/problem?id=1988 题目大意:给你N个方块,编号从1到N,有两种操作,第一种是M(x,y),意思是将x所在的堆放到y所在的堆上面. 第二种是C(x) ...
- POJ 1988 Cube Stacking (种类并查集)
题目地址:POJ 1988 这道题的查找合并的方法都能想的到,就是一点没想到,我一直天真的以为查询的时候,输入后能立即输出,这种话在合并的时候就要所有的结点值都要算出来,可是经过路径压缩之后,没办法所 ...
- POJ 1988 Cube Stacking 【带权并查集】
<题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...
- POJ 1988 Cube stacking【并查集高级应用+妙用deep数组】
Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes ...
随机推荐
- 使用js获取url中的get参数并转成json格式
写在前面的 没啥说的 上代码 思路就是先获取到?后面的参数区,然后 利用字符串转数组方法获取到各个参数 var json = {}; var url = 'https://www.baidu.com/ ...
- c/s结构的自动化——pyautogui
环境:Python 3.5.3 pip install pyautogui -i http://pypi.douban.com/simple --trusted-host pypi.douban.co ...
- DedeCMS搜索结果页面调用自定义字段的方法
有时候在我们需要在dedecms的搜索结果页面调用自定义字段,尤其是在做下载站的时候,需要在搜索结果页调用软件大小以及软件等级等等,但是我们发现在搜索结果页模板中使用“[field:字段名]”标签无法 ...
- (一)React再学习
新公司的技术栈是React,虽然之前对react大概过了一遍,但是自己没有实际落地过项目 再学习一遍react: 一.react全家桶 ·create-react-app ·组件化思维 ·JSX ·开 ...
- Vue 基础篇
Vue 基础篇 一.框架与库的区别 JQ库->DOM(DOM操作) + Ajax请求 art-template库->模板引擎 框架 -> 全方位.功能齐全 简易的DOM体验 + 发请 ...
- pandas 8 画图
from __future__ import print_function import pandas as pd import numpy as np import matplotlib.pyplo ...
- vs解决方案里复制一个项目
首先,保证要复制的项目的整洁无垃圾文件: 然后,选“文件”/“导出模板”,起个名字: 再者,创建一个同类型的项目,这时项目模板里就会出现你刚才导出的项目了.
- hihocoder 1124 : 好矩阵 dp
好矩阵 时间限制:3000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定n, m.一个n × m矩阵是好矩阵当且仅当它的每一个位置都是非负整数,且每行每列的和 ≤ 2.求好矩阵的个 ...
- [Test] Easy automated testing in NodeJS with TestCafe
Quickly get up and running with sensible automated testing scenarios written in ES6. Installing and ...
- Struts2中Struts.xml的作用
struts.xml 为Struts 2的核心配置文件.struts.xml文件主要负责管理应用中的Action映射,以及该Action包含的Result定义等.struts.xml中主要配置Stru ...