Codeforces 1324F Maximum White Subtree DFS
题意
给你无根一颗树,每个节点是黑色或白色。对于每一个节点,问包含该节点的权值最大的子树。
子树的权值等于子树中白点的个数减去黑点的个数。
注意,这里的子树指的是树的联通子图。
解题思路
这场就这题卡的比较久。
首先,如果是有根树的话,只需要dfs一遍就能得出根的答案。
设根为1,将无根树转为有根树。对于每一个节点定义\(ans\)和\(val\)。
其中,\(val_u=\sum_{val_v>0且v是u的儿子}{val_v}\)
然后,再一次dfs就可以获得所有节点的答案。
对于节点\(u\)和他的父节点\(f\),他往子节点方向的结果已经确定了,就是\(val_u\),现在就剩他往\(f\)方向的结果需要计算。如果\(val_u>0\),那么往\(f\)方向的权值就是\(ans_f-val_u\),否则就是\(ans_f\)。如果往\(f\)方向的权值大于0就加到\(ans_u\)上。
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define endl '\n'
const double PI=acos(-1.0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
namespace IO{
bool REOF = 1; //为0表示文件结尾
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && REOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (REOF = 0, EOF) : *p1++;
}
template<class T>
inline bool read(T &x) {
char c = nc();bool f = 0; x = 0;
while (c<'0' || c>'9')c == '-' && (f = 1), c = nc();
while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
if(f)x=-x;
return REOF;
}
template<typename T, typename... T2>
inline bool read(T &x, T2 &... rest) {
read(x);
return read(rest...);
}
inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')); }
// inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || c==' '; }
inline bool read_str(char *a) {
while ((*a = nc()) && need(*a) && REOF)++a; *a = '\0';
return REOF;
}
inline bool read_dbl(double &x){
bool f = 0; char ch = nc(); x = 0;
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=nc();}
while(ch>='0'&&ch<='9'){x=x*10.0+(ch^48);ch=nc();}
if(ch == '.') {
double tmp = 1; ch = nc();
while(ch>='0'&&ch<='9'){tmp=tmp/10.0;x=x+tmp*(ch^48);ch=nc();}
}
if(f)x=-x;
return REOF;
}
template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;
cerr<<'='<<h<<','<<' '; _dbg(sdbg+1, a...);
}
template<class T> ostream &operator<<(ostream& os, vector<T> V) {
os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class T> ostream &operator<<(ostream& os, set<T> V) {
os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class T> ostream &operator<<(ostream& os, map<T,T> V) {
os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.st << "," << P.nd << ")";
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
}
using namespace IO;
const int maxn=2e5+5;
const int maxv=2e5+5;
const int mod=998244353; // 998244353 1e9+7
const int INF=1e9+7; // 1e9+7 0x3f3f3f3f 0x3f3f3f3f3f3f3f3f
const double eps=1e-12;
int dx[4]={0,1,0,-1};
//int dx[8]={1,0,-1,1,-1,1,0,-1};
int dy[4]={1,0,-1,0};
//int dy[8]={1,1,1,0,0,-1,-1,-1};
// #define ls (x<<1)
// #define rs (x<<1|1)
// #define mid ((l+r)>>1)
// #define lson ls,l,mid
// #define rson rs,mid+1,r
/**
* ********** Backlight **********
* 仔细读题
* 注意边界条件
* 记得注释输入流重定向
* 没有思路就试试逆向思维
* 加油,奥利给
*/
int tot,head[maxn];
struct Edge{
int v,nxt;
Edge(){}
Edge(int _v,int _nxt):v(_v),nxt(_nxt){}
}e[maxn<<1];
void init(){
tot=1;
memset(head,0,sizeof(head));
}
void addedge(int u,int v){
e[tot]=Edge(v,head[u]); head[u]=tot++;
e[tot]=Edge(u,head[v]); head[v]=tot++;
}
void addarc(int u,int v){
e[tot]=Edge(v,head[u]); head[u]=tot++;
}
int n,a[maxn],ans[maxn],val[maxn];
void dfs1(int u,int f){
val[u]=a[u];
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
dfs1(v,u);
if(val[v]>=0)val[u]+=val[v];
}
}
void dfs2(int u,int f){
ans[u]=val[u];
if(val[u]>0)ans[f]-=val[u];
if(ans[f]>0)ans[u]+=ans[f];
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==f)continue;
dfs2(v,u);
}
if(val[u]>0)ans[f]+=val[u];
}
void solve(){
read(n);
for(int i=1;i<=n;i++){
read(a[i]);
if(a[i]==0)a[i]=-1;
}
int u,v; init();
for(int i=1;i<=n-1;i++){
read(u,v);
addedge(u,v);
}
dfs1(1,0);
dfs2(1,0);
for(int i=1;i<=n;i++)printf("%d ",ans[i]);
}
int main()
{
// freopen("in.txt","r",stdin);
// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int _T; read(_T); for(int _=1;_<=_T;_++)solve();
// while(read(n))solve();
solve();
return 0;
}
Codeforces 1324F Maximum White Subtree DFS的更多相关文章
- CF1324 --- Maximum White Subtree
CF1324 --- Maximum White Subtree 题干 You are given a tree consisting of \(n\) vertices. A tree is a c ...
- Codeforces Round #627 (Div. 3) F - Maximum White Subtree(深度优先搜索)
题意: n 个点 n - 1 条边的树,问每个点所在所有子树中白黑点数目的最大差. 思路: 白点先由下至上汇集,后由上至下分并. #include <bits/stdc++.h> usin ...
- CF1324F Maximum White Subtree 题解
原题链接 简要题意: 给定一棵树,每个点有黑白两种颜色:对每个节点,求出包含当前节点的连通图,使得白点数与黑点数差最小.输出这些值. F题也这么简单,咳咳,要是我也熬夜打上那么一场...可惜没时间打啊 ...
- CF1324F Maximum White Subtree——换根dp
换根dp,一般用来解决在无根树上,需要以每个节点为根跑一边dfs的dp问题 我们做两遍dfs 先钦定任意一个点为根 第一遍,算出\(f_i\)表示\(i\)的子树产生的答案,这里,子树指的是以我们钦定 ...
- Codeforces 484B Maximum Value(高效+二分)
题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...
- codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)
题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...
- Codeforces C. Maximum Value(枚举二分)
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】
传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...
- Codeforces Round #665 (Div. 2) D - Maximum Distributed Tree dfs贡献记录
题意: t组输入,每组数据中n个节点构成一棵树,然后给你n-1条边.给你一个m,然后给你m个k的素数因子,你需要给这n-1条边都赋一个权值,这n-1条边的权值之积应该等于k.如果k的素数因子数量小于n ...
随机推荐
- JS解密入门——有道翻译
JS解密入门——有道翻译 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这 ...
- java动态代理——代理方法的假设和验证及Proxy源码分析五
前文地址 https://www.cnblogs.com/tera/p/13419025.html 本系列文章主要是博主在学习spring aop的过程中了解到其使用了java动态代理,本着究根问底的 ...
- Linux查询版本、查询端口
lsb_release -a 查看当前Linux系统版本 netstat 检查端口 netstat 是一个命令行工具,可以提供有关网络连接的信息.要列出正在侦听的所有 TCP 或 UDP 端口,包括使 ...
- Linux下如何高效切换目录?
Linux 下对于目录的切换,大家肯定会想到一个命令:cd 命令.这个是 Linux 下再基本不过的命令,如果这个命令都不知道的话,赶紧剖腹自尽去吧. cd 命令确实很方便,但如果需要频繁在下面的目录 ...
- Java并发---并发理论
一.如何理解线程安全 在多线程中稍微不注意就会出现线程安全问题,那么什么是线程安全问题? 我的认识是.在多线程下代码执行的结果和预期的正确的结果不一致,该代码就是线程不安全的,否则就是线程安全的 在深 ...
- 5年Java程序员,五面蚂蚁险拿offer定级P7,大厂面试不过如此?
当时面试能记下的就这些了,可能不太全请见谅: 一面 1.HashMap和ConcurrentHashMap: 2.再谈谈一致hash算法? 3.乐观锁还有悲观锁: 4.可重入锁和Synchronize ...
- python 01 print input int
学过c语言与c语言的数据结构与算法后再来学习python,感觉编程的核心内容没有变,但每个编程语言都有自己的特点.本次学习的目标是理解python的特点与用法,把学过的bif(内置函数)用法记录下来, ...
- 哲思集-转载自CSDN
CSDN 创始人蒋涛:世界被编码之后 分享赚¥9.90订阅博主 “写代码 20 年,通过网络平台和社区,帮助中国的程序员更好成长,这是我最希望被大家记住的.”这是蒋涛接受 ZAKER 采访的开场白. ...
- idea工程在maven projects中显示灰色的解决办法
原文链接:https://blog.csdn.net/qq_30507287/article/details/83515461 在Mac上使用idea进行开发的过程中,一般在MavenProject中 ...
- 7.hbase shell命令 cmd
$HADOOP_USER_NAME #创建命名空间create_namespace 'bd1902' #展示所有命名空间 list_namespace #删除命名空间,The namespace mu ...