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 ...
随机推荐
- 背包DP HDOJ 5410 CRB and His Birthday
题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == ...
- Android推送服务(1)几种实现方式
1.几种常见的解决方案实现原理 1)轮询(Pull)方式:应用程序应当阶段性的与服务器进行连接并查询是否有新的消息到达,你必须自己实现与服务器之间的通信,例如消息排队等.而且你还要考虑轮询的频率,如果 ...
- Vue自定义过滤器格式化数字三位加一逗号
<template> <div class="index-compont"> <div class="totalCount"> ...
- spring tool suite开发环境搭建
先把是构建工具maven: maven里面有一个conf文件夹,然后里面有个setting.xml配置文件,先要把项目要的setting.xml覆盖这个原来的配置文件. 这个maven配置文件有一个作 ...
- 215 Kth Largest Element in an Array 数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假 ...
- Jmeter+Jenkins+Ant自动化集成环境搭建
搭建环境: JDK:jdk1.8.0_92 Ant:apache-ant-1.9.7 Jmeter: apache-jmeter-3.0 Jenkins:jenkins-2.19.3 具体环境配置 1 ...
- C# 判断是否移动设备
/// <summary> /// 判断是否移动设备. /// </summary> /// <returns></returns> public st ...
- [SPOJ1812]Longest Common Substring II 后缀自动机 多个串的最长公共子串
题目链接:http://www.spoj.com/problems/LCS2/ 其实两个串的LCS会了,多个串的LCS也就差不多了. 我们先用一个串建立后缀自动机,然后其它的串在上面跑.跑的时候算出每 ...
- ViewPager讲解以及ViewFlipper
1.加入ViewPager最好导入<android.support.v4.view.ViewPager>兼容低版本 2.将布局转换为View的方法 3.适配器类型 PagerAdapter ...
- php判断是否引入某文件
Code: /* 判断是否引入了公共文件demo.php */ $include_files = get_included_files(); $include_files_exist = 0 ; fo ...