HDU 5682 zxa and leaf

zxa and leaf

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 341    Accepted Submission(s): 116


Problem Description
zxa have an unrooted tree with n nodes, including (n1) undirected edges, whose nodes are numbered from 1 to n . The degree of each node is defined as the number of the edges connected to it, and each node whose degree is 1 is defined as the leaf node of the tree.

zxa wanna set each node's beautiful level, which must be a positive integer. His unrooted tree has m(1mn) leaf nodes, k(1km) leaf nodes of which have already been setted their beautiful levels, so that zxa only needs to set the other nodes' beautiful levels.

zxa is interested to know, assuming that the ugly level of each edge is defined as the absolute difference of the beautiful levels between two nodes connected by this edge, and the ugly level of the tree is the maximum of the ugly levels of **all the edges on this tree**, then what is the minimum possible ugly level of the tree, can you help him?
 

Input
The first line contains an positive integer T , represents there are T test cases.

For each test case:

The first line contains two positive integers n and k , represent the tree has n nodes, k leaf nodes of which have already been setted their beautiful levels.

The next (n1) lines, each line contains two distinct positive integers u and v , repersent there is an undirected edge between node u and node v .

The next k lines, each lines contains two positive integers u and w , repersent node u is a leaf node, whose beautiful level is w .

There is a blank between each integer with no other extra space in one line.

It's guaranteed that the input edges constitute a tree.

1T10,2n5104,1kn,1u,vn,1w109
 

Output
For each test case, output in one line a non-negative integer, repersents the minimum possible ugly level of the tree.
 

Sample Input
  
  
2 3 2 1 2 1 3 2 4 3 9 6 2 1 2 1 3 1 4 2 5 2 6 3 6 5 9
 

Sample Output
  
  
3 1
Hint
If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5717  5716  5715  5714  5713 

题意就是告诉你某一个树,某一些点的权值已经确定,现在定义不同u,v并且相连的难看度是他们权值差的绝对值,问这个权值最小是什么多少。
首先二分答案,然后确定一个根,遍历整个树,去判断是否都满足答案。
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int M=5*1e4+1000;
int ind[M],val[M],idmax[M],idmin[M];
vector<int>v[M];
inline void getmax(int &a,int b)
{
    if(a<=b)
        a=b;
}
inline void getmin(int &a,int b)
{
    if(a>=b)
        a=b;
}
int dfs(int ans,int x,int fa)
{
    idmax[x]=1e9;
    idmin[x]=-1e9;
    if(val[x])
        idmax[x]=idmin[x]=val[x];
    for(int i=v[x].size()-1; i>=0; i--)
    {
        int kid=v[x][i];
        if(kid==fa)
            continue;
        if(!dfs(ans,kid,x))
            return 0;
        int min1=idmin[kid]-ans;
        int max1=idmax[kid]+ans;
        getmax(idmin[x],min1);
        getmin(idmax[x],max1);
    }
    return idmin[x]<=idmax[x];
}
bool check(int ans,int root)
{
    return dfs(ans,root,0);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,k;
        for(int i=1; i<=M; i++)
        {
            ind[i]=0;
            val[i]=0;
            v[i].clear();
        }
        cin>>n>>k;
        for(int i=1; i<=n-1; i++)
        {
            int x,y;
            cin>>x>>y;
            v[x].push_back(y);
            v[y].push_back(x);
            ind[x]++;
            ind[y]++;
        }
        for(int i=1; i<=k; i++)
        {
            int x,y;
            cin>>x>>y;
            val[x]=y;
        }
        int root;
        if(n==2)
            root=1;
        else
            for(int i=1; i<=n; i++)
                if(ind[i]!=1)
                {
                    root=i;
                    break;
                }
        int l=0,r=1e9;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(check(mid,root))
                r=mid;
            else
                l=mid+1;
        }
        cout<<l<<endl;
    }
    return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值