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

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 \(…
原题链接 简要题意: 给定一棵树,每个点有黑白两种颜色:对每个节点,求出包含当前节点的连通图,使得白点数与黑点数差最小.输出这些值. F题也这么简单,咳咳,要是我也熬夜打上那么一场...可惜没时间打啊 美国佬怎么想的,不能让比赛设置成美国的上午,那我们就是下午了:非要设置成下午,那我们就是半夜... 首先,这题一看就是 \(\texttt{dp}\),树形 \(\texttt{dp}\),换根 \(\texttt{dp}\). 下文中,用 \(\texttt{Subtree(i)}\) 表示 \…
换根dp,一般用来解决在无根树上,需要以每个节点为根跑一边dfs的dp问题 我们做两遍dfs 先钦定任意一个点为根 第一遍,算出\(f_i\)表示\(i\)的子树产生的答案,这里,子树指的是以我们钦定的那个点为根的有根树上的子树 这是从下向上的转移,也是一般树上dp的常规操作 第二遍,我们要算出真正的答案 这次是对于无根树中的子树,在第一遍dfs中,\(f_i\)已经包含了\(i\)所有"向下"的子树的贡献,那剩下的一棵子树是以它为根并向上连向它父亲的 设\(i\)的父亲是\(fa\)…
题意 给你无根一颗树,每个节点是黑色或白色.对于每一个节点,问包含该节点的权值最大的子树. 子树的权值等于子树中白点的个数减去黑点的个数. 注意,这里的子树指的是树的联通子图. 解题思路 这场就这题卡的比较久. 首先,如果是有根树的话,只需要dfs一遍就能得出根的答案. 设根为1,将无根树转为有根树.对于每一个节点定义\(ans\)和\(val\). 其中,\(val_u=\sum_{val_v>0且v是u的儿子}{val_v}\) 然后,再一次dfs就可以获得所有节点的答案. 对于节点\(u\…
题意: n 个点 n - 1 条边的树,问每个点所在所有子树中白黑点数目的最大差. 思路: 白点先由下至上汇集,后由上至下分并. #include <bits/stdc++.h> using namespace std; const int M=220000; vector<vector<int>> e(M); int n,a[M]; void dfs1(int u,int pre){ for(int v:e[u]){ if(v!=pre){ dfs1(v,u); a[…
Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subtree of a tree is any node of that tree plus all its descendants. The average value of a tree is the sum of its values, divided by the number of nodes.…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcode-cn.com/problems/maximum-average-subtree/ 题目描述 Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subtre…
CF1324 --- Maximum White Subtree 题干 You are given a tree consisting exactly of \(n\) vertices. Tree is a connected undirected graph with \(n−1\) edges. Each vertex \(v\) of this tree has a value \(a_v\) assigned to it. Let \(dist(x,y)\) be the distan…
传送门 考虑构造一些区间使得树尽可能的 "大" 发现这棵树最多就是一条链加上链上出去的其他边连接的点 构造的区间大概长这样(图比较丑请谅解..$qwq$,图中每一个 "└┘" 都是一段区间): 发现树其实就是个 "毛毛虫":传送门 所以直接求最大的毛毛虫即可 设毛毛虫的主链集合为 $S$ ,那么毛毛虫的点数就是 $\sum_{u \in S} (du[u])-(size-2)$ 其中 $du[u]$ 表示点 $u$ 的度数,$size$ 为 $S…
题意 给定一颗树,求这个树的最大子树,且这个子树是一个good-tree. good-tree的定义是:每个节点可以表示成一个数值区间,而树上的边表示两个点表示的数值区间相交. 题解 通过分析可以发现,这个子树是这个树的一条链,然后允许这条链上的点带上直接连接的点. 然后就转化为树上求最长链的DP问题. // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #pragma GCC optimize(4) #include <bits/s…