CodeForces 1324F Maximum White Subtree】的更多相关文章

题意 给你无根一颗树,每个节点是黑色或白色.对于每一个节点,问包含该节点的权值最大的子树. 子树的权值等于子树中白点的个数减去黑点的个数. 注意,这里的子树指的是树的联通子图. 解题思路 这场就这题卡的比较久. 首先,如果是有根树的话,只需要dfs一遍就能得出根的答案. 设根为1,将无根树转为有根树.对于每一个节点定义\(ans\)和\(val\). 其中,\(val_u=\sum_{val_v>0且v是u的儿子}{val_v}\) 然后,再一次dfs就可以获得所有节点的答案. 对于节点\(u\…
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 \(…
题意: 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[…
原题链接 简要题意: 给定一棵树,每个点有黑白两种颜色:对每个节点,求出包含当前节点的连通图,使得白点数与黑点数差最小.输出这些值. F题也这么简单,咳咳,要是我也熬夜打上那么一场...可惜没时间打啊 美国佬怎么想的,不能让比赛设置成美国的上午,那我们就是下午了:非要设置成下午,那我们就是半夜... 首先,这题一看就是 \(\texttt{dp}\),树形 \(\texttt{dp}\),换根 \(\texttt{dp}\). 下文中,用 \(\texttt{Subtree(i)}\) 表示 \…
换根dp,一般用来解决在无根树上,需要以每个节点为根跑一边dfs的dp问题 我们做两遍dfs 先钦定任意一个点为根 第一遍,算出\(f_i\)表示\(i\)的子树产生的答案,这里,子树指的是以我们钦定的那个点为根的有根树上的子树 这是从下向上的转移,也是一般树上dp的常规操作 第二遍,我们要算出真正的答案 这次是对于无根树中的子树,在第一遍dfs中,\(f_i\)已经包含了\(i\)所有"向下"的子树的贡献,那剩下的一棵子树是以它为根并向上连向它父亲的 设\(i\)的父亲是\(fa\)…
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然后枚举k,每次用二分找到小于k∗aj而且最大的ai,维护答案,过程中加了一些剪枝. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn =…
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of *a**i* divi…
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.…
题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个块相连要求相连处颜色相同,求价值最大的连接方案. 关心到color最大为4,以4种颜色为点,对于每个块,在(color1,color2)间连一条边权为(val)的边,建一张4个点n条边的图.显然,在图上选一条价值最大的路径(或回路)就是答案了. 方法一: 如果这张图本身就是Eular路径(或Eula…
作者: 负雪明烛 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…