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先取,他能 ...
随机推荐
- netty学习记录1
最近在学习netty,看的是<netty权威指南 第2版>. 然后看的同时也把书上面的代码一行行敲下来做练习,不过到第三章就出问题了. 按照书上讲的,sever/client端都需要继承C ...
- wget 下载页面下所有文件
先介绍几个参数:-c 断点续传(备注:使用断点续传要求服务器支持断点续传),-r 递归下载(目录下的所有文件,包括子目录),-np 递归下载不搜索上层目录,-k 把绝对链接转为相对链接,这样下载之后的 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目8
2014-03-21 22:23 题目:假设你一开始有一个空数组,你在读入一些整数并将其插入到数组中,保证插入之后数组一直按升序排列.在读入的过程中,你还可以进行一种操作:查询某个值val是否存在于数 ...
- USACO Section1.5 Number Triangles 解题报告
numtri解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...
- leetcode 【Rotate List 】python 实现
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- SQL语句中的换行符
直接看示例(顺便注意下连接符) 示例1:Access INSERT INTO ZD_DJDCB (DJH,ZDSZ) VALUES ('150105101204JC00428', '北: 地块1' + ...
- day06_04 购物车讲解02
1.0 补充知识 a,b = [2,3] print(a) print(b) #>>>2 #>>>3 a,b = (2,3) print(a) print(b) # ...
- Windows添加自定义服务、批处理文件开机自启动方法
[Windows 添加自定义服务方法]: 1.使用Windows服务工具instsrv.exe与srvany.exe: 参考:https://wenku.baidu.com/view/44a6e6f8 ...
- [C++] 数据结构应用——链表
C++ 数据结构应用--链表 代码已经封装成class啦,方便使用. 头文件:Linklist.h #include <iostream> /*********************** ...
- HDU 4681 String 胡搞
设串C的第一个字母在串A中出现的位置是stA, 串C的最后一个字母在串A中出现的位置是edA. 设串C的第一个字母在串B中出现的位置是stB, 串C的最后一个字母在串B中出现的位置是edB. 求出每一 ...