[机房练习赛4.3][poj1988]bricks
来源:互联网 发布:python与weka的区别 编辑:程序博客网 时间:2024/05/22 03:20
Problem 1. bricks
Input file: bricks.in
Output file: bricks.out
Time limit: 1 second
jyb 在BUAA 天天被大神虐,所以只能去搬砖了。
终于在2019 年的夏天,菜菜的jyb 找不到工作,真的去工地搬砖了。jyb 的工头cky 是一个很麻烦的人,他
会让jyb 按某种方式搬砖,还问会问一些奇怪的问题。
现在有n 块砖,m 次操作。操作有两种:
1. M x y 把编号为x 的砖所在的一摞砖搬到编号为y 的砖所在的一摞砖的上面。如果x 和y 在同一摞砖则
忽略这个操作。(最初,每块砖都是单独一摞)
2. C x 询问x 下面压着多少块砖。
jyb 搬砖实在是太累了,想请你帮忙回答一下cky 工头的询问。
Input
第1 行,2 个整数n;m,表示一共有多少块砖以及有多少操作。
接下来m 行,每行一个操作,操作表示形式与前文一致。
Output
对于每次询问操作,输出答案
Sample
bricks.in
6 6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
bricks.out
1
0
2
Note
• 对于30% 的数据,1 n 103,1 m 103;
• 对于60% 的数据,1 n 104,1 m 105;
• 对于100% 的数据,1 n 2 105,1 m 3 105。
Bricks:并查集(by jyb)
用链表什么的暴力枚举题意,最坏复杂度是O(m^2)的。
注意到每次都是移动一摞砖,其实问题就是集合的合并,只是这个集合是一个有序的,我们还需要知道每个元素在集合中的位置。我们可以利用并查集高效的来解决。我们引入一个dep数组,对于祖先节点(将每摞砖最上方的一块设为祖先,设最下面的为祖先也是可以的),我们记录该集合中一共有多少块砖,对于其他节点,我们记录它上方有多少块砖,这样我们可以很容易地计算出下方有多少块。这样在合并x和y(设他们祖先为fx,fy)时,我们将dep[fx]赋给dep[fy](因为移到了上方,fx有多少块fy上方就有多少块),然后dep[fx]要加上原本的dep[fy]。难点就是在合并的过程中,dep数组怎么维护。其实很简单,只需要在路径压缩时顺带维护即可,如果一个节点x的祖先fx不是祖先(即已被合并过),那么dep[x]加上dep[fx]即可(等于头上又多了一摞砖,所以要加上)。
//jybの标程#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<string>#include<iostream> #include<queue> #include<cstdio>using namespace std; int n,m;int x,y;char c;int father,t1,t2;int fa[3000010];int dep[3000010];int getfather(int x){ if(x == fa[x]) return x; else{ int f = fa[x]; //用来判断自己记录的祖先是否真的是祖先 fa[x] = getfather(fa[x]); //路径压缩 if(fa[f] != f) //如果不是,则需要更新上面的砖块数 dep[x] += dep[f]; return fa[x]; }}void Init(){ for(int i = 1; i <= n; i++){ fa[i] = i; dep[i] = 1; }}int main() { freopen("bricks.in","r",stdin); freopen("bricks.out","w",stdout); scanf("%d%d",&n,&m); Init(); while(m--){ scanf(" %s",&c); if(c == 'M'){ scanf("%d%d",&x,&y); t1 = getfather(x); t2 = getfather(y); if(t1 != t2){ fa[t2] = t1; //合并 int ll = dep[t2]; //t2上面的砖块数等于t1那摞砖的总数 dep[t2] = dep[t1]; // dep[t1] += ll; //而t1的总数也需要加上t2的总数 } } else{ scanf("%d",&x); //因为问的在下面的砖块,所以减去自己 if(x == fa[x]){ //因为祖先和非祖先,dep数组的含义不 printf("%d\n",dep[x] - 1); continue; } father = getfather(x); //非祖先的话,因为dep保存的是自己上面还有多少个,所以用总的减去上面的和自己,即可得到下面有多少个 printf("%d\n",dep[father] - dep[x] - 1); } }}
原题,略有不同
Cube Stacking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 24922 Accepted: 8719
Case Time Limit: 1000MS
Description
Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:
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
Line 1: A single integer, P
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
Print the output from each of the count operations in the same order as the input file.
Sample Input
6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4
Sample Output
1
0
2
Source
USACO 2004 U S Open
- [机房练习赛4.3][poj1988]bricks
- [机房练习赛4.3]drive
- [机房练习赛4.3]graph
- [机房练习赛4.3]airplane
- POJ1988
- poj1988
- poj1988
- [机房练习赛4.4]setmod
- [机房练习赛4.5]note
- [机房练习赛4.5]number
- [机房练习赛4.6] derange
- [机房练习赛7.7]haokunnan
- [机房练习赛7.11]graph
- 20170117 机房『练习赛』
- 20170121 机房『练习赛』
- 20170122 机房『练习赛』
- 20170207 机房『练习赛』
- [机房练习赛] 题解 求逆序对
- 《Objective-C编程全解》 读书笔记 第十九章 并行编程
- 如何通过mysql去除重复数据呢
- 文件的上传和下载
- 《Objective-C编程全解》 读书笔记 第二十章 键值编码
- 划分无冲突子集
- [机房练习赛4.3][poj1988]bricks
- Hibernate中实体映射时的命名策略(2)
- 微服务架构(六): API Gateway
- c:计算合数的质因数,并输出为n=axbxcxd
- myeclipse+maven实现多模块项目struts+spring+mybatis
- JAVA进阶学习-反射机制
- Linux目录
- Leetcode-94. Binary Tree Inorder Traversal
- Eclipse的使用总结