POJ1988 并查集的使用

Cube Stacking

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

 1 #include<stdio.h>
 2 #define N 30001
 3
 4 int count[N], num[N], pre[N];
 5
 6 void inite()
 7 {
 8     for(int i = 0; i < N; i++)
 9     {
10         count[i] = 0;
11         num[i] = 1;
12         pre[i] = i;
13     }
14 }
15
16 int find(int x)
17 {
18     if(pre[x] == x)
19         return x;
20
21     int t = find(pre[x]);
22     count[x] += count[pre[x]];
23     pre[x] = t;
24     return t;
25
26 }
27 void Union(int x, int y)
28 {
29     int i = find(x);
30     int j = find(y);
31     if(i == j)
32     {
33         return;
34     }
35     count[i] = num[j];
36     num[j] += num[i];
37     pre[i] = j;
38 }
39
40
41
42 int main()
43 {
44     int i, x, y, n;
45     char s[2];
46     scanf("%d",&n);
47     inite();
48     for(i = 0; i < n; i++)
49     {
50         scanf("%s",s);
51         if(s[0] == ‘M‘)
52         {
53             scanf("%d%d",&x,&y);
54             Union(x,y);
55         }
56         else if(s[0] == ‘C‘)
57         {
58             scanf("%d",&x);
59             int c = find(x);
60             printf("%d\n",count[x]);
61         }
62     }
63     return 0;
64 }

 
时间: 2024-08-23 04:29:02

POJ1988 并查集的使用的相关文章

poj1988 简单并查集

B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%lld & %llu Submit Status Description Input Output Sample Input Sample Output Hint Description Farmer John and Betsy are playing a game with

POJ1988 CubeStacking (并查集)

本文出自:http://blog.csdn.net/svitter 题意: 开始有N堆方块,编号从1-n.每次移动一堆方块,最后求某个方块下面方块的个数. 输入输出分析: 开始输入一个数字P,代表输入操作个数. 此处发现在g++4.8的版本中,类似与 char ch[0]这样的数组也是可以开辟的... 一个不小心开辟了这样一个数组..然后return 0完全找不到错误所在.. 随后的2-1+p行,每行一组操作数据. M i j代表移动i的堆到j的堆上. C i代表求出i以下的方块个数. 数据结构

带权并查集 POJ1988 POJ2492

单纯的并查集很简单,带权并查集还能解决更多的问题,才更好玩,来个题热身.对于下面的知识,现在就当你已经熟练掌握了递归和并查集的路径压缩. POJ1988:题目链接 http://poj.org/problem?id=1988 题目大意:有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作:  M x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上.  C x : 问方块x下面有多少个方块. 操作最多有 P (P<=100,000)次.对每次C操作

poj1988 Cube Stacking(并查集

题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[1 6]上为[2 4 1 6]),C x为输出x下面有几个数. 思路:并查集每个集合以栈最下面的数为根,维护两个数组num[x]表示x所在集合节点总数,count[x]表示x下方节点个数.每次查找压缩路径的时候更新count(换父节点的时候每轮都把父节点的count加给儿子,就可以一直更新到x所在栈

加权并查集——(POJ1988)Cube Stacking

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

Cube Stacking POJ1988 【并查集的应用】

http://poj.org/problem?id=1988 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

POJ1988 Cube Stacking(并查集)

题目链接:http://poj.org/problem?id=1988 题意:有n个元素,开始每个元素各自在一个栈中,有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 第二种操作是询问含有x元素下面有多少个元素. 思路: 并查集,把每一堆看作一个栈,堆的下方看作栈顶.因为当我们知道栈中元素的总数,和某元素到“栈顶”的距离, 我们就能知道这个元素下面有多少元素.合并操作的时候,始终使用在下面栈的根来做合并之后的根,这样也就达到了栈中的根是栈中的“栈顶”元素的效果,我们只需在每个“栈

并查集练习2(带权并查集)

明天旅游去爬山逛庙玩,今天练一天然后早早睡觉啦~ poj1703 Find them, Catch them (带权并查集) 1 #include<cstdio> 2 const int N=1e5+1; 3 int f[N]; 4 int r[N];//表示与父节点的关系,0同类,1不同类 5 int n; 6 void init(){ 7 for(int i=1;i<=n;++i){ 8 f[i]=i; r[i]=0; 9 } 10 } 11 int fin(int x){ 12 i

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最