zoukankan      html  css  js  c++  java
  • HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树

    zxa and leaf

    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++.
     

    题意:

      http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=696&pid=1003

    题解:

       二分答案LL,检查是否存在答案不超过LL的解,也即对于任意一条边上的两个点uu和vv,有|level_u-level_v|leq Llevelu​​levelv​​L,如果能维护出每个点可能的取值区间就可以判断是否有解了。

    对于n>1n>1的情况,随便找点做根,做树形dp即可,总的时间复杂度为O(nlogmax(w))O(nlogmax(w))。

      需要注意的就是当n==2的时候,这个根要找好

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include<vector>
    #include<map>
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    const int N = 50000+20, M = 1e6+10, mod = 1e9+7,inf = 1e9;
    typedef long long ll;
    
    int n,m,root,f,H[N],mi[N],mx[N],d[N];
    vector<int >G[N];
    void dfs(int u,int fa,int k) {
        mi[u] = -inf;
        mx[u] = inf;
        if(H[u]) {
            mi[u] = H[u];
            mx[u] = H[u];
        }
        for(int i=0;i<G[u].size();i++) {
            int to = G[u][i];
            if(to==fa) continue;
            dfs(to,u,k);
            if(mi[to]==-inf) continue;
            mi[to] -= k;
            mx[to] += k;
            mi[u] = max(mi[u],mi[to]);
            mx[u] = min(mx[u],mx[to]);
        }
        if(mi[u]==-inf) {
           return ;
        }
        if(mi[u]>mx[u]) {
            f = 0;
            return ;
        }
    }
    bool check(int k) {
         f = 1;
         dfs(root,-1,k);
         if(f) return true;
         else return false;
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            scanf("%d%d",&n,&m);
            for(int i=1;i<=n;i++) G[i].clear(),H[i] = 0,d[i] = 0;
            for(int i=1;i<n;i++) {
                int u,v;
                scanf("%d%d",&u,&v);
                G[u].push_back(v);
                G[v].push_back(u);d[u]++,d[v]++;
            }
            if(n==2) root = 1;
            else
            for(int i=1;i<=n;i++) {
                if(d[i]!=1) {
                    root = i;
                    break;
                }
            }
            for(int i=1;i<=m;i++) {
                int x,y;
                scanf("%d%d",&x,&y);
                H[x] = y;
            }
            int l = 0,r = 1e9,ans=1e9;
            while(l<=r) {
                int mid = (l+r)>>1;
                if(check(mid)) r=mid-1,ans = mid;
                else l = mid+1;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Antd下拉多选带勾选框
    POJ
    HDU 4281(01 背包+ 多旅行商问题)
    Codeforces Round #460 (Div. 2) D. Substring
    HDU
    POJ 2184 Cow Exhibition
    Codechef FRBSUM 解题报告
    UVA11982题解
    Suffix Array 后缀数组算法心得
    51nod1158 单调栈 个人的想法以及分析
  • 原文地址:https://www.cnblogs.com/zxhl/p/5496478.html
Copyright © 2011-2022 走看看