【北大夏令营笔记-并查集】poj1988-Cube Stacking

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

并查集的应用
将每一堆当成一个集合,当这一堆压倒那一堆的时候,进行集合合并

AC代码:

#include<stdio.h>
#include<stdlib.h>
#define MAX 31000
int parent[MAX];
int sum[MAX];
int under[MAX];
int GetRoot(int a)
{
    if(parent[a]==a)
    return a;
    int t=GetRoot(parent[a]);
    under[a]+=under[parent[a]];
    parent[a]=t;
    return parent[a];
}
void merge(int a,int b)
{
  //把b所在的堆,叠放到a所在的堆
  a=GetRoot(a);
  b=GetRoot(b);
  if(a==b)
  return;
  parent[b]=a;
  under[b]=sum[a];//under[b]赋值前一定是0,因为
  //parent[b]=b,b一定是原b所在堆的最底下的
  sum[a]+=sum[b];
}
int main()
{
    int i,j,n;
    for(i=0;i<MAX;i++)
    {
       sum[i]=1;
       parent[i]=i;
       under[i]=0;
    }
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        char s[20];
        int a,b;
        scanf("%s",s);
        if(s[0]=='M'){
              scanf("%d %d",&a,&b);
              merge(b,a);
        }else
        {
            scanf("%d",&a);
            GetRoot(a);//进行矩阵压缩,更新以前没有更新的under值(加上父亲以及所有祖先的under值)
            //最下面的那个是老祖宗,一般下面被压的的辈分比上面高
            printf("%d\n",under[a]);
        }
    }
    return 0;
}
时间: 2024-08-31 22:08:13

【北大夏令营笔记-并查集】poj1988-Cube Stacking的相关文章

【北大夏令营笔记-并查集】poj1611-The Suspects

The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21517 Accepted: 10422 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To mi

【北大夏令营笔记-数学题】百练1700-Crossing River

1700:Crossing River 查看 提交 统计 提示 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the

【北大夏令营笔记-线段树】POJ3468-A Simple Problem with Integers

A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57993 Accepted: 17658 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of ope

【北大夏令营笔记-线段树】POJ3264-Balanced Lineup

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32777 Accepted: 15424 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John de

【北大夏令营笔记-动态规划】poj1458-Common Subsequence

Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37543 Accepted: 15016 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., x

HDU 2818 Building Block, poj 1988 Cube Stacking:带权并查集

链接: HDU: http://acm.hdu.edu.cn/showproblem.php?pid=2818 POJ:  http://poj.org/problem?id=1988 题目: Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbered 1...N.Initially, there are N piles, and each pile contai

并查集专题【完结】

个人整理 并查集 [poj] 第一题 poj 1182 食物链 点击打开链接poj 1182 思路: 带权并查集 分析: 1 典型的带权并查集的题目 2 如果x和y是同类的关系认为是0,如果是x吃y那么关系认为是1,那么x和y的关系为2就说明y吃x 3 那么如果x和y同类则rank[x] == rank[y] , 如果x吃y那么rank[x] == (rank[y]+1)%3 , 注意mod3的使用 点击查看代码 第二题 poj 1308 Is It A Tree? 点击打开链接 poj 130

图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 B(G).其中 T(G)是遍历图时所经过的边的集合,B(G) 是遍历图时未经过的边的集合.显然,G1(V, T) 是图 G 的极小连通子图,即子图G1 是连通图 G 的生成树. 深度优先生成森林   右边的是深度优先生成森林: 连通图的生成树不一定是唯一的,不同的遍历图的方法得到不同的生成树;从不

并查集(Disjoint Set)

在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将属于同一组的元素所在的集合合并,其间要反复查找一个元素在哪个集合中.这一类问题其特点是看似并不复杂,但数据量极大,若用正常的数据结构来描述的话,往往在空间上过大,计算机无法承受:即使在空间上勉强通过,运行的时间复杂度也极高,根本就不可能在规定的运行时间(1-3秒)内计算出试题需要的结果,只能用并查集来描述. 定义 并查集(Disjoint Set),即"不相交集合",是一种树型的数据结构