Cube Stacking

题目传送

题目描述

Language:
Cube Stacking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 30051 Accepted: 10556
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

我先吐槽一波 嗯嗯……难受 mmp 了 递归实现并查集真是无敌了 这种题题意看的朦朦胧胧的 我真是弱爆了 看别人的题解都得好一会吭哧

真心题意很难懂 就是一开始一堆就一个盒子 完了有两种(秀)操作,M A B 把A盒子所在的这一坨放到B所在的这一坨上面,注意是以坨为单位放的,放完之后就变成了一坨 C A就是问咱A盒子所在的这一坨中(其实就是一坨)A下面还有几个盒子 ,这都神吗鬼操作……代码奉上 如果一时半会看的听懵逼 没关系 懵逼完之后才会出奇迹

#include<cstdio>
#include<iostream>//唉……并查集加上递归简直无敌了 我虚了虚了 看题看的都朦朦胧胧的 

using namespace std;
const int maxn=3*1e4+7;

int N,P;
int pre[maxn];//存放i最近一次操作的堆顶节点
int dis[maxn];//i到最近堆顶的距离,是不断更新的因为堆顶一直在变
int rank[maxn];//i所在的堆的高度,也是不断更新的以为堆顶一直在变
char s[3];
int Find(int x)
{
	if(x!=pre[x])//广大码友们注意了哈 这儿是if 不是while 大爷的 第一次看别人题解都瞧走眼了 找了好半天bug
	{
		int t=pre[x];
		pre[x]=Find(t);//一层一层的更新dis dis dis aaaaaa
		dis[x]+=dis[t];//好一个堆的叠加,当前节点到堆顶的距离加上次堆顶到上一堆顶的距离,递归结束后dis[x]存的就是到最高堆顶的距离 
	}
	return pre[x];//返回的也是x的最高堆顶节点 
}
void John(int x,int y)
{
	int fx=Find(x);
	int fy=Find(y);
	if(fx!=fy)
	{
		pre[fy]=fx;//fx变成了新的堆顶
		dis[fy]=rank[fx];//fy到fx所在的这个堆堆顶的距离 
		rank[fx]+=rank[fy];//更新fx这个堆的高度 
	}
	return ;
}
int main()
{
	int A,B;
	while(~scanf("%d",&P))
	{
		int i,j,k;
		for(i=1;i<=30000;i++)
		{
			pre[i]=i;
			rank[i]=1;
			dis[i]=0;
		} 
		for(i=1;i<=P;i++)
		{
			scanf("%s",s);
			if(s[0]=='M')
			{
				scanf("%d%d",&A,&B);
				John(A,B);
			}
			else
			{
				scanf("%d",&A);
				int t=Find(A);
				printf("%d\n",rank[t]-dis[A]-1);//堆的高度减去A到堆顶的距离就是下面hao
			}
		}
	}
	return 0;
}
全部评论

相关推荐

菜菜咪:1. 可以使用简历网站的模版,美观度会更好一点 2. 邮箱可以重新申请一个,或者用qq邮箱的别名,部分hr可能会不喜欢数字邮箱 3. 项目经历最好分点描述,类似的项目很多,可以参考一下别人怎么写的 4. 自我评价可加可不加,技术岗更看重技术。最后,加油,优秀士兵
点赞 评论 收藏
分享
10-09 09:39
门头沟学院 C++
HHHHaos:这也太虚了,工资就一半是真的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-21 17:16
科大讯飞 算法工程师 28.0k*14.0, 百分之三十是绩效,惯例只发0.9
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务