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 ...
随机推荐
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- php 遇到报错 Call to a member function fetch_object()
1.检查语法 ,没问题 <?php require "fun.php"; $kc_sql="select distinct KCM from KCB"; ...
- javaservlet介绍
servlet 是 serve applet的意思 Java servlet是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. Servlet运行于支持Jav ...
- D. Chloe and pleasant prizes 树上dp + dfs
http://codeforces.com/contest/743/problem/D 如果我们知道mx[1]表示以1为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...
- sdut1650I-Keyboard(dp)
链接 题目大意就相当于 跟你一串字符串 让你截成k段 使总体的值最小 想法是递归的 递归太慢 可以转换为递推的 这样就有可以推出状态方程 dp[i][j] = max(dp[i][j],dp[i-1] ...
- 阿里maven仓库地址
在国内访问Maven仓库,连接速度太慢.下面是将中央仓库替换成阿里云的中央仓库的方法. 第一种,统一修改仓库地址 可以直接修改Mavenconf文件夹中的setting.xml文件,或者在.m2文件夹 ...
- jQuery相关知识总结
1 encodeURIComponent(city)处理js传值乱码问题 2 总体概述 以后项目如果没有特殊情况,一般采用jQuery作为最基础的公共底层库. 另外对于前端的javascript相关的 ...
- Android学习笔记(十四) Handler理论补充
一.如何下载Android源码 在SDK Manager中选中Sources for Android SDK. 二.ThreadLocal初步介绍 1)执行ThreadLocal对象(static f ...
- H.264学习笔记1——相关概念
此处记录学习AVC过程中的一些基本概念,不定时更新. frame:帧,相当于一幅图像,包含一个亮度矩阵和两个色度矩阵. field:场,一帧图像,通过隔行扫描得到奇偶两场,分别称为顶场和底场或奇场和偶 ...
- SQL转Java代码小工具
工作中使用SQL的时候很多,当使用hibernate的时候,经常遇到多行的SQL,通常在PL/SQL或其他地方写好SQL,测试没问题后,需要将SQL写到程序代码中,多行SQL需要拼接字符串,手动一行行 ...