全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电建物业考试题目及答案
- 雨篷雨棚项目可行性研究报告
- 内科护理学试题及答案泌尿系统作业习题
- 2025年成都百万职工技能大赛(低压电工)备赛试题库(含答案)
- 广西中考物理5年(2021-2025)真题分类汇编:专题11 电流和电路(解析版)
- 2020-2025年注册城乡规划师之城乡规划原理自我检测试卷A卷附答案
- 聘用科学顾问协议书模板
- 识别虚拟货币协议书
- imap协议书是指什么
- 农产品批发市场统一称重创新创业项目商业计划书
- 输液泵教学培训课件
- 动物样品采集培训课件
- QC/T 798-2025汽车用多层塑料燃油管
- 中国汉字听写比赛常用词汇表
- 医保支付方式改革课件
- 《无人机复合材料结构设计与制造技术》全套教学课件
- 2025至2030年中国石墨润滑剂市场现状分析及前景预测报告
- (高清版)DB11∕T 509-2025 房屋建筑修缮工程定案和施工质量验收规程
- 【课件】滑动摩擦力+课件+-2024-2025学年人教版(2019)必修第一册
- (2025版)中国老年糖尿病诊疗指南
- 暑假雏鹰活动方案
评论
0/150
提交评论