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

时间:2021-12-26 14:58:41
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(1≤N≤100000)N(1≤N≤100000), indicating the number of trees nodes.

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

The following one line contains one number AA(A≤100000A≤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(B≤100000B≤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=;
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]=; int tmp=;
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=;
feach(i,u) {
if(Gv==f) continue;
if(!dfs(Gv,u)) return false;
tmp+=Mx[Gv];
}
Mx[u]=min(Mx[u],tmp+);
if(Mx[u]<Mn[u]) return false;
return true;
}
bool check(int Mid)
{
rep(i,,N) Mx[i]=sz[i];
rep(i,,B) Mx[x[i]]=min(Mx[x[i]],Mid-y[i]);
if(dfs(,)&&Mx[]>=Mid) return true;
return false;
}
int main()
{
int T,u,v,w,e;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
rep(i,,N) G[i].clear(),Mn[i]=;
rep(i,,N-) {
scanf("%d%d",&u,&v);
G[u].pb(v); G[v].pb(u);
}
scanf("%d",&A);
rep(i,,A){
scanf("%d%d",&w,&e);
Mn[w]=max(Mn[w],e);
}
dfs1(,);
scanf("%d",&B);
rep(i,,B) scanf("%d%d",&x[i],&y[i]);
int L=Mn[],R=N,ans=-,Mid;
while(L<=R){
Mid=(L+R)/;
if(check(Mid)) ans=Mid,R=Mid-;
else L=Mid+;
}
printf("%d\n",ans);
}
return ;
}