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 ;
}

HDU - 6241 :Color a Tree(不错的二分)的更多相关文章

  1. hdu 6241 Color a Tree 2017 CCPC 哈理工站 L

    Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...

  2. hdu 4603 Color the Tree

    这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...

  3. HDU 1055 - Color a Tree

    一棵树,结点树为n,根结点为r.每个结点都有一个权值ci,开始时间为0,每染色一个结点需要耗时1,每个结点的染色代价为ci*ti(ti为当前的时间),每个结点只有在父结点已经被染色的条件下才能被染色. ...

  4. Color a Tree HDU - 6241

    /* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...

  5. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  6. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  7. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  9. Color a Tree[HDU1055]

    Color a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  10. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. $用ConfigParser模块读写conf配置文件

    ConfigParser是Python内置的一个读取配置文件的模块,用它来读取和修改配置文件非常方便,本文介绍一下它的基本用法. 数据准备 假设当前目录下有一个名为sys.conf的配置文件,其内容如 ...

  2. Spark机器学习7·降维模型(scala&python)

    PCA(主成分分析法,Principal Components Analysis) SVD(奇异值分解法,Singular Value Decomposition) http://vis-www.cs ...

  3. [国家集训队] Crash 的文明世界(第二类斯特林数)

    题目 [国家集训队] Crash 的文明世界 前置 斯特林数\(\Longrightarrow\)斯特林数及反演总结 做法 \[\begin{aligned} ans_x&=\sum\limi ...

  4. 【BZOJ4671】(斯特林反演)

    题目 [BZOJ4671]异或图 很有意思的题 做法 直接处理显然很难,我们考虑范围扩大以求容斥或反演这类的帮助 \(f_i\)表示至少有\(i\)个联通块的方案,形如设立\(i\)个联通块轮廓,联通 ...

  5. 20145201《Java程序设计》第九周学习总结

    20145201 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层 ...

  6. 如何自定义echarts 线性图的选择事件

    最近在做公司的数据大盘,要用到图表显示,echarts研二的时候有用过,我就决定用它了. 这里用到一个可以同时显示多条曲线的line-charts,基本样子如下: 看到这个画红色圈圈的地方了吗??? ...

  7. java用servlet、cookie实现一个阅读记录

    效果如图 代码1 package com.xiaostudy.servlet; import java.io.IOException; import java.io.PrintWriter; impo ...

  8. MQ 个人小结

    在PCS项目: talking 发送队列1.1 创建@Beanpublic Queue orderTakingQueue() { return createQueue(orderTakingQueue ...

  9. U盘安装XP

    ZC: 第1阶段结束 进入第2阶段 的时候,还是需要手动选择USB启动 (如果没有修改BIOS为USB启动的话) ZC: 我用的 WinSetupFromUSB,是文章尾部的那个版本的软件界面. ZC ...

  10. ie if判断

    <p> </p> <!--[if lt IE 7]> <html lang="en" ng-app="myApp" c ...