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 ...
随机推荐
- Apollo源码搭建调试看一文就够
Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场景. 我 ...
- AJPFX关于代码块的总结
代码块: { 执行语句; }(1) 当出现在局部位置时, 为局部代码块. 局部位置: 如语句块中, 函数中, 构造代码块中, 静 ...
- skeljs框架关键点使用
global 全局 style.css containers: 1400px;容器宽度 xlarge 超大屏(media: max-width:1680px) style-xlarge.cs ...
- [BZOJ1009][HNOI2008]GT考试 DP+矩阵快速幂+KMP
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1009 我们令$dp(i,j)$表示已经填了$i$位,而且后缀与不幸运数字匹配了$j$位,那 ...
- 了解java内存回收机制-博客导读
此文作为读优质博客前的导读文 1.如何判断对象是否该回收 该对象是否被引用,是否处于不可达状态 2.对象的引用机制 强引用.软引用.弱引用.虚引用 3.垃圾回收机制如何回收.算法. 串行回收.并行回收 ...
- mysql 的 case when then 用法 和null 的判断
表:一个表 aa 有两个字段 id 和 sex ,第1条记录的sex 为空串 ('') 第二条记录的sex 为空 (null) 1. 用法: 第一种: select (case 字段名 whe ...
- Java对Redis基本使用
1 引入jar包 java是通过Jedis对redis进行操作的,首先引入jedis.jar <dependency> <groupId>redis.clients</g ...
- 2015年度精品 最新力作32位和64位xp,win7,win8,win10系统下载(电脑城专用版)
一.系统主要特点 1.安装维护方便快速 - 全自动无人值守安装,采用万能GHOST技术,安装系统过程只需3-5分钟,适 合新旧各种机型. - 集成常见硬件驱动,智能识别+预解压技术,绝大多数硬件可以快 ...
- PHP网络协议相关考点
HTTP状态码 HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码. HTTP状态码主要有5种,代表5种不同类型的响应: 1xx:信息性状态码,代表接 ...
- windows环境开启PHP fileinfo扩展
fileinfo作用:本模块中的函数通过在文件的给定位置查找特定的 魔术 字节序列 来猜测文件的内容类型以及编码(通俗来讲就是获取文件的MIME信息) 开启PHP fileinfo扩展的方法: 1.下 ...