hdu 5102(巧妙的搜索)
The K-th Distance
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 752 Accepted Submission(s): 216
a tree, which has n node in total. Define the distance between two node
u and v is the number of edge on their unique route. So we can have
n(n-1)/2 numbers for all the distance, then sort the numbers in
ascending order. The task is to output the sum of the first K numbers.
For each case, the first line contain two integer n and K (2≤n≤100000,0≤K≤min(n(n−1)/2,106) ). In following there are n-1 lines. Each line has two integer u , v. indicate that there is an edge between node u and v.
3 3
1 2
2 3
5 7
1 2
1 3
2 4
2 5
10
把所有边 (u,v) 以及(v,u)放入一个队列,队列每弹出一个元素(u,v),对于所有与u相邻的点w,如果w!=v,就把(w,u)入队。这样就能一个一个生成前K小的 距离。 注意到每条边实际上会入队两次,只要把K翻倍且把ans除2即可,时间复杂度为O(n+K);
这种搜索方式还是第一次看到。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <queue>
using namespace std;
const int N = ;
int n,k,ans; struct node{
int u,v,w;
node(){};
node(int _u,int _v,int _w):u(_u),v(_v),w(_w){};
};
queue<node> q;
struct Edge{
int v,next;
}edge[*N];
int head[N];
int tot;
void addedge(int u,int v,int &k){
edge[k].v = v,edge[k].next = head[u],head[u] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot= ;
}
void bfs(){
int cnt = ;
while(!q.empty()){
node temp = q.front();
q.pop();
int u = temp.u,v=temp.v,w = temp.w;
if(cnt>=k) break;
for(int i=head[u];i!=-;i=edge[i].next){
int _v = edge[i].v;
if(_v!=v){
ans +=w+;
cnt++;
q.push(node(_v,u,w+));
}
if(cnt>=k) break;
}
if(cnt>=k) break;
}
}
int main(){
int tcase;
scanf("%d",&tcase);
while(tcase--){
while(!q.empty()) q.pop();
init();
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) q.push(node(i,,));
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v,tot);
addedge(v,u,tot);
}
ans = ;
k = *k;
bfs();
printf("%d\n",ans/);
}
return ;
}
hdu 5102(巧妙的搜索)的更多相关文章
- hdu 5102 树上前k短路径长度和
http://acm.hdu.edu.cn/showproblem.php?pid=5102 给一棵树,求出所有节点的距离中前k小的路径长度和 由于路径长度的定义为两点之间的边的个数,所有遍历1~n- ...
- HDU 4616 Game (搜索)、(树形dp)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4616 这道题目数据可能比较弱,搜索都可以AC,但是不敢写,哎…… 搜索AC代码: #include & ...
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 4722(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- HDU 5102 The K-th Distance(模拟)
题意:输入一棵树,输出前k小的点对最短距离dis(i,j)的和. 模拟,官方题解说得很清楚了.不重复了. http://bestcoder.hdu.edu.cn/ 需要注意的是,复杂度要O(n+k), ...
- HDU 1896 Stones --优先队列+搜索
一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...
- HDU 1978 记忆化搜索(dfs+dp)
Y - How many ways Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4597 记忆化搜索
² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...
随机推荐
- 使用Oracle绿色客户端(InstantClient)连接远程Oracle的配置方法
非常简单的配置,网上一搜,有很多,但是还是想记录下来,说不定以后需要了,直接进自己的博客看看也好啊. 下载了PLSQL Developer 11,安装好了发现不能连接远程数据库,但是又不想安装orac ...
- laravel5.5契约
无规矩不成方圆, Laravel 的契约是一组定义框架提供的核心服务的接口,规定了实现该接口的规范. 为什么要使用接口 首先,让我们来看一些高耦合缓存实现的代码.如下: <?php namesp ...
- 忙着Windows 10迁移?不要忘记Windows 7的安全!
许多企业已经开始准备向Windows 10迁移了,对于IT管理员来说,Windows 7的安全性不容忽视. 自微软正式停止Windows 7主流支持至今刚刚超过一年,并且其扩展支持将于2010年结束. ...
- MySQL Group Replication数据安全性保障
本文来自数据库内核专栏 在之前的文章中,介绍了MGR对数据可靠性.可用性和一致性的实现方案.简单来说,MGR通过基于paxos协议的多副本来实现数据的可靠性,通过多副本上的majority机制来实现可 ...
- 《Cracking the Coding Interview》——第2章:链表——题目4
2014-03-18 02:27 题目:将一个单链表按照一个值X分为两部分,小于X的部分放在大于等于X的部分之前. 解法:按照值和X的大小,分链表为两条链表,然后连起来成一条. 代码: // 2.4 ...
- SQL Server无法连接到数据库
连接数据库的时候出现如下错误: 我解决的使用方法: 第一步:关闭上面的错误,取消连接数据库. 第二步:开始->程序->Microsoft SQL Server 2008 R2->配置 ...
- JMeter学习笔记(九) 参数化1--函数助手:_CSVRead
1.函数助手:_CSVRead 1)准备数据文件 ,文件可以是.csv格式,.dat格式,txt格式等 2)打开函数助手,生成参数 3)添加HTTP请求,引用参数 4)执行HTTP请求,察看结果树中的 ...
- python小结
c:\python33添加到你的PATH 环境变量中,你可以在DOS 窗口中 输入以下命令:set path=%path%;C:\python33 id() 方法的返回值就是对象的内存地址. 在#! ...
- NSIS编译报错:您可能有有一个或两个(大)的旧临时文件
一.有时在编译NSIS时会出现如下错误: 注意: 您可能有有一个或两个(大)的旧临时文件 残留在临时目录文件夹中 (通常这种情况只会发生在 Windows 9x 系统中). 二.本人遇到的问题原因: ...
- hadoop +streaming 排序总结
参考http://blog.csdn.net/baidu_zhongce/article/details/49210787 hadoop用于对key的排序和分桶的设置选项比较多,在公司中主要以KeyF ...