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. hadoop19---动态代理

    Action调用service里面的方法,动态代理:改变方法的实现在方法前后加逻辑不是加新方法. 在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于Io ...

  2. resin服务一直不停重启

    resin服务不断重启. 原因为resin配置文件使用域名.需要到服务上绑定一下域名.

  3. 部署 LAMP (CentOS 7.2),摘自阿里云,方便查看使用

    原文地址:https://help.aliyun.com/document_detail/50774.html?spm=5176.product25365.6.728.C9s3V8 简介 LAMP指L ...

  4. [转]React Native 语言基础之ES6

    React Native 是基于 React 这个前端框架来构建native app的架构.React Native基于ES6(即ECMAScript2015)语言进行开发的. JS的组成 1) 核心 ...

  5. NaviCat Primium远程连接Oracle 11g数据库操作方法

    说明:作者使用的系统是windows 10 64位系统,使用的软件及插件都是基于64位系统进行安装.一. 软件准备 1.windows 10 64位 操作系统 2.NaviCat Primium 11 ...

  6. sublime使用sublimelint-luacheck屏蔽指定警告

    在成功安装SublimeLinter-lua与luacheck以后,如果没有语法error,则会进行警告提示. 如下图 waring: line contains trailing whitespac ...

  7. SpringBoot和Mycat动态数据源项目整合

    SpringBoot项目整合动态数据源(读写分离) 1.配置多个数据源,根据业务需求访问不同的数据,指定对应的策略:增加,删除,修改操作访问对应数据,查询访问对应数据,不同数据库做好的数据一致性的处理 ...

  8. Shell awk 求标准差

    cat > temp000180255798957892187719 awk '{x[NR]=$0; s+=$0; n++} END{a=s/n; for (i in x){ss += (x[i ...

  9. eclipse显示结果窗口字体大小

    设置前的字体大小 设置后的字体大小 步骤

  10. Android并发编程之白话文详解Future,FutureTask和Callable

    从最简单的说起Thread和Runnable 说到并发编程,就一定是多个线程并发执行任务.那么并发编程的基础是什么呢?没错那就是Thread了.一个Thread可以执行一个Runnable类型的对象. ...