有权并查集,Poj(1988)
题目链接: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)的更多相关文章
- 并查集 POJ 1988
#include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
- POJ 1988 Cube Stacking 【带权并查集】
<题目链接> 题目大意: 有几个stack,初始里面有一个cube.支持两种操作: 1.move x y: 将x所在的stack移动到y所在stack的顶部. 2.count x:数在x所 ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- poj 1733 Parity game(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- POJ 1984 Navigation Nightmare(二维带权并查集)
题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1 F2 L D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...
随机推荐
- Java学习笔记day08_day09_对象实例化_private_this
1.类与对象 类就是一个模版. 对象的实例化就是根据模版类, 使用new关键字创建实际的对象. 2.类的定义及创建对象 类的定义格式: public class 类名{ //属性(变量) 数据类型 变 ...
- php数组·的方法-数组与数据结构
/*数组与数据结构*/ //shuffle() 随机打乱数组 //array_push() 数组末尾添加元素 //array_pop() 数组末尾删除元素 //array_shift() 数组首位删除 ...
- 转载 Some indexes or index [sub]partitions of table VAS.TAB_PUB_CALLLOG have been marked unusable
http://www.xifenfei.com/2011/12/some-indexes-or-index-subpartitions-of-table-vas-tab_pub_calllog-hav ...
- Murano Weekly Meeting 2015.08.25
Meeting time: 2015.August.25th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting summ ...
- HDU 1045——Fire Net——————【最大匹配、构图、邻接矩阵做法】
Fire Net Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- POJ 3164——Command Network——————【最小树形图、固定根】
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 15080 Accepted: 4331 ...
- checkbox判断选中的三种方法
方法一: if ($("#checkbox-id")get(0).checked) { // do something } 方法二: if($('#checkbox-id' ...
- JavaFX--第3天窗口布局
1.windows之间的交互 2.关闭程序 3.布局镶嵌 1.windows之间的交互 我们要实现“确定”.“取消”之类的功能:就像我们平时使用Word的时候要关闭会提示要不要保存的信息. 步骤如下: ...
- dreamweaver,access2010,数学
dreamweaver 1,点插入-表格-设置表格. 2,再次修改表格,打开属性修改指标. (修改图片时,也可以选中图片打开对应的属性修改) 设置字体: 1,打开属性-页面属性,弹出操作窗口,设置想改 ...
- (开发)ESLint - 代码规范
参考文档:http://eslint.cn/ ESLint 是在 ECMAScript/JavaScript 代码中识别和报告模式匹配的工具,它的目标是保证代码的一致性和避免错误.在许多方面,它和 J ...