Color a Tree

题目大意:一棵树,根为1.某些点有一些限制。限制A: 该$x_i$点子树染色点至少$y_i$个。 限制B: 该$x_i$点子树外染色至少$y_i$个。求最少染色点数。

首先是二分答案,二分染色的点数。check就是对于这个mid能否满足要求。

令$f1[i]$表示$i$为根的子树的最少被染色个数。
令$f2[i]$表示$i$为根的子树的最多被染色个数。

符合要求的应该是$f1[i]<=mid<=f2[i]$。

考虑到$[L,M]$与$[M+1,R]$这样的子树合并的时候要求和,考虑按dfs深度分化,或者bfs序。

$f2[i]$更新由子树和mid-子树外的点的个数决定。
$f2[i]=min(\sum son,mid-bb[i])$.

$f1[i]$更新由子树和和子树内的点限制。
$f1[i]=max(\sum son,aa[i])$.

注意坑点 aa[i] 和 bb[i] 要取最大值。

#include <bits/stdc++.h>using namespace std;
typedef long long ll;
const int maxn = 100005;
vector<int> e[maxn];
int fa[maxn];
int Q[maxn], T, f, r;
bool vis[maxn];
int aa[maxn], bb[maxn], na, nb, u, v, n;
int f1[maxn], f2[maxn], mid;bool dfs(int id, int fa) {f1[id] = 0; f2[id] = 1;bool ok = 1;for (auto ep : e[id]) {if (ep == fa) continue;ok &= dfs(ep, id);if (!ok) break;}if (!ok) return false;if (bb[id] != -1) {if (mid - bb[id] < f1[id]) return false;f2[id] = min(f2[id], mid - bb[id]);}if (aa[id] != -1) {if (f2[id] < aa[id]) return false;f1[id]=max(f1[id],aa[id]);}f1[fa] += f1[id];f2[fa] += f2[id];return true;
}bool check(int mid) {f1[0] = f2[0] = 0;bool ok = dfs(1, 0);if (!ok) return false;if (f1[0] > mid) return false;if (f2[0] < mid) return false;return true;
}int main() {//freopen("in.txt","r",stdin);for (cin >> T; T--;) {scanf("%d", &n);memset(aa, -1, sizeof aa);memset(bb, -1, sizeof bb);for (int i = 1; i <= n; i++)e[i].clear();for (int i = 1; i < n; i++) {scanf("%d%d", &u, &v);e[u].push_back(v);e[v].push_back(u);}scanf("%d", &na);for (int i = 1; i <= na; i++) {scanf("%d%d", &u, &v);if (aa[u] == -1) aa[u] = v;else aa[u] = max(aa[u], v);}scanf("%d", &nb);for (int i = 1; i <= nb; i++) {scanf("%d%d", &u, &v);if (bb[u] == -1) bb[u] = v;else bb[u] = max(bb[u], v);}int l = 1, r = n, res = -1;while (l <= r) {mid = (l + r) >> 1;if (check(mid))r = (res = mid) - 1;else l = mid + 1;}printf("%d\n", res);}return 0;
}

转载于:https://www.cnblogs.com/foreignbill/p/7841929.html

HDU 6241 Color a Tree相关推荐

  1. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  2. POJ 2054 Color a Tree (贪心)

    $ POJ~2054~Color~a~Tree $ $ solution: $ 我们先从题中抽取信息,因为每个点的费用和染色的次数有关,所以我们可以很自然的想到先给权值大的节点染色.但是题目还说每个节 ...

  3. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  4. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. 【HDU - 4786 】Fibonacci Tree (最小生成树变形,上下界贪心,tricks)

    题干: Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some research on ...

  6. HDU 1556 Color the ball (数状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 5274 Dylans loves tree(树链剖分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5274 [题目大意] 给出一棵树,每个点有一个权值,权值可修改,且大于等于0,询问链上出现次数为奇数 ...

  8. hdu 1556 Color the ball

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. HDU - 6183 Color it(动态开点线段树/树状数组套动态开点线段树)

    题目链接:点击查看 题目大意:给出一个二维平面坐标系,需要完成四种操作: 0:删除所有点 1 xycx\ y\ cx y c:在点 (x,y)(x,y)(x,y) 处添加颜色 ccc 2 xy1y2x ...

最新文章

  1. 没有数学何来计算机:论计算机起源的数学思想
  2. 构建弹性架构组件—ELB和ASG
  3. ZBarSDK扫描二维码
  4. 第三次学JAVA再学不好就吃翔(part35)--多态向上转型和向下转型
  5. 了解ADF Faces clientComponent属性
  6. (转)动态规划和贪心算法的区别
  7. 字符串匹配算法——KMP算法学习
  8. ES6字符串的扩展方法~超详细哦
  9. django 模型增加字段后迁移失败
  10. INSTALL_FAILED_UID_CHANGED解决办法
  11. 怎么样使prestashop 运行速度更快
  12. 高通Snapdragon Sensor Core(SSC)笔记
  13. python获取当前工作路径
  14. echarts字符云(词云)
  15. git push时报错error: File: xxx 252.15 MB, exceeds 100.00 MB.
  16. 解决 Spring Cloud 整合 zipkin 报错:org.springframework.boot.actuate.health.CompositeHealthIndicator......
  17. springcloud为什么关闭了bus还是会连接mq
  18. 【区块链基础】5——ETH协议
  19. 洛谷P3975【天津省选2015】(后缀自动机DP)
  20. python跑神经网络_程序员深夜用Python跑神经网络,只为用中二动作关掉台灯!

热门文章

  1. 便利删除_知名便利店凉了?!刚刚道歉
  2. You are running Vue in development mode.Make sure to turn on production mode when deploying for p...
  3. python写一个app接收摄像头传输的视频_如何使用Python中的tcp套接字发送和接收网络摄像头流?...
  4. 修改java或css后不生效,还是显示修改之前的样式,问题已解决
  5. Android获取EditText,Spinner,CheckBox,RadioButton信息Toast显示
  6. c++语言定义排序函数,关于C++中定义比较函数的三种方法小结
  7. 20200316:H指数(leetcode274)
  8. 冯诺依曼结构和哈佛结构01
  9. VB 阿拉伯数字转换为中文大写数值函数
  10. 使用tar进行磁带备份的命令汇总