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 ...
随机推荐
- SVN冲突以及解决办法
1.冲突原因: 假设 A.B 两个用户都在版本号为 100 的时候,更新了 kingtuns.txt 这个文件,A 用户在修改完成之后提交 kingtuns.txt 到服务器, 这个时候提交成功,这个 ...
- 论文笔记:Real-Time MDNet
Real-Time MDNet ECCV 2018 2018-10-22 15:52:01 Paper:http://openaccess.thecvf.com/content_ECCV_2018/ ...
- tcp协议以及socket介绍
壹:tcp协议:可靠传输 一: 3次握手,建立链接:4次挥手,断开链接 3次握手,建立链接:图解 ,这样双向通路就建立完成了. 在建立链接时,并没有数据传输,所以中间两部可以合在一起,也就是3次握手, ...
- HashMap 和 HashTable差别
代码版本 JDK每一版本都在改进.本文讨论的HashMap和HashTable基于JDK 1.7.0_67.源码见这里 1. 时间 HashTable产生于JDK 1.1,而HashMap产生于JDK ...
- 在table表格中实现圆角效果
在table中设置border-radius发现不起作用,网上查找了一番,原因是border-collapse:collapse和border-radius不兼容. 设计图效果 代码实现效果: < ...
- prometheus告警函数
PromQL基础 http_request_total{} 瞬时向量表达式,选择当前最新的数据 http_request_total{}[5m] 区间向量表达式,选择以当前时间为基准,5分钟内 ...
- a*b高精度数组算法
#include<stdio.h> #include<string.h> int main() { ]={},b[]={},c[]={},len1=,len2=; ],str2 ...
- CentOS 7上安装Pure-FTPd
# 安装 yum install epel-release yum install pure-ftpd 位置文件位于/etc/pure-ftpd/pure-ftpd.conf # 修改配置文件 # 注 ...
- Python 同一文件中,有unittest不执行“if __name__ == '__main__”,不生成HTMLTestRunner测试报告的解决方案
1.问题:Python中同一个.py文件中同时用unittest框架和HtmlReport框架后,HtmlReport不被执行. 2.为什么?其实不是HtmlReport不被执行,也不是HtmlRep ...
- Oracle单机Rman笔记[1]---环境准备
A.-----安装程序准备---- 1.拷贝oracle安装包到一个目录下 2.检查并修改hostname /etc/sysconfig/network中的hostname要与/etc/hosts中的 ...