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,表示编号为x的节点的子树中要有不少于y个点被染色。

  后B条规则的输入也约定为一组x,y,表示编号为x的节点的子树之外要有不少于y个点被染色。

  求满足条件的最少染色数。无答案返回-1.

  约定输入的树的根节点序号为1.

  数据规模:节点数N≤100000,边数为N-1,A≤100000,B≤100000。

  

模拟赛的时候没想出来。

关键在于二分答案,把子树外不少于y个节点被染色转化为子树内最多多少个点被染色。

然后dfs维护节点的子树能染色点数的区间即可,最后判断是否出现冲突以及二分的答案mid在不在根节点的子树能染色点数的区间中即可。

O(nlogn)

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int MAXN=;
int pointer[MAXN];
int n,tot,A,B,now;
int low[MAXN],high[MAXN];
int RuleA[MAXN];
int RuleB[MAXN];
int cnt=;
struct Edge
{
int to,next;
Edge() {}
Edge(int b,int nxt) {to=b;next=nxt;}
}edge[*MAXN];
void init()
{
tot=;
memset(pointer,-,sizeof(pointer));
memset(RuleA,,sizeof(RuleA));
memset(RuleB,,sizeof(RuleB));
}
inline void addedge(int a,int b)
{
edge[tot]=Edge(b,pointer[a]);
pointer[a]=tot++;
edge[tot]=Edge(a,pointer[b]);
pointer[b]=tot++;
}
void Input()
{
scanf("%d",&n);
int u,v;
rep(i,,n-)
{
scanf("%d%d",&u,&v);
addedge(u,v);
}
scanf("%d",&A);
int x,y;
rep(i,,A)
{
scanf("%d%d",&x,&y);
RuleA[x]=max(y,RuleA[x]);
}
scanf("%d",&B);
rep(i,,B)
{
scanf("%d%d",&x,&y);
RuleB[x]=max(RuleB[x],y);
}
}
void dfs(int u,int pre)
{
int lowsum=,highsum=;
low[u]=RuleA[u];
high[u]=now-RuleB[u];
for(int j=pointer[u];j!=-;j=edge[j].next)
{
int v=edge[j].to;
if(v==pre) continue;
dfs(v,u);
lowsum+=low[v];
highsum+=high[v];
}
low[u]=max(low[u],lowsum);
high[u]=min(high[u],highsum+); }
bool check(int k)
{
now=k;
dfs(,);
if(RuleB[]>) return ;
for(int i=;i<=n;++i)
{
if(high[i]<low[i]) return ;
}
if(k<=high[]&&k>=low[]) return ;
else return ;
}
int main()
{
// freopen("in.txt","r",stdin);
int TT;scanf("%d",&TT);
rep(t1,,TT)
{
init();
Input();
int l=,r=n;
while(l<r)
{
int mid=(l+r)>>;
if(check(mid)) r=mid;
else l=mid+;
}
if(!check(l)) printf("-1\n");
else printf("%d\n",l);
}
return ;
}

hdu 6241 Color a Tree 2017 CCPC 哈理工站 L的更多相关文章

  1. hdu 4603 Color the Tree

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

  2. HDU 6121 Build a tree —— 2017 Multi-University Training 7

    HazelFan wants to build a rooted tree. The tree has nn nodes labeled 0 to n−1, and the father of the ...

  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 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)

    题目链接  2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块.    分块的时候满足每个块是一个 ...

  6. HDU 6270 Marriage (2017 CCPC 杭州赛区 G题,生成函数 + 容斥 + 分治NTT)

    题目链接  2017 CCPC Hangzhou Problem G 题意描述很清晰. 考虑每个家庭有且仅有$k$对近亲的方案数: $C(a, k) * C(b, k) * k!$ 那么如果在第$1$ ...

  7. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  8. HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)

    题目链接  2017 CCPC Hangzhou  Problem E 题意  给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...

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

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

随机推荐

  1. Shiro权限管理框架

    一.Shiro介绍 Apache Shiro 是Java 的一个安全框架.Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE 环境,也可以用在JavaEE 环境.Shiro 可以 ...

  2. 程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板

    程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板 Web 开发中几乎的平台都需要一个后台管理,但是从零开发一套后台控制面板并不容易,幸运的是有很多开源免费的后台控制面板可以给开发者 ...

  3. linux PWM蜂鸣器移植以及驱动程序分析【转】

    本文转载自:https://blog.csdn.net/lxllinux/article/details/80885331 一.关于PWM:        PWM(Pulse Width Modula ...

  4. Cmd Markdown 编辑阅读器

    我们理解您需要更便捷更高效的工具记录思想,整理笔记.知识,并将其中承载的价值传播给他人,Cmd Markdown 是我们给出的答案 -- 我们为记录思想和分享知识提供更专业的工具. 您可以使用 Cmd ...

  5. P3346 [ZJOI2015]诸神眷顾的幻想乡

    思路 注意到叶子节点(度数为1)只有20个,可以分别以这20个节点为根,把所有子串插入SAM中,统计最后的本质不同的子串个数 所以就是广义SAM了 然后注意要判断一下有无重复插入 代码 #includ ...

  6. Learning-MySQL【3】:数据类型和运算符

    一.数据类型 1.数值类型 为每张表的每个字段选择合适的数据类型是数据库设计过程中一个重要的步骤. 数字分为整数和小数.其中整数用整数类型表示,小数用浮点数类型和定点数类型表示. 浮点数类型包括单精度 ...

  7. 【NOI 2015】品酒大会

    Problem Description 一年一度的"幻影阁夏日品酒大会"隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发"首席品酒家"和" ...

  8. Jmeter 分布式(Jmeter5.1版本)

    一.修改负载机配置 vi /home/programs/apps/apache-jmeter-5.1/bin/jmeter.properties A.(先保证1099端口没有被占用,这里假设此端口未被 ...

  9. wrk 安装使用

    ==================== 安装 ====================https://github.com/wg/wrk/wiki sudo yum -y groupinstall ...

  10. ckeditor5 增加居中alignment

    https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html 克隆下来 gi ...