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;
}
全部评论

相关推荐

淬月星辉:专利是什么?至少描述一下吧,然后把什么计算机二级、普通话这种拉低格调的证书删掉,不然hr以为你没东西写
点赞 评论 收藏
分享
来,说点可能被同行“骂”的大实话。🙊当初接数字马力Offer时,朋友都说:“蚂蚁的“内包”公司?你想清楚啊!”但入职快一年后的今天,我反而对他有了不一样的看法!🔹&nbsp;是偏见?还是信息差!之前没入职之前外面都在说什么岗位低人一等这类。实际上:这种情况不可至否,不能保证每个团队都是其乐融融。但我在的部门以及我了解的周边同事都还是十分好相处的~和蚂蚁师兄师姐之间也经常开一些小玩笑。总之:身份是蚂蚁公司给的,地位是自己挣的(一个傲娇女孩的自述)。🔹&nbsp;待遇?玩的就是真实!试用期工资全额发!六点下班跑得快(早9晚6或者早10晚7,动态打卡),公积金顶格交。别听那些画饼的,到手的钱和下班的时间才是真的(都是牛马何必难为牛马)。🔹&nbsp;能不能学到技术?来了就“后悔”!我们拥有权限直通蚂蚁知识库,技术栈多到学不完。说“学不到东西”的人,来了可能后悔——后悔来晚了(哈哈哈哈,可以不学但是不能没有)!💥&nbsp;内推地址:https://app.mokahr.com/su/ueoyhg❗我的内推码:NTA6Nvs走我的内推,可以直达业务部门,面试流程更快速,进度可查!今天新放HC,之前挂过也能再战!秋招已经正式开始啦~机会就摆在这,敢不敢来试一试呢?(和我一样,做个勇敢的女孩)
下午吃泡馍:数字马力的薪资一般哇,5年经验的java/测试就给人一万出头,而且刚入职第三天就让人出差,而且是出半年
帮你内推|数字马力 校招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务