HDU - 6241 :Color a Tree(不错的二分)

Posted hua-dong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 6241 :Color a Tree(不错的二分)相关的知识,希望对你有一定的参考价值。

Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are numbered 1,2,...,N1,2,...,N. The root of the tree is node 11. The initial color of each node is white. Bob can use one unit energy to color one node into black. To prevent Bob being lazy with painting, Alice proposed A+BA+B rules. Each rule is represent by two numbers xixi and yiyi. 
For the first AA rules, it means that there should be no less than yiyi nodes painted black for the subtree of node xixi. 
For the other BB rules, it means that there should be no less than yiyi nodes painted black for all nodes except the subtree of node xixi. 
You need to help Bob to calculate the minimum energy he needs for the painting with all rules proposed by Alice satisfied. 

InputThe first line is the number of test cases. For each test case, the first line contains one positive number N(1N100000)N(1≤N≤100000), indicating the number of trees nodes. 

The following N1N−1 lines describe the edges. Each line contains two integers u,vu,v(1u,vN1≤u,v≤N), denoting there is a edge between node uu and node vv. 

The following one line contains one number AA(A100000A≤100000), indicating the first AArules. 

The following AA lines describe the first AA rules. Each line contains two numbers xixiand yiyi as described above. 

The following one line contains one number BB(B100000B≤100000), indicating the other BBrules. 

The following BB lines describe the other BB rules. Each line contains two numbers xixiand yiyi as described above. 
OutputFor each test case, output a integer donating the minimum energy Bob needs to use with all rules propose by Alice satisfied. If there is no solution, output 1−1instead. 
Sample Input

2 
5
1 2
2 3
3 4
1 5
2
2 1
5 1
1
2 1
5
1 2
2 3
3 4
1 5
3
1 2
2 2
5 1
1
3 5

Sample Output

2
-1

题意:给定大小为N的树,限制点都是白色,让你染色,求最小染色数,有A+B个的限制,A限制表示X子树至少有Y个点被染色。B限制表示X子树之外的那些点,至少有Y个点被染色。

思路:很难想到二分答案。根据A条件我们可以得到每个子树至少有多少个点染色;二分之后,根据B条件,我们可以得到子数最多有多少个染色点,然后看每个点是否有矛盾,如果有矛盾,或者整棵树不够染色,输出-1。是否二分成立。

#include<bits/stdc++.h>
#define pb push_back
#define feach(i,u) for(int i=0,L=G[u].size();i<L;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Gv G[u][i]
using namespace std;
const int maxn=100010;
vector<int>G[maxn];
int A,B,x[maxn],y[maxn];
int Mn[maxn],Mx[maxn],sz[maxn],N;
bool dfs1(int u,int f)
{
    sz[u]=1; int tmp=0;
    feach(i,u) {
        if(Gv==f) continue;
        dfs1(Gv,u);
        sz[u]+=sz[Gv];
        tmp+=Mn[Gv];
    }
    Mn[u]=max(Mn[u],tmp);
}
bool dfs(int u,int f)
{
    int tmp=0;
    feach(i,u) {
        if(Gv==f) continue;
        if(!dfs(Gv,u)) return false;
        tmp+=Mx[Gv];
    }
    Mx[u]=min(Mx[u],tmp+1);
    if(Mx[u]<Mn[u]) return false;
    return true;
}
bool check(int Mid)
{
    rep(i,1,N) Mx[i]=sz[i];
    rep(i,1,B) Mx[x[i]]=min(Mx[x[i]],Mid-y[i]);
    if(dfs(1,0)&&Mx[1]>=Mid) return true;
    return false;
}
int main()
{
    int T,u,v,w,e;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        rep(i,1,N) G[i].clear(),Mn[i]=0;
        rep(i,1,N-1) {
            scanf("%d%d",&u,&v);
            G[u].pb(v); G[v].pb(u);
        }
        scanf("%d",&A);
        rep(i,1,A){
            scanf("%d%d",&w,&e);
            Mn[w]=max(Mn[w],e);
        }
        dfs1(1,0);
        scanf("%d",&B);
        rep(i,1,B) scanf("%d%d",&x[i],&y[i]);
        int L=Mn[1],R=N,ans=-1,Mid;
        while(L<=R){
            Mid=(L+R)/2;
            if(check(Mid)) ans=Mid,R=Mid-1;
            else L=Mid+1;
        }
        printf("%d
",ans);
    }
    return 0;
}

 

以上是关于HDU - 6241 :Color a Tree(不错的二分)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 6241

HDU 1055 Color a Tree

HDU1055Color a Tree

hdu 4603 Color the Tree

hdu1556 Color the ball

hdu1556-Color the ball