POJ-1988Cube Stacking/HDU-2818Building Block;
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 23283 | Accepted: 8166 | |
| Case Time Limit: 1000MS ->Link<- |
||
poj的一道题,同时也是HDu上的原题;
Building Block
Time Limit: 2000/1000
MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
-> Link
<-
题目还不错,带权值并查集;
题意:有N个木块初始都放在第一层,对木块有两种操作,一种是将编号为X的木块所在的这一整列木块放在编号为Y的木块所在的这列木块上方;一种是查询编号为X的木块的下方有多少木块;理解样例就明白了;
思路:本质上还是并查集,理解了并查集的原理就不难了;可以开两个数组low[]和high[],,一个记录某个木块上方有多少木块,一个记录这个木块到离他最近的父亲节点的木块个数(这里“最近的父亲节点”正是并查集的原理);在移动某个木块的时候实际上是移动它所在的这一整列木块,然后更新的时候只需将X的父亲节点的父亲节点改成Y的父亲节点,将Y的所在的这一列的木块个数赋给X的父亲节点的一个数组,将Y所在的这一整列木块个数加上X所在的这一整列木块个数;查询的时候利用find()函数递归查找;
const int N=30000+10;
int f[N],h[N],low[N],m;
int find(int x)
{
if(f[x]==-1)
return x;
int dx=find(f[x]);
low[x]+=low[f[x]];
return f[x]=dx;
}
int main()
{
scanf("%d",&m);
memset(low,0,sizeof(low));
memset(f,-1,sizeof(f));
for(int i=1; i<N; i++)
h[i]=1;
while(m--)
{
char c;
int x,y;
getchar();
scanf("%c",&c);
if(c=='M')
{
scanf("%d%d",&x,&y);
int xx=find(x);
int yy=find(y);
if(xx==yy)//如果两个木块已经在同一列就不用操作了;
continue;
f[xx]=yy;
low[xx]=h[yy];
h[yy]+=h[xx];
}
else
{
scanf("%d",&x);
find(x);
printf("%d\n",low[x]);
}
}
return 0;
}
也可以用结构体来记录两个变量;
const int N=30000+10;
struct node
{
int h,low;
} a[N];
int f[N],m;
int find(int x)
{
if(f[x]==-1)
return x;
int dx=find(f[x]);
a[x].low+=a[f[x]].low;
return f[x]=dx;
}
int main()
{
scanf("%d",&m);
char c;
int x,y;
memset(a,0,sizeof(a));
memset(f,-1,sizeof(f));
for(int i=0; i<=N; i++)//HDU上的数据有0的点,所以从0开始,不然就WA;
a[i].h=1;
while(m--)
{
getchar();
scanf("%c",&c);
if(c=='C')
{
scanf("%d",&x);
find(x);
printf("%d\n",a[x].low);
}
else
{
scanf("%d%d",&x,&y);
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
f[xx]=yy;
a[xx].low=a[yy].h;
a[yy].h+=a[xx].h;
}
}
}
return 0;
}
POJ-1988Cube Stacking/HDU-2818Building Block;的更多相关文章
- POJ 3831 & HDU 3264 Open-air shopping malls(几何)
题目链接: POJ:id=3831" target="_blank">http://poj.org/problem?id=3831 HDU:http://acm.h ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- POJ 3481 & HDU 1908 Double Queue (map运用)
题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...
- POJ 3835 & HDU 3268 Columbus’s bargain(最短路 Spfa)
题目链接: POJ:http://poj.org/problem?id=3835 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=3268 Problem ...
- POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(数学)
题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Descript ...
- POJ 3928 & HDU 2492 Ping pong(树阵评价倒数)
主题链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Descript ...
- ●线段树的三个题(poj 3225,hdu 1542,hdu 1828)
●poj 3225 Help with Intervals(线段树区间问题) ○赘述题目 给出以下集合操作: 然后有初始的一个空集S,和以下题目给出的操作指令,并输入指令: 要求进行指令操作后,按格式 ...
- POJ 3480 & HDU 1907 John(尼姆博弈变形)
题目链接: PKU:http://poj.org/problem? id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1907 Descri ...
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
题目链接: POJ 1012: id=1012">http://poj.org/problem?id=1012 HDU 1443: pid=1443">http:// ...
- POJ 3486 & HDU 1913 Computers(dp)
题目链接:PKU:HDU: PKU:http://poj.org/problem?id=3486 HDU:pid=1913" target="_blank">htt ...
随机推荐
- Windows查杀端口
Windows环境下当某个端口被占用时,通过netstat命令进行查询pid,然后通过taskkill命令杀进程. 一.查询占用端口号的进程信息 netstat -an|findstr 二.杀掉占用端 ...
- Service官方教程(5)后台服务发送通知、把服务变前台服务。
1.Sending Notifications to the User (发送通知) Once running, a service can notify the user of events usi ...
- foreach的学习使用归纳
1: 实现对双向链表的遍历使用 LinkedList<string> llary = new LinkedList<string>(); llary.AddLast (&quo ...
- C#中的list的System.Predicate<in T>和System.Comparison<in T>的应用
public class Data { ; ; ; ; public Data() { count++; ma = count; } } //一句话删除满足要求的集合 Asm.RemoveAll((D ...
- iOS开发系列--通知与消息机制--转
来自:http://www.cocoachina.com/ios/20150318/11364.html 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户 ...
- 使用ImageMagick处理图片后导致图片变大的问题
最新在使用ImageMagick处理图片时,发现有时经过处理的图片变大的问题. 例如原图为27K,经处理后的图片会变为77K. 先看测试代码: // imgtest.cpp : 定义控制台应用程序的入 ...
- .Net应用自定义鼠标样式
(调用系统API的方法) 1.引用命名空间 using System.Runtime.InteropServices; 命名空间提供各种各样支持 COM 互操作 及平台调用服务的成员.using Sy ...
- vim快捷键参考
一. 移动: h,j,k,l: 左,下,上,右. w: 下一个词的词首.W:下一个单词(不含标点). e:下一个词的词尾.E:不含标点. b:上一个词的词首.B:不含标点. <>: v 模 ...
- reveal.js让程序员做ppt也享受快乐
前言 程序员除了会写的一手漂亮的代码,也要求做出风格优雅的PPT,诸如向领导汇报工作.向小组成员反馈项目进展自己的工作等等.就本人而言,做ppt还要去找模板,还需要设计风格,内心是焦灼的.于是乎,我搜 ...
- php学习知识点
1.PHP 代码被包含在特殊的起始符和结束符中 <? ?> 2.php的用途 服务端脚本 命令行脚本. 编写桌面应用程序.3.输出语句.文本 echo printf4.$_SERVER 是 ...