有权并查集,Poj(1988)
2016-06-24 01:07
295 查看
题目链接: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];
题目大意:
有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 = 30000+10; 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=0;i<maxn;i++) { father[i] = i; dist[i] = 0; size[i] = 1; } 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]-1); } } return 0; }
相关文章推荐
- 电影评析
- Java之JSP和Servlet基础知识。
- 家庭网络方案分享
- 作者:唐欢----感悟和经验
- [!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfil
- 个人总结
- Struts2系列:(10)struts.xml和struts.properties详解
- 问题 H: 扫雷
- 普通View的measure流程
- 将博客搬至CSDN
- 表数据拼接,表头和数据用"|"和"\n"分隔
- laravel数据库迁移
- 《构建之法》读书笔记
- Docker容器操作中常用命令集合
- 使用json web token
- Do you want to know me?----Come in.
- [Python Scrapy] Windows下BeautifulSoup安装
- android中的网络解析xml,json,html框架
- 机器学习学习笔记 PRML Chapter 1.2 : Probability Theory
- java中Map,List与Set的区别