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

#include <iostream>
#include<stdio.h>
using namespace std;
#define maxn 30003
int num[maxn];
int dis[maxn]={0};
int father[maxn];
int find_set(int x)
{
    if(x!=father[x])
    {
        int temp=father[x];
        father[x]=find_set(father[x]);
        dis[x]+=dis[temp];
    }
    return father[x];
}
void union_set(int a,int b)
{
    int fathera=find_set(a);
    int fatherb=find_set(b);
    father[fatherb]=fathera;
    dis[fatherb]=num[fathera];
    num[fathera]+=num[fatherb];
}
int main()
{
    int n;
    char ch[2];
    int a,b;
    scanf("%d",&n);
    int i;
    for(i=0;i<maxn;i++)
    {
        father[i]=i;
        num[i]=1;
    }
    while(n--)
    {
        scanf("%s%d",&ch,&a);
        if(ch[0]=='M')
        {
            scanf("%d",&b);
            union_set(a,b);
        }
        else
        {
            int temp=find_set(a);
            printf("%d\n",num[temp]-dis[a]-1);
        }
    }
    return 0;
}

PKU1988磁铁的更多相关文章

  1. 利用智能手机(Android)追踪一块磁铁(三)

    更新磁铁追踪算法的源代码,Android Studio项目工程 github地址:https://github.com/amazingyyc/MagnetLocate 说明:将磁铁的位置信息封装成消息 ...

  2. 利用智能手机(Android)追踪一块磁铁(二)

    在上一篇博客中提到了利用磁场强度推算传感器位置坐标的公式,下面就介绍怎么利用智能手机完成磁铁的追踪(任何具有磁感应器的装置均可以),这里主要是利用Android手机. 1:程序步骤: 首先将磁铁放置在 ...

  3. 利用智能手机(Android)追踪一块磁铁(一)

    之前看到一个外国人用iPhone做了一个追踪磁铁的Demo感觉不错(参考视频:http://v.youku.com/v_show/id_XODM2MjczNzE2.html),然后我就参考做了一个An ...

  4. 利用智能手机(Android)追踪一块磁铁(转)

    利用智能手机(Android)追踪一块磁铁(一) 利用智能手机(Android)追踪一块磁铁(二) 利用智能手机(Android)追踪一块磁铁(三)

  5. windows phone 8.1开发:磁铁|Tile更新

    原文出自:http://www.bcmeng.com/tile/ 上一篇给大家分享了toast通知操作的方法,这一篇文章我们就来看windows phone 8.1开发中的磁铁更新.磁铁是window ...

  6. MJJ玩磁铁

    题目: Problem D: MJJ玩磁铁 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 139  Solved: 9[Submit][Status][ ...

  7. Win10手动添加开始磁铁

    1.移动到C:\Users\spring\AppData\Roaming\Microsoft\Windows\Start Menu\Programs 2.拖拽

  8. WPF 控件总结

    内容控件:1.Button:当Button.IsCancel="True"时,点击按钮,对话框关闭.当Button.IsDefault="True",按回车触发 ...

  9. Windows phone应用开发[17]-xap提交异常处理

    在windows phone 应用提交操作上早在2011年时就写过一篇Windows phone 应用开发[4]-应用发布,那时wp应用提交官方市场的流程繁杂[超过了5步].因为上传和填写应用信息页面 ...

随机推荐

  1. 用JQuery怎么去一次性获取 aspTextBox 文本框的值

    如:<asp:TextBox ID="txtSubject" runat="server">aa</asp:TextBox> 第一次用服 ...

  2. React之前端路由

    通过之前的博客介绍,对于react,我们已经可以写单个组件.复合组件/单个页面了,接下来就是实现页面的跳转了,这个时候,我们就需要前端路由了. 一.react-router-dom 安装这个依赖,th ...

  3. linux下利用inode删除指定文件文件

    本文主要介绍使用inode删除异常文件名的文件的方法,供大家参考: 在Linux中,有时候会遇到文件名是乱码或者是某些特殊中文的文件,这时候通过文件名就很难删除. 同时,对于linux中的任何一个文件 ...

  4. [IC]Lithograph(0)半导体制造的基本过程

    1. 晶圆片 Wafer 晶圆(Wafer)是指硅半导体集成电路制作所用的硅芯片,由于其形状为圆形,故称为晶圆.晶圆是生产集成电路所用的载体,一般意义晶圆多指单晶硅圆片. 半导体行业从图1到图2,是一 ...

  5. 关于Django的core first 、db first

    db first 根据数据库的表生成类 django : python manage.py inspectdb code first 根据类创建数据库表: django: python manage. ...

  6. [剑指offer]数组中最小的K个数,C++实现

    原创博文,转载请注明出处! http://github.com/wanglei5205 http://cnblogs.com/wanglei5205 # 题目 输入n个整数,找出其中最小的K个数.例如 ...

  7. 类的静态方法@staticmethod

    静态方法 @staticmethod 静态方法是定义在类内部的函数,此函数的作用域是类的内部 说明: 静态方法需要使用 @staticmethod装饰器定义 静态方法与普通函数定义相同,不需要传入se ...

  8. Cannot setup mail box on Android

    Error: “You don’t have permission to sync with this server”  Solution: “You have 10 phone partnershi ...

  9. JSON简介[转]

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  10. 使用Linq动态排序

    Linq排序很方便,如果能动态创建Expression再排序就更方便了. 正序还是倒序排列 var order = typeof(Enumerable).GetMember(direction == ...