首页 > 其他分享 >hdu 5102(队列+节点扩展)

hdu 5102(队列+节点扩展)

时间:2023-05-29 18:33:03浏览次数:33  
标签:node tmp hdu 队列 head tot int 5102 include


The K-th Distance


Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)




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)


 



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



用一个队列维护即可,先将所有边入队,然后不停的用边的一个端点向外扩张,每扩张一次,路径长度就+1


由于1->2和2->1会重复算,所以K要扩大一倍,那么最后的结果肯定会多算一倍,除以2即可。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 100007

struct node
{
    int u,v,d;
    node(int _u,int _v,int _d):u(_u),v(_v),d(_d){}
    node(){}
};
struct Edge{
    int v,next;
}G[2*N];
int ans,k,tot,head[N];
queue<node> q;

void addedge(int u,int v)
{
    G[tot].v = v;
    G[tot].next = head[u];
    head[u] = tot++;
}

void bfs()
{
    int cnt = 0;
    while(!q.empty())
    {
        node tmp = q.front();
        q.pop();
        int u = tmp.u, v = tmp.v, d = tmp.d;
        if(cnt >= k) break;
        for(int i = head[u]; i != -1; i = G[i].next)
        {
            int vv = G[i].v;
            if(vv != v)
            {
                ans += d+1;
                cnt++;
                q.push(node(vv,u,d+1));
            }
            if(cnt >= k) break;
        }
    }
}

int main()
{
    int t,n,u,v,i;
    scanf("%d",&t);
    while(t--)
    {
        while(!q.empty()) q.pop();
        scanf("%d%d",&n,&k);
        tot = 0;
        memset(head,-1,sizeof(head));
        for(i = 1; i <= n; i++) q.push(node(i,i,0));
        for(i = 1; i < n; i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        k *= 2;
        ans = 0;
        bfs();
        cout<<ans/2<<endl;
    }
    return 0;
}



标签:node,tmp,hdu,队列,head,tot,int,5102,include
From: https://blog.51cto.com/u_16143128/6373499

相关文章

  • hdu 5367(线段树+区间合并)
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5367官方题解:对于求“高山脉”长度,可以利用线段树。树节点中保存左高度连续长度,左高度连续区间的高度,左高度连续区间的状态(即是否高于第一个高度不同的山),右高度连续长度,右高度连续区间的高度,右高度连续区间的状态,每段区间内的“......
  • hdu 5201(隔板法+容斥原理)
    TheMonkeyKingTimeLimit:8000/4000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)ProblemDescriptionnpeaches.Andtherearemmonkeys(includingGoKu),theyarenumberedfrom1tom,GoKu’snumberis1.GoKuwa......
  • hdu 5086(dp)
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5086题目大意:给出长度为n的数组,然后要求累计里面的每个子串的和。解题思路:这道题直接枚举肯定不行,所以要找递推关系。假设:{1,2,3,4}为某一个序列,假设我们已经找到了{1,2,3},接下来把{4}加入进去;由于{1,2,3}已经有{1},{2},{3},{1,......
  • hdu 5188(限制01背包)
    zhxandcontestTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)ProblemDescriptionAsoneofthemostpowerfulbrushesintheworld,zhxusuallytakespartinallkindsofcontests.Oneday,zhxtakespar......
  • hdu 5171(矩阵快速幂)
    GTY'sbirthdaygiftTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)ProblemDescriptiona,b∈S),andadda+b Input2≤n≤100000,1≤k≤1000000000).Thesecondlinecontainsnelementsai......
  • hdu 5157(manacher+前缀和+树状数组)
    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5157解题思路:我们可以先用mancher算法对字符串进行处理,把以每个点为中心的回文串半径求出来,然后进行处理。加入对以p为中心的点,从p-r[i]+1~p都是回文串的开头,那么对于每个回文串(开头是j)只要记录结尾从1~j-1的回文串个数,我们可......
  • 关于消息队列的一些思考
    日志与消费队列消息队列的应用价值数据集成于系统解耦异步处理与事件驱动流量削峰事务消息与分布式事务的最终一致从历史看消息队列的价值演化思考手上的工作,找到他的价值和定位,将价值最大化1.日志和消息队列推荐文章:TheLog:Whateverysoftwareengineershou......
  • Disruptor内存消息队列简单使用
    Disruptor内存消息队列最近在做一个有关使用内存消息队列到功能,比如将日志信息或点击统计信息持久化等操作,开始想着用java到内存队列作为缓冲区,后来在网上搜到Disruptor这个东西,神乎其神到,就简单了解了一下,做了一个demo,感觉还不错,可以用用,有关概念可以自行搜索,下面就简单介绍一下开......
  • 数据结构之队列
    @TOC前言本文章讲述的是数据结构的特殊线性表——队列一.什么是队列,队列的特点队列是数据结构中的一种特殊的线性表,它与栈不同,队列的基本图例如上图:显然,队列的特点就是:先进先出FirstInFirstOut那么我们使用什么样的方式来实现队列呢?基于队列的特点,使用链表而不是数组来实现队......
  • 代码随想录Day11|栈和队列
    20.有效的括号经典的利用栈的题目这里选择用java来写,注意我们的java中的泛型不能用基本数据类型,而是应该使用包装类注意!java一定是定义后需要声明,然后才能使用1047.删除字符串中的所有相邻重复项 略比较简单150.逆波兰表达式求值注意:leetcode内置jdk的问题,不能使......