



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Pku1998 Cube Stacking/* Name: pku1998 Copyright: ecjtu_acm Author: yimao Date: 22-08-10 09:19 Description: 并查集*/一、题目DescriptionFarmer 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. OutputPrint the output from each of the count operations in the same order as the input file. Sample Input6M 1 6C 1M 2 4M 2 6C 3C 4Sample Output102二、题目大意及分析【题意】摘自ACM程序设计竞赛入门。有n个独立的磁铁(1-n标号)放在桌上,一个人对这n堆进行移动操作,然后另一个人询问。规则:M: 将含有编号为X的磁铁放在包含Y的顶端上;C:询问在编号Z下面磁铁的数目;【分析】利用并查集,每一堆磁铁看作一个集合,“M a b”就是将a归并到b的上面,每个集合是有序的。设定3个域Father: 根节点,初始化为本身;Num: 以该节点为根的集合的元素个数;Dis: 该点到其根的距离;那么由numfathera-disa-1就是以a节点为根的集合所含有的元素个数。三、代码及相关说明/*1988 Accepted 736K 610MS G+ 1023B 2010-08-22 09:06:36 */*1988 Accepted 516K 297MS C+ 1023B 2010-08-22 09:07:03 */#include#define arr 30005int fatherarr,numarr,disarr;/查找根节点;int find(int x) if(x!=fatherx) int temp=fatherx; fatherx=find(fatherx); disx+= distemp; return fatherx; /把a放在b上面;father_b改变;void union_set(int a,int b) int f_a=find(a),f_b=find(b); fatherf_b=f_a; disf_b=numf_a; numf_a+= numf_b;int main() int N,i,a,b; char ch3; while(scanf(%d,&N)!=EOF) for(i=1;iarr;i+) fatheri=i,numi=1,disi=0; /上面写i=N就Runtime Error; for(i=1;i=N;i+) scanf(%s,ch); i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 光缆架设施工方案及质量控制要点
- 新兴市场与行业集中变化-洞察及研究
- 幼儿园小司机主题教育活动方案
- 企业内部羽毛球运动会策划方案
- 农村互联网金融风险防控-洞察及研究
- 文化地理学在灾害管理中的应用-洞察及研究
- 无纺布在纺织业的创新应用-洞察及研究
- 智能物流平台的数据安全策略-洞察及研究
- 动态图神经网络压缩策略-洞察及研究
- 文化敏感性在职场交流中的作用-洞察及研究
- 化肥欠款协议模板
- “对校园欺凌说不”主题班会课件
- PLC电气控制设计污水处理系统样本
- 青春筑梦强国有我
- 班组质量管理和现场管理培训
- 新一代5G网络-全网部署与优化
- 导学案:化学合成材料
- 高中通用技术作品设计方案七篇
- MT-T 1199-2023 煤矿用防爆柴油机无轨胶轮运输车辆通用安全技术条件
- Linux系统基础操作培训文档
- 石油化工工艺装置蒸汽管道配管的设计
评论
0/150
提交评论