poj 1988 Cube Stacking

点击打开链接poj 988

思路: 带权并查集
分析:
1 题目给定2种指令 M x y把x的集合放在y集合的上面,C x求x的下面有多少个元素
2 我们用rank[x]表示x以下有多少个元素,那么对于指令M x y我们始终把左边的合并到右边,那么这样rank就满足压缩的性质
3 但是因为这边的合并和普通不一样,它是把x所在的集合放在y所在集合上面,实际上是x的跟节点合并到y集合的最远点,所以我们应该开个数组记录当前集合最远的点

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 300010;

int father[MAXN];
int rank[MAXN];
int max_rank[MAXN];

void init(int n){
    memset(rank , 0 , sizeof(rank));
    memset(max_rank , 0 , sizeof(max_rank));
    for(int i = 1 ; i <= n ; i++)
        father[i] = i;
}

int find(int x){
    if(father[x] != x){
       int fa = father[x];
       father[x] = find(fa);
       rank[x] += rank[fa];
    }
    return father[x];
}

void move(int x , int y){
    int fx = find(x);
    int fy = find(y);
    if(fx != fy){
        father[fx] = fy;
        rank[fx] = max_rank[fy]+1;
        max_rank[fy] += max_rank[fx]+1;
    }
}

int main(){
    char ch;
    int n , x , y;
    while(scanf("%d%*c" , &n) != EOF){
         init(n);
         for(int i = 0 ; i < n ; i++){
            scanf("%c" , &ch);
            if(ch == 'M'){
               scanf("%d%d%*c" , &x , &y);
               move(x , y);
            }
            else{
               scanf("%d%*c" , &x);
               int fx = find(x);// 注意先压缩
               printf("%d\n" , rank[x]);
            }
         }
    }
    return 0;
}
时间: 2024-08-10 12:12:42

poj 1988 Cube Stacking的相关文章

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 1128 Frame Stacking 拓扑

        和上一题一样的一个覆盖问题,这个要给出全部可能序列,所以就有个递归过程      用了很多stl,本来以为会TLE,结果0ms就过了,还有很大的优化空间,比如可以把拓扑的dfs和入度为0结合起来就可以少很多重复搜索的情况.   /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <i

并查集专题【完结】

个人整理 并查集 [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

数据结构专题

打星号的表示个人认为比较经典,或是算法比较好的题目   1195 Mobile phones 树状数组 1455 1521 Entropy huffman 1703 Find them, Catch them 并查集 1785 Binary Search Heap Construction 1794 Castle Walls 逆序对 1961 Period KMP重复因子 1984* Navigation Nightmare 并查集+坐标平移 1986* Distance Queries LCA

【北大夏令营笔记-并查集】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

A题常用数据结构

  基本结构 高级结构 题单 集合结构   幷查集 POJ 1182 POJ 1308 POJ 1611 POJ 1986 POJ 1988 线性结构 数组 栈 队列 双端队列 POJ POJ POJ POJ POJ 树状结构 二叉树 BST AVL树 splay树(伸展树) Treap Cartesian Tree Size Balance Tree POJ 3580(splay tree) POJ 2761(Treap) POJ 2201(Cartesian Tree) POJ 3481(S

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj 题型分类

主流算法: 1.搜索 //回溯 2.DP(动态规划) 3.贪心 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同等安置矩形的并的面积与周长 7.组合数学 //Polya定理 8.模拟 9.数据结构 //并查集.堆 10.博弈论 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 2377, 2380, 1318, 1877, 1928, 1971,