hdu 5682 zxa and leaf

zxa and leaf

Accepts: 25

Submissions: 249

Time Limit: 5000/2500 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

问题描述

zxa有一棵含有nn个节点的无根树,包含(n-1)(n−1)条无向边,点从11到nn编号,定义每个点的度数为与这个点相连的边的数量,度数为11的节点被称作这棵树的叶子节点。

zxa想给每个节点设置它的好看度,好看度必须为正整数。他的无根树有m(1\leq m\leq n)m(1≤m≤n)个叶子节点,其中的k(1\leq k\leq m)k(1≤k≤m)个叶子节点的好看度已经确定,zxa只需要设置其他节点的好看度。

zxa很好奇,如果令每条边的难看度是这条边所连接的两个节点的好看度差值的绝对值,整棵树的难看度是所有边的难看度中的最大值,那么这棵树的难看度最小是多少,你能帮助他吗?

输入描述

第一行有一个正整数TT,表示有TT组数据。

对于每组数据:

第一行有两个正整数nn和kk,表示这棵树有nn个节点,其中kk个叶子节点的好看度已经确定。

接下来(n-1)(n−1)行,每行有两个互异的正整数uu和vv,表示节点uu和节点vv之间有一条无向边。

接下来kk行,每行有两个正整数uu和ww,表示节点uu是叶子节点,而且它的好看度是ww。

每一行相邻数字之间只有一个空格。

保证输入的边构成一棵树。

1\leq T\leq 10,2\leq n\leq 5\cdot10^4,1\leq k\leq n,1\leq u,v\leq n,1\leq w\leq 10^91≤T≤10,2≤n≤5⋅10?4??,1≤k≤n,1≤u,v≤n,1≤w≤10?9??

输出描述

对于每组数据,输出一行,包含一个非负整数,表示这棵树的难看度最小值。

输入样例

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

输出样例

3
1

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <cstring>
using namespace std;
const int Max=5e4+10;
typedef long long LL;
vector<int>book[Max];
const int inf=1e9+10;
int vis1[Max],a[Max],vis2[Max];
int pnt[Max*2],head[Max],nxt[Max*2],e; //如果边数写错给小了,报超时什么鬼。。。
inline void addedge(int u,int v)
{
    pnt[e]=v;nxt[e]=head[u];head[u]=e++;
}
int maxx[Max],minx[Max],limit;
bool dfs(int u)
{
    int v;
    maxx[u]=inf;minx[u]=0;  //每个虚节点节点范围设为无限大
    if(vis1[u]) maxx[u]=minx[u]=a[u];
    for(int i=head[u];i!=-1;i=nxt[i])
    {
        v=pnt[i];
        if(!vis2[v])
        {
           vis2[v]=1;
           if(!dfs(v)) return 0;     //先dfs,使得所有的节点都有范围
           vis2[v]=0;
           maxx[u]=min(maxx[u],maxx[v]+limit);
           minx[u]=max(minx[u],minx[v]-limit); //每一个节点的范围需要满足相邻节点的要求
        }
    }
    return maxx[u]>=minx[u]; //每一个节点都必须满足条件
}
bool check(int len)
{
    limit=len;
    memset(vis2,0,sizeof(vis2));
    if(!dfs(1)) return 0;
    return 1;
}
int main()
{
    int T;
    for(scanf("%d",&T);T;T--)
    {
        memset(head,-1,sizeof(head));
        memset(nxt,-1,sizeof(nxt));
        memset(pnt,-1,sizeof(pnt));
        memset(vis1,0,sizeof(vis1));
        int n,k,u,v;
        scanf("%d%d",&n,&k);
        e=0;
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&u,&v);
            a[u]=v;
            vis1[u]=1;
        }
        int l=0,r=inf,ans,mid;  //二分求得答案
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(check(mid)) r=mid-1,ans=mid;
            else           l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-08-04 06:40:28

hdu 5682 zxa and leaf的相关文章

HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树

zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n−1) 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

zxa and leaf

zxa and leaf Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 502    Accepted Submission(s): 198 Problem Description zxa have an unrooted tree with n nodes, including (n−1) undirected edges, whos

hdu 5680 zxa and set(水题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5680 zxa and set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 411    Accepted Submission(s): 310 Problem Description zxa has a set A={a1,a2,?,a

DFS——hdu5682zxa and leaf

一.题目回顾 题目链接:zxa and leaf Problem Description zxa have an unrooted tree with n nodes, including (n?1) 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

HDU 4570

E - Multi-bit Trie Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4570 Description IP lookup is one of the key functions of routers for packets forwarding and classifying. Generally, IP lookup

HDU 4035:Maze 概率DP求期望(有环)

Maze 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4035 题意: 有N(2 ≤ N ≤ 10000)个房间和一堆双向边(不存在环),每个房间有ki和ei两个值,分别代表回到房间1和游戏结束的概率,求游戏结束时通过的边数的期望 题解: 一道很好很经典的求期望的题 设E[i]为以i为起点,直到游戏结束所通过边数的期望,则E[1]即所求答案 设fa代表父亲节点(由于不存在环,则图为一棵树,设1为根节点),∑ch代表所有孩子节点,size代表与这

HDU 4865 Peter&#39;s Hobby

Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 647    Accepted Submission(s): 273 Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf

HDU 4819:Mosaic(线段树套线段树)

http://acm.hdu.edu.cn/showproblem.php?pid=4819 题意:给出一个矩阵,然后q个询问,每个询问有a,b,c,代表(a,b)这个点上下左右c/2的矩形区域内的(最大值+最小值)/2是多少,并且将(a,b)的值替换成这个答案. 思路:很久以前被暴力跑过去的一道题,今天怎么交也过不去...果然是人品爆发了. 学了一下树套树,一开始觉得挺容易理解,但是后面PushUp那里挺难懂的(对我来说). 我的理解: 对于每个线段树的结点开一棵线段树,即tree[x][y]

hdu 1316 How many Fibs?(高精度斐波那契数)

//  大数继续 Problem Description Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1 + fn-2 (n >= 3) Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b]. Input The input contains several test cas