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 ...
随机推荐
- Shell 解释器初识
1.脚本文件要以.sh结尾,第一行要跟#!/bin/bash解释器. 2.运行shell脚本. (1)添加权限:可以加x执行权限,./123.sh (2)命令执行:bash 123.sh,sh 123 ...
- Shell 实践、常用脚本进阶
1.备份单个文件 #!/bin/bash #备份单个文件 DATE=`/bin/date +%y%m%d` /bin/tar -czpf /backup/$1.$DATE.tar.gz /backup ...
- A Summary of Multi-task Learning
A Summary of Multi-task Learning author by Yubo Feng. Intro In this paper[0], the introduction of mu ...
- Ansible在Ubuntu上的安装
#apt安装 apt-get install software-properties-common apt-add-repository ppa:ansible/ansible apt-get upd ...
- Windows Update Medic Service 拒绝访问
修改注册表:HEKY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WaaSMedicSvc 中Start的值改为4.
- Endnote X8激活注册信息
使用以下激活信息进行注册: Name:胡萝卜周 Organization:www.carrotchou.blog Product Key:3VHLX-TJJ74-SAM4N-38HEX-KNUCL 来 ...
- ASP.NET Core Swagger 显示接口注释
在Startup中 services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new Info { Title ...
- chrome 调试功能使用说明
单文件搜索 ctrl+f 全局搜索 ctrl+shift+f
- linux文件管理之链接文件
文件链接 ====================================================================================软链接 或 符号链接硬 ...
- VSCode汉化
1.打开VSCode 点击箭头指示地方 在搜索框中输入chinese 然后安装中文简体 2.按住 Ctrl+shift+p 选择配置显示语言 然后会看见下面的样子 添加 "locale&q ...