HDU - 6241 :Color a Tree(不错的二分)
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(不错的二分)的更多相关文章
- 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 ...
- hdu 4603 Color the Tree
这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...
- HDU 1055 - Color a Tree
一棵树,结点树为n,根结点为r.每个结点都有一个权值ci,开始时间为0,每染色一个结点需要耗时1,每个结点的染色代价为ci*ti(ti为当前的时间),每个结点只有在父结点已经被染色的条件下才能被染色. ...
- Color a Tree HDU - 6241
/* 十分巧妙的二分 题意选最少的点涂色 使得满足输入信息: 1 x的子树涂色数不少于y 2 x的子树外面涂色数不少于y 我们若是把2转化到子树内最多涂色多少 就可以维护这个最小和最大 如果我们二分出 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- POJ 2054 Color a Tree
贪心.... Color a Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Color a Tree[HDU1055]
Color a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- 【转】Linux 下取进程占用 cpu/内存 最高的前10个进程
# Linux 下 取进程占用 cpu 最高的前10个进程ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head # linux 下 取进程占用内存 ...
- 对 Java Integer.valueOf() 的一些了解
从一道选择题开始 分析 选项A 选项A中比较的是i01和i02,Integer i01=59这里涉及到自动装箱过程,59是整型常量,经包装使其产生一个引用并存在栈中指向这个整型常量所占的内存,这时 ...
- Ubuntu 16.04 安装Django
> pip install django==1.10.3......或者:> pip3 install django==1.10.3(我采用)......或者:>python3 -m ...
- eclipse中自动加载源码的方法
1.选中项目右键properties--java build path--Libraries--Add External class Folder 找到项目将项目添加进去 2.然后就是这样 3.OK
- CSS 边距和填充
margin and padding are the two most commonly used properties for spacing-out elements. A margin is t ...
- sublime批量替换文本重复单词
事先需要把单词打到文本的每一行 排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[ ...
- 并查集 试水 hdu1232
#include <stdio.h> #include <stdlib.h> int n,m; ],rank[]; int count; int find(int x) { i ...
- JUnit4 入门笔记
Test注解的两个可选参数 expected timeout The Test annotation supports two optional parameters. The first, expe ...
- 【P3254】圆桌问题(最大流,洛谷)
看到题目,产生第一反应,是否可以匹配的是这么多.那么连边跑一遍最大流就行了. 从源点向每个单位连一条长度为l的边,然后所有单位和餐桌分别连边,流量为1,所有餐桌向汇点连边,流量为餐桌容量.然后跑一遍最 ...
- vs2013 浏览器 browserlink 不停访问