poj1988

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

#include<iostream>

using
namespace std;

const
int maxn = 30000+100;

int F[maxn], D[maxn], S[maxn];

void
set(int
n)

{

    for(int
i =0 ;i < n; i++)

    {

        F[i] = i;

        D[i] = 0;

        S[i] = 1;

    }

}

void
find(int
x)

{

    if(x != F[x])

    {

        find(F[x]);

        D[x]+= D[F[x]];

        F[x] = F[F[x]];

    }

}

void
Union(int
x, int
y)

{

    find(x);find(y);

    x = F[x]; y= F[y];

    F[x] = y;

    D[x] = S[y];

    S[y] += S[x];

}

int
main()

{

    set(maxn);

    int
P;

    cin>>P;

    char
c; int
a, b;

    while(P--)

    {

        cin >> c;

        if(c==‘M‘)

        {

            cin>> a >> b;

            Union(a, b);

        }else

        {

            cin>>a;

            find(a);

            cout << D[a] <<endl;

        }

    }

    return
0;

}

  

时间: 2024-07-29 04:56:35

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

并查集的高效之处在于路径压缩和延迟更新. 在本题中需要额外维护子树的规模以及当前子树节点到跟的距离两个数组. 由于一个新的数必然是两棵树拼接而成,对于子树规模的更新直接相加即可, 对于节点到跟的距离: 我们让a树的根指向b树的根,同时更新a树根到(a+b)树根(即b树根)的距离为size_of(Tree(b))即可完成维护. http://poj.org/problem?id=1988 1 #include <cstdio> 2 #include <cstring> 3 #incl

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 w

加权并查集——(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

POJ1988 Cube stacking(非递归)

n有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作: nM x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上. nC x : 问方块x下面有多少个方块. n操作最多有 P (P<=100,000)次.对每次C操作,输出结果. 这题挺厉害的,真的不容易想 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<strin

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