F. Maximum White Subtree 树形dp*换根

本文分享了一段使用C++实现的图论算法代码,主要针对树形结构进行深度优先搜索(DFS)来求解节点的最优路径问题。通过递归调用,计算每个节点的最大得分,并进一步调整以确保整体路径的最优化。代码中详细展示了如何初始化图结构、执行DFS遍历以及最终输出结果的过程。

在这里插入图片描述
大佬博客

#include <bits/stdc++.h>
using namespace std ;
const int N = 2e5 + 10 ;
int ans[N] , a[N] , dp[N] ;
vector<int> v[N] ;
int dfs(int u , int fa)
{
  dp[u] = a[u] ;
  for(auto x : v[u])
   {
     if(x == fa) continue ;
     dfs(x , u) ;
     dp[u] += max(dp[x] , 0) ;
   }
}
int dfs1(int u , int fa , int sum)
{
  ans[u] = dp[u] + sum ;
  for(auto x : v[u])
   {
     if(x == fa) continue ;
     dfs1(x , u , max(0 , ans[u] - max(0 , dp[x]))) ;
   }
}
int main()
{
  int n ;
  cin >> n ;
  for(int i = 1; i <= n ;i ++) cin >> a[i] , a[i] = (a[i] == 0 ? -1 : 1);
  for(int i = 1 , x , y ; i < n ;i ++)
   cin >> x >> y , v[x].push_back(y) , v[y].push_back(x) ;
  dfs(1 , -2) ;
  dfs1(1 , -1 , 0) ;
  for(int i = 1; i <= n ;i ++) cout << ans[i] << " " ;
  puts("") ;
  return 0 ;
}
"Visual Studio Code is unable to watch for file changes in this large workspace" (error ENOSPC) When you see this notification, it indicates that the VS Code file watcher is running out of file handles that are needed to implement file watching. Most often this can happen when opening a workspace that is large and contains many files. Before adjusting platform limits, make sure that potentially large folders, such as Python .venv, are added to the files.watcherExclude setting (more details below). It is also possible that other running applications consume so many file handles that none are left for VS Code to use. In that case, it might help to close these other applications. The current limit can be viewed by running: cat /proc/sys/fs/inotify/max_user_watches Copy The limit can be increased to its maximum by editing /etc/sysctl.conf (except on Arch Linux, read below) and adding this line to the end of the file: fs.inotify.max_user_watches=524288 Copy The new value can then be loaded in by running sudo sysctl -p. While 524,288 is the maximum number of files that can be watched, if you're in an environment that is particularly memory-constrained, you might want to lower the number. Each file watch takes up 1,080 bytes, so assuming that all 524,288 watches are consumed, that results in an upper bound of around 540 MiB. Arch-based distros (including Manjaro) require you to change a different file; follow these steps instead. Another option is to exclude specific workspace directories from the VS Code file watcher with the files.watcherExclude setting. The default for files.watcherExclude excludes node_modules and some folders under .git, but you can add other directories that you don't want VS Code to track. "files.watcherExclude": { "**/.git/objects/**": true, "**/.git/subtree-cache/**": true, "**/node_modules/*/**": true }
最新发布
08-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值