CF1324 --- Maximum White Subtree

题干

You are given a tree consisting of \(n\) vertices. A tree is a connected undirected graph with \(n−1\) edges. Each vertex \(v\) of this tree has a color assigned to it (\(a_v\)=1 if the vertex \(v\) is white and \(0\) if the vertex \(v\) is black).

You have to solve the following problem for each vertex \(v\): what is the maximum difference between the number of white and the number of black vertices you >can obtain if you choose some subtree of the given tree that contains the vertex \(v\)? The subtree of the tree is the connected subgraph of the given tree. More >formally, if you choose the subtree that contains \(cnt_w\) white vertices and \(cnt_b\) black vertices, you have to maximize \(cnt_w−cnt_b\).

\(\mathcal{Input}\)

The first line of the input contains one integer \(n\) (\(2 \leq n \leq 2 * 10^5\)) — the number of vertices in the tree.

The second line of the input contains \(n\) integers \(a_1,a_2,\cdots, a_n (0\leq a_i \leq 1)\), where \(a_i\) is the color of the \(i-th\) vertex.

Each of the next \(n−1\) lines describes an edge of the tree. Edge \(i\) is denoted by two integers \(u_i\) and \(v_i\), the labels of vertices it connects \((1 \leq u_i,v_i \leq n,u_i \not= v_i)\).

It is guaranteed that the given edges form a tree.

\(\mathcal{Output}\)

Print \(n\) integers \(res_1, res_2, \cdots, res_n\), where \(res_i\) is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex \(i\).

\(\mathcal{Example}\)

\(Case_1\)

\(Input\)

9

0 1 1 1 0 0 0 0 1

1 2

1 3

3 4

3 5

2 6

4 7

6 8

5 9

\(Output\)

2 2 2 2 2 1 1 0 2

\(Case_2\)

\(Input\)

4

0 0 1 0

1 2

1 3

1 4

\(Output\)

0 -1 1 -1

\(\mathcal{Note}\)

The first example is shown below:



The black vertices have bold borders.

In the second example, the best subtree for vertices \(2\),\(3\) and \(4\) are vertices \(2\),\(3\) and \(4\) correspondingly. And the best subtree for the vertex \(1\) is the subtree consisting of vertices \(1\) and \(3\).

\(\mathcal{Tag}\)

dfs and similar dp *1800

思路分析

 这题要求的是求出对任何一个vertex \(v\),求出包含这个节点的子树\(cnt_w - cnt_b\)的最大值。

暴力想法

 首先思考下暴力写法应该如何写。



 所以,对于所有可能的路径的贡献值的累加,且贡献值需大于等于\(0\).不妨设\(dp[v]\)代表该结点的最大值。故

\[dp[v] = a_v + \sum_{i=0}^{k}max(0, dp[i])
\]

 假如用暴力写法,就是对于每个结点\(v\),暴力搜索所有的adjacent结点,利用\(dfs\)暴力搜索。但是结点最大为\(2*10^5\)这个暴力算法显然会超时,考虑如何优化。

算法优化

 对于从下往上的贡献,可以利用从下往上的\(dfs\)树形\(dp\)进行获取,剩余的就是刨去以\(v\)为根的子树的贡献值比较难求。在这里我们设\(fa\)为结点\(v\)的父节点。\(f_v\)代表从下往上以\(v\)为根的白点数减去黑点数的最大值.\(dp[v]\)代表最终的最大值。因此根据刨去以\(v\)为根的子树的贡献值这个思想,我们可以发现:

\[add = dp[fa] - max(0, f_v)
\]

 就是刨去以\(v\)为根的子树的贡献值。因此最终我们可以写出状态转移方程:

\[ dp[v] =\left\{\begin{array}[ll]
1f[v] & if \;v = root \\
f[v] + max(0, dp[fa] - max(0, f[v])) & if\; v \not= root\\
\end{array}\right.
\]

 因此最后我们的思路为:

  • 从下往上树形\(dp\),计算\(f_v\)
  • 从上往下换根\(dp\),计算\(dp[v]\)

代码

#include<bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>; VI a;
VI dp;
VI ans;
VVI e; void dfs(int x, int fa = -1){
dp[x] = a[x];
for (int to : e[x]){
if (to == fa) continue;
dfs(to, x);
dp[x] += max(0, dp[to]);
}
} void rdfs(int x, int fa = -1){
ans[x] = dp[x];
for (int to : e[x]){
if (to == fa) continue;
dp[x] -= max(0, dp[to]);
dp[to] += max(0, dp[x]);
rdfs(to, x);
dp[to] -= max(0, dp[x]) ;
dp[x] += max(0, dp[to]);
}
} int main(){
int n; cin >> n;
a = dp = ans = VI(n);
e = VVI(n); for (int i = 0; i < n; ++ i){
cin >> a[i];
if (a[i] == 0) a[i] = -1;
}
for (int i = 0; i < n - 1; ++ i){
int x, y;
cin >> x >> y;
-- x, -- y;
e[x].push_back(y);
e[y].push_back(x);
}
dfs(0);
rdfs(0); for (int ret : ans) cout << ret << " ";
cout << endl;
return 0;
}

 第一次在博客园写题解,加油加油! 每天一个CF题!

CF1324 --- Maximum White Subtree的更多相关文章

  1. CF1324F Maximum White Subtree 题解

    原题链接 简要题意: 给定一棵树,每个点有黑白两种颜色:对每个节点,求出包含当前节点的连通图,使得白点数与黑点数差最小.输出这些值. F题也这么简单,咳咳,要是我也熬夜打上那么一场...可惜没时间打啊 ...

  2. CF1324F Maximum White Subtree——换根dp

    换根dp,一般用来解决在无根树上,需要以每个节点为根跑一边dfs的dp问题 我们做两遍dfs 先钦定任意一个点为根 第一遍,算出\(f_i\)表示\(i\)的子树产生的答案,这里,子树指的是以我们钦定 ...

  3. Codeforces 1324F Maximum White Subtree DFS

    题意 给你无根一颗树,每个节点是黑色或白色.对于每一个节点,问包含该节点的权值最大的子树. 子树的权值等于子树中白点的个数减去黑点的个数. 注意,这里的子树指的是树的联通子图. 解题思路 这场就这题卡 ...

  4. Codeforces Round #627 (Div. 3) F - Maximum White Subtree(深度优先搜索)

    题意: n 个点 n - 1 条边的树,问每个点所在所有子树中白黑点数目的最大差. 思路: 白点先由下至上汇集,后由上至下分并. #include <bits/stdc++.h> usin ...

  5. [Leetcode] 1120. Maximum Average Subtree

    Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subt ...

  6. 【LeetCode】1120. Maximum Average Subtree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...

  7. CF1092 --- Tree with Maximum Cost

    CF1324 --- Maximum White Subtree 题干 You are given a tree consisting exactly of \(n\) vertices. Tree ...

  8. Codeforces 1238F. The Maximum Subtree

    传送门 考虑构造一些区间使得树尽可能的 "大" 发现这棵树最多就是一条链加上链上出去的其他边连接的点 构造的区间大概长这样(图比较丑请谅解..$qwq$,图中每一个 "└ ...

  9. [CF 1238F] The Maximum Subtree 树DP

    题意 给定一颗树,求这个树的最大子树,且这个子树是一个good-tree. good-tree的定义是:每个节点可以表示成一个数值区间,而树上的边表示两个点表示的数值区间相交. 题解 通过分析可以发现 ...

随机推荐

  1. 写给程序员的机器学习入门 (二) - pytorch 与矩阵计算入门

    pytorch 简介 pytorch 是目前世界上最流行的两个机器学习框架的其中之一,与 tensoflow 并峙双雄.它提供了很多方便的功能,例如根据损失自动微分计算应该怎样调整参数,提供了一系列的 ...

  2. [ddt01篇]十年测试老鸟帮您解析:ddt数据驱动实现自动化测试入门基础应用

    一.什么是DDT数据驱动框架 ​ 全称:data driver test数据驱动测试框架,可以完美的应用于unittest框架实现数据驱动.ddt使用简介: 1.测试数据为多个字典的list类型 2. ...

  3. Halo博客的搭建

    今日主题:搭建一个私人博客 好多朋友和我说,能不能弄一个简单的私人博客啊,我说行吧,今天给你们一份福利啦! 搭建一个私人博客,就可以在自己的电脑上写博客了 Halo Halo 是一款现代化的个人独立博 ...

  4. Linux基础;Day07

    dns服务  dns的作用:地址解析 IP -> 域名(反向)  域名 -> IP(正向) 类型 主域名服务器 负责维护一个区域的所有域名信息,是特定的所有信息的权威信息源,数据可以修改. ...

  5. 【php】php操作MySQL数据库

    一.操作步骤: 1. 连接MySQL数据库并判断是否连接成功2. 选择数据库3. 设置字符集4. 准备SQL语句5. 向MySQL服务发送SQL语句6. 解析处理结果集7. 释放结果集,关闭数据库连接 ...

  6. C#多线程系列(3):原子操作

    本章主要讲述多线程竞争下的原子操作. 目录 知识点 竞争条件 线程同步 CPU时间片和上下文切换 阻塞 内核模式和用户模式 Interlocked 类 1,出现问题 2,Interlocked.Inc ...

  7. Java创建线程的三种形式的区别以及优缺点

    1.实现Runnable,Callable Callable接口里定义的方法有返回值,可以声明抛出异常. 继承Callable接口实现线程 class ThreadCall implements Ca ...

  8. 【课程学习】课程2:十行代码高效完成深度学习POC

    本文用户记录黄埔学院学习的心得,并补充一些内容. 课程2:十行代码高效完成深度学习POC,主讲人为百度深度学习技术平台部:陈泽裕老师. 因为我是CV方向的,所以内容会往CV方向调整一下,有所筛检. 课 ...

  9. G. 大树的水塘

    已知每块石头中的规格是1×1×1,水塘的长度为N,宽度为1,在第i位置,大树放了ai个石头 设大树建造的水塘蓄水量为V 请你求出在长度和宽度不变的情况下,建造一个蓄水量不小于V的水塘最多可以节约多少石 ...

  10. 关于C++线程池的实现的思考

    今天突然对前些日子一直很疑惑的c++线程池有了新的想法.其实所谓的线程池无非就是两个技术点,一个,多线程,指工作线程和主线程分离,或者说数据接收和处理分两个线程,一般就是讲需要运行的函数放到子线程执行 ...