有权并查集,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 ...
随机推荐
- Alibaba Java Coding Guidelines 安装(eclipse)并简单使用
源码地址:https://github.com/alibaba/p3c 1.安装 第1步.help >> Instakll New Software... 第2步.在Work with中输 ...
- 8-----BBS论坛
BBS论坛(八) 8.1.发送邮箱验证码功能 (1)cms/resetemail.html {% from 'common/_macros.html' import static %} {% bloc ...
- cesm1_2_2在南信大大型机上的移植以及运行简单case的步骤
真实验证有效:点击链接 查看具体移植过程.
- android Binder机制(一)架构设计
Binder 架构设计 Binder 被设计出来是解决 Android IPC(进程间通信) 问题的.Binder 将两个进程间交互的理解为 Client 向 Server 进行通信. 如下:bind ...
- 卸载 maya
AUTO Uninstaller 更新下载地址 1.选择MAYA 2.选择版本 3.点击[开始卸载]
- 二叉搜索树-php实现 插入删除查找等操作
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...
- javaScript 删除确认实现方法总结分享
第一种方法:挺好用的,确认以后才能打开下载地址页面.原理也比较清晰.主要用于删除单条信息确认.<SCRIPT LANGUAGE=javascript> function p_del() { ...
- Python札记1--基础
Python语言虽然简单,但相关的细节仍需要注意,本系列札记,旨在记录学习python过程中需要注意的相关知识点或相关难点的理解. 1. 列表 a. python的列表list中元素的类型可以不同 b ...
- HBuilder更换部分
1.HBuilder工作空间的更换 HBuilder的默认工作空间的修改并不像其他ide一样,在设置里进行更改,而是在工具中进行设置. 1.单击菜单栏“工具”,选择“变更默认代码存放目录” 2.进行修 ...
- C#委托(一)——说明及举例
C#命名空间下有五种类型,分别为: 类.构造.接口.枚举.委托. 委托被定义为5中基本类型的一种,也就意味着代码可以这么写: using System; namespace Test { delega ...