博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cube Stacking
阅读量:6717 次
发布时间:2019-06-25

本文共 2293 字,大约阅读时间需要 7 分钟。

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 18514   Accepted: 6426
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

6M 1 6C 1M 2 4M 2 6C 3C 4

Sample Output

102
1 #include"iostream" 2 #include"cstdio" 3 #include"cstring" 4 using namespace std; 5 const int ms=30001; 6 int parent[ms]; 7 int sum[ms]; 8 int under[ms]; 9 int find(int x)10 {11     if(parent[x]==x)12         return x;13     int t=find(parent[x]);14     under[x]+=under[parent[x]];15     parent[x]=t;16     return parent[x];17 }18 void merge(int a,int b)19 {20     int n;21     int fa=find(a);22     int fb=find(b);23     if(fa==fb)24         return ;25     under[fb]=sum[fa];26     parent[fb]=fa;27     sum[fa]+=sum[fb];28 }29 int main()30 {31     int i,j,p;32     for(i=0;i
>p;39 char c;40 while(p--)41 {42 cin>>c;43 if(c=='M')44 {45 cin>>i>>j;46 merge(j,i);47 }48 else49 {50 cin>>i;51 find(i);52 cout<
<
View Code

转载于:https://www.cnblogs.com/767355675hutaishi/p/3859066.html

你可能感兴趣的文章
欢迎加入Java私活外包QQ群
查看>>
Python风靡全宇宙,首要原因是它?
查看>>
Win7部署基础知识(8):使用WDS捕获与应用映像
查看>>
企业云桌面-14-将vCenter 6.5证书导入-受信任人-企业
查看>>
Python从菜鸟到高手(13):分片(Slicing)
查看>>
实战操作百度文库、百度经验营销,让您的“流量”稳居首页
查看>>
KMS激活服务器inactivity exceeded threshold警报处理
查看>>
IT草根的江湖之路之五:鉴于现实,屈服!
查看>>
拇指接龙游戏从WIN32向Android移植过程问题记录(1)
查看>>
2011年春季-C语言课程设计-报告格式
查看>>
sql之group by分析
查看>>
简单的webservice调用(天气预报)
查看>>
使用NdbUnit更新数据报“违反并发性 updatecommand 影响了预期 1 条记录中的 0 条”错误的原因...
查看>>
基于ArcGIS10.0和Oracle10g的空间数据管理平台十五(C#开发)-空间数据导出
查看>>
DB2 应用
查看>>
第十六章 为什么说张清“虎头蛇尾”
查看>>
ShiftOperators.cs
查看>>
C#中的预处理命令
查看>>
K-means聚类算法(非MapReduce实现)
查看>>
使用C#创建SQL Server的存储过程(Visual Studio 2005 + SQL Server 2005)
查看>>