题目链接:http://poj.org/problem?id=1988

题目大意:

有n个从1到n编号的箱子,将每个箱子当做一个栈,对这些箱子进行p次操作,每次操作分别为以下两种之一:

输入 M x y:表示将编号为x的箱子所在的栈放在编号为y的箱子所在栈的栈顶.
输入 C x:计算编号为x的所表示的栈中在x号箱子下面的箱子数目.

思路:

move a,b的时候(合并的时候),b其中一个作为子树,dist[fb],距离根节点的距离为size[fa],然后size[fa]+=size[fb];

count c的时候,就是size[fa[c]]-dist[c]-1啦。

注意的是:Find_set的时候,要将dist处理好啦,(还是在同一个树中)

if(x!=father[x])

int t=father[x];

father[x]=Find_set[father[x]];

dist[x]+=dist[t];

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm> using namespace std; const int maxn = +;
int father[maxn];
int size[maxn]; ///表示团的大小
int dist[maxn]; ///到根节点的距离 int Find_set(int x)
{
if(x!=father[x])
{
int t=father[x];
father[x] = Find_set(father[x]);
dist[x]+=dist[t];
}
return father[x];
} void Union(int x,int y)
{
father[y] = x;
dist[y] = size[x];
size[x] +=size[y];
} int main()
{
for(int i=;i<maxn;i++)
{
father[i] = i;
dist[i] = ;
size[i] = ;
}
int t;
scanf("%d",&t);
getchar();
while(t--)
{
char order;
scanf("%c",&order);
if(order=='M')
{
int x,y;
scanf("%d%d",&x,&y);
getchar();
int fa = Find_set(x);
int fb = Find_set(y);
if(fa!=fb)
Union(fa,fb);
}
else {
int c;
scanf("%d",&c);
getchar();
printf("%d\n",size[Find_set(c)]-dist[c]-);
}
}
return ;
}

有权并查集,Poj(1988)的更多相关文章

  1. 并查集 POJ 1988

    #include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...

  2. POJ 1988 Cube Stacking( 带权并查集 )*

    POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...

  3. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

  4. POJ 1988 Cube Stacking 【带权并查集】

    <题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...

  5. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  6. poj 1733 Parity game(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...

  7. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  8. POJ 1984 Navigation Nightmare(二维带权并查集)

    题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1  F2  L  D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...

  9. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  10. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

随机推荐

  1. select随笔

    粘贴下面代码 select 美化 <!doctype html> <html lang="en"> <head> <meta charse ...

  2. git命令行操作:拉不到最新代码???

    现场场景:   仓库中有一个包名使用了驼峰命名,还有一个非驼峰的同名包, windows系统下因为不区分文件夹大小写,拉取没问题,但是本地push不上去.打算到Linux上clone下来后,删除那个驼 ...

  3. Web开发中FormData对象的使用

    参考: FormData 对象的使用 - Web API 接口 | MDN

  4. table size script :

    I think Jonathan Lewis has explained the algorithm before, but it's alsosomething that we have inves ...

  5. cmake中文帮助文档

    CMake的 在这个页面 了解CMake的生成命令 在摇篮使用cmake变量 报告问题 使用过Android Studio 2.2及更高版本,可以使用NDK和CMake的 编译C和C ++代码到本机库 ...

  6. mysql 死锁解决办法

    查询表的时候,发现一圈圈转啊转,就是不出来数据,猜测表被锁住 解决办法 : mysql> show processlist ; mysql> kill 4;       说明 : 4为 i ...

  7. Java实现中文词频统计

    昨日有个中文词频统计的需求, 百度一番后, 发现一大堆标题党文章, 讲的与内容严重不符, 这里就简单记录下自己实现的流程吧! 与英文单词的词频统计不同, 中文的难点在于如何分词, 不过好在有许多优秀的 ...

  8. [Android]apk反编译方法

    在学习Android开发的过程你,你往往会想去学习别人的apk是怎么开发的,作为一个开发者,你可能会很想知道这些效果界面是怎么去实现的,这时,你便可以对改应用的APK进行反编译查看.下面是我参考了一些 ...

  9. pat1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  10. pat1016. Phone Bills (25)

    1016. Phone Bills (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A long-di ...