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 ...
随机推荐
- 定位bug的基本要求
很多人觉得qa只是负责发现问题,这个实在太狭隘了,现代qa除了发现问题这种基本功外,定位问题,提出解决方案,提出预防方案也是要掌握的技能.这里先说定位问题的要求,定位问题要向深入,前提当然是对功能.产 ...
- STAT UN2102 Homework
STAT UN2102 Homework 4 [100 pts]Due 11:59pm Monday, May 6th on CanvasYour homework should be submitt ...
- Linux学习进阶示意图
Linux 基础 Linux 基础 Linux安装专题教程 Linux中文环境 Linux—从菜鸟到高手 鸟哥的Linux私房菜 基础学习篇(第二版) Ubuntu Linux入门到精通 Linux标 ...
- 获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等) 目标:使用百度定位sdk开发实时移动距离计算功能,根据经纬度的定位,计算行驶公里数并实时刷新界面显示.大家 ...
- 《温故而知新》JAVA基础五
定义:是类和类之间的关系"is a" 弗父类(基类)->子类(派生类) 是一直单继承的关系 好处:子类拥有父类的属性方法(private除外) 语法 class Son ex ...
- MVC 深入讲解Routing _路由规则【八】
一.客户端=>控制器 在项目中我们引用了system.web.routing, 如果第一个匹配成功了,那么后面的都不会再匹配. 1. routing的作用: 确定colltroller,确定ac ...
- VC.重定向标准输出到文件(父进程方式)
1.libxml2 使用过程中,有时 libxml2里面会报一些错误信息,在 控制台的程序中 这些信息看起来比较乱,不易观察,我想将这些信息重定向到 文件中 1.1.本进程内:试着 将标准输出,标准错 ...
- HDFS常用操作命令
启动hdfs#start-all.sh查看hdfs的配置文件#cat hdfs-site.sh#hadoop fs -put /soft/jdk /#HDFS上传文件命令查看上传后的文件属性#hado ...
- 8个让DevOps转型取得成功的关键步骤
关注嘉为科技,获取运维新知 在数字化时代,企业需要更快更灵活的交付来支持业务运营,这种迫切的需求促成了DevOps的高速发展,成为了企业获得竞争优势的关键.尽管大家都知道DevOps给业务带来的好 ...
- form标签的 enctype属性
1.enctype的定义: enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码. 默认地,表单数据会编码为 "application/x-www-form-urlencod ...