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

Problem Description
Given
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.
 
Input
There are several cases, first is the number of cases T. (There are most twenty cases).
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.
 
Output
For each case output the answer.
 
Sample Input
2
3 3
1 2
2 3
5 7
1 2
1 3
2 4
2 5
 
Sample Output
4
10
 
Source
 
题意:求出一棵树里面每两个点之间距离的前K大之和。
题解:

把所有边 (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(巧妙的搜索)的更多相关文章

  1. hdu 5102 树上前k短路径长度和

    http://acm.hdu.edu.cn/showproblem.php?pid=5102 给一棵树,求出所有节点的距离中前k小的路径长度和 由于路径长度的定义为两点之间的边的个数,所有遍历1~n- ...

  2. HDU 4616 Game (搜索)、(树形dp)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4616 这道题目数据可能比较弱,搜索都可以AC,但是不敢写,哎…… 搜索AC代码: #include & ...

  3. [HDU 2102] A计划(搜索题,典型dfs or bfs)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu 4722(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...

  5. hdu 5335 Walk Out (搜索)

    题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...

  6. HDU 5102 The K-th Distance(模拟)

    题意:输入一棵树,输出前k小的点对最短距离dis(i,j)的和. 模拟,官方题解说得很清楚了.不重复了. http://bestcoder.hdu.edu.cn/ 需要注意的是,复杂度要O(n+k), ...

  7. HDU 1896 Stones --优先队列+搜索

    一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...

  8. HDU 1978 记忆化搜索(dfs+dp)

    Y - How many ways Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4597 记忆化搜索

    ² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...

随机推荐

  1. iOS 中 AFNetworking HTTPS 的使用

    由于我们公司由HTTP转HTTPS,出现了一系列问题特此记录下. 一.HTTPS 二.App Transport Security 三.iOS 中用HTTPS 注意的问题 四.使用 AFNetwork ...

  2. 《算法》C++代码 Floyd

    今天写写最短路径的Floyd算法(有翻译叫弗洛伊德,不过这奇葩翻译用来读读就好……). 这个算法的实质,广义来讲,其实是DP(动态规划).其实按说,算法应该先说说什么贪心.搜索.DP.二分之类的基本算 ...

  3. loadrunner检查点设置失败,日志中SaveCount无法被正常统计出来

    在脚本正确的情况下的web_reg_find检查点检查失败,SaveCount无法被正常统计出来. 在检查项Text为中文的情况下, ******(我是被录制下来的代码) web_reg_find(& ...

  4. HTML5标签学习笔记

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equiv="con ...

  5. NTP学习

    NTP(The Network Time Protocol),本以为是一个非常简单的协议,但是看了百度百科和ntp.org的介绍后,我发现我错了. 这个看似简单的协议存在一个很关键也是非常重要的问题- ...

  6. 【志银】Ubuntu Apache2配置SSL证书

    1.准备工作 证书文件:zain.crt.zain.key /etc/apache2/文件夹下新建ssl 文件夹,将证书文件放入/etc/apache2/ssl 2.配置SSL证书 打开/etc/ap ...

  7. CodeBlocks X64 SVN 编译版

    CodeBlocks X64 SVN 编译版 采用官方最新的SVN源码编译而来,纯64位的,所以32位系统是不能使用的.字体使用的是微软的YaHei UI字体,如果有更好的字节建议,可以留言. 由于直 ...

  8. 云效(阿里云)流水线 + nginx + uWsgi + flask + python3 基础环境搭建 --备忘

    一.开发环境搭建 1.安装python3 yum -y groupinstall "Development tools" yum -y install zlib-devel bzi ...

  9. js万年历

    首先,注意: 1.延迟执行     window.setTimeout(    ,     )     里面的时间是以毫秒计算的 2.间隔执行    window.setInterval(     , ...

  10. Python洗牌算法重写

    Python有自带的洗牌算法函数shuffle(). 自己也通过学习也琢磨了一下它的实现,然后给出一个时间复杂度O(n),空间复杂度O(4)的例子: import random def shuffle ...