hdu 6241 Color a Tree 2017 CCPC 哈理工站 L
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的更多相关文章
- hdu 4603 Color the Tree
这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...
- 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 ...
- 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 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)
题目链接 2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块. 分块的时候满足每个块是一个 ...
- HDU 6270 Marriage (2017 CCPC 杭州赛区 G题,生成函数 + 容斥 + 分治NTT)
题目链接 2017 CCPC Hangzhou Problem G 题意描述很清晰. 考虑每个家庭有且仅有$k$对近亲的方案数: $C(a, k) * C(b, k) * k!$ 那么如果在第$1$ ...
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- HDU 6268 Master of Subgraph (2017 CCPC 杭州 E题,树分治 + 树上背包)
题目链接 2017 CCPC Hangzhou Problem E 题意 给定一棵树,每个点有一个权值,现在我们可以选一些连通的点,并且把这点选出来的点的权值相加,得到一个和. 求$[1, m] ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
随机推荐
- CSS hover
CSS hover hover 鼠标移动到当前标签上时,以下css属性才能生效 <!DOCTYPE html> <html lang="en"> <h ...
- 内置函数filter()和匿名函数lambda解析
一.内置函数filter filter()函数是 Python 内置的一个高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回由符合条件迭代器 ...
- tensorflow中batch normalization的用法
网上找了下tensorflow中使用batch normalization的博客,发现写的都不是很好,在此总结下: 1.原理 公式如下: y=γ(x-μ)/σ+β 其中x是输入,y是输出,μ是均值,σ ...
- Javascript中DataGrid表格纵线添加数据
接之前写的一篇博客http://www.cnblogs.com/Liu30/p/7229641.html,生成一个6*24的表格之后,添加数据 表格数据一般都是按行添加,我所做的这个表格是想添加一天2 ...
- 最短路计数——Dijkstra
题目: 给出一个N个顶点M条边的无向无权图,顶点编号为1−N.问从顶点1开始,到其他每个点的最短路有几条. ——传送门 受到题解的启发,用 Dijkstra A掉(手工代码) 思路: 1.无向无权图, ...
- Executors创建线程池的几种方式以及使用
Java通过Executors提供四种线程池,分别为: 1.newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程. ...
- Android Vector曲折的兼容之路
Android Vector曲折的兼容之路 两年前写书的时候,就在研究Android L提出的Vector,可研究下来发现,完全不具备兼容性,相信这也是它没有被广泛使用的一个原因,经过Google的不 ...
- Steam饥荒
存档回滚 D:\Program Files (x86)\Steam\userdata\***\219740\remote 巨人国是survival_数字,海难是shipwreck_数字,哈姆雷特是po ...
- httpd常见配置
httpd常见配置 配置文件 /etc/httpd/conf/httpd.conf 主配置文件 /etc/httpd/conf.d/*.conf 辅助配置文件 配置文件语法检查及重新加载配置文 ...
- 『Python』多进程处理
尝试学习python的多进程模组,对比多线程,大概的区别在: 1.多进程的处理速度更快 2.多进程的各个子进程之间交换数据很不方便 多进程调用方式 进程基本使用multicore() 进程池优化进程的 ...