NYOJ 679 The Weight of Tree 搜索+dp+邻接表
The Weight of Tree
- 描述
-
456 has a tree of n nodes, each node is assigned with an integer number. Now 456 wants to select a subtree, such that the sum of all integers on the nodes of the subtree is maxmized. Can you help him?
- 输入
- On the first line of the input is an integer T, and then T cases follows. Each case begins with a positive integer n(1 <= n <= 10^5), then n numbers Wi(-1000 <= Wi <= 1000),Wi for the number on the ith node. Then n - 1 lines follows, each line contains two numbers a, b(1 <= a, b <= n)indicate that there is a edge between node a and b.
- 输出
- For each test case, output one integer on a line, the maximized sum can be achieved by selecting a subtree.
- 样例输入
-
3 1 5 2 5 -5 1 2 5 -2 -3 7 -1 4 1 2 2 3 3 4 2 5
样例输出
5 5 8
从第一个点一直往下深搜,然后回溯,判断子节点的值是否大于0,如果大于0,父节点的值变为当前的值加上子节点的值。最后输出最大值即可。
#include<stdio.h> #include<string.h> #include<vector> #include<algorithm> #define N 100005 using namespace std; vector<int> vec[N]; int vis[N],dp[N],MAX; void dfs(int x) {vis[x]=1;for(int i=0;i<vec[x].size();i++){int y=vec[x][i];if(vis[y])continue;dfs(y);if(dp[y]>0)dp[x]+=dp[y];MAX=max(dp[x],MAX);} } int main() {int t,n,i,a,b;scanf("%d",&t);while(t--){memset(vis,0,sizeof(vis));memset(vec,0,sizeof(vec));scanf("%d",&n);MAX=-999999;for(i=1;i<=n;i++){scanf("%d",&dp[i]);MAX=max(dp[i],MAX);}for(i=1;i<n;i++){scanf("%d%d",&a,&b);vec[a].push_back(b);vec[b].push_back(a);}dfs(1);printf("%d\n",MAX);}return 0; }
NYOJ 679 The Weight of Tree 搜索+dp+邻接表相关推荐
- 图的深度优先搜索和广度优先搜索(邻接表) - Java实现
文章目录 前言 1.什么是图? 2.图如何表示? 3.如何创建一个邻接表 一.深度优先搜索(Depth First Search) 二.广度优先搜索(Breadth First Search) 三.寻 ...
- CF 1646D Weight the Tree 树形dp
题目链接 这道题拖了很长时间才去补,今天想起来做这道题 这道题的题意是可以组成合法的点互不相邻,我的思路没有错误,但是赛时没有写出代码,现在想想也觉得可惜,我要是多想到一个度的条件,赛时可能就会过了o ...
- Codeforces Round #168 (Div. 2)D. Zero Tree(DP,中等难度)
D. Zero Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- [蓝桥杯][算法提高VIP]夺宝奇兵(记忆化搜索||DP)
题目描述 在一座山上,有很多很多珠宝,它们散落在山底通往山顶的每条道路上,不同道路上的珠宝的数目也各不相同.下图为一张藏宝地图: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 " ...
- 最短路径Dijkstra(静态邻接表+优先队列模板)+ 记忆化搜索
这道题的解题步骤是这样的: (1)用Dijkstra求出每个点到house(也就是2号点)的最短距离,我是记录在数组dist[]中: (2)我们要求的是office(1号点)到house(或2--&g ...
- 分别用邻接矩阵和邻接表实现图的深度优先遍历和广度优先遍历_数据结构|图的邻接表与深度、广度优先搜索
线性存储元素时,元素的关系也同时确定了.而非线性数据结构就不同了,需要同时考虑存储数据元素和数据元素的关系. 由于图的结构比较复杂,任意两个顶点之间都可能存在联系,因此无法以数据元素在存储区中的物理位 ...
- 根据邻接表求深度优先搜索和广度优先搜索_深度优先搜索/广度优先搜索与java的实现...
度:某个顶点的度就是依附于该顶点的边的个数 子图:一幅图中所有边(包含依附边的顶点)的子集 路径:是由边顺序连接的一系列定点组成 环:至少含有一条边且终点和起点相同的路径 连通图:如果图中任一个到另一 ...
- PAT甲级1053 Path of Equal Weight (30分) :[C++题解]dfs求树的路径长度、邻接表
文章目录 题目分析 题目链接 题目分析 输入样例: 20 9 24 10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2 00 4 01 02 03 04 02 1 ...
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列.(同一个结点的同层邻接点,节点编号小的优先遍历) 输入 输入第一行为整数n(0< ...
最新文章
- python神奇时钟项目_Python Tkinter 数字时钟小项目
- PAT_B_1085_Java(25分)
- 未来如何与计算机和谐相处,未来计算机将进入身体和大脑,大脑将和云连接
- Java IdentityHashMap put()方法与示例
- Django组件--cookie与session
- php der格式 证书,php读取der格式证书乱码解决方法_PHP教程
- 傻瓜式一键破解MD5加密
- 通过举例彻底搞懂Matlab中max函数和min函数的用法(求最大值和最小值)
- 朴素贝叶斯-公园穿凉鞋问题的推导
- 深度学习项目:男女性别识别【附完整源码】
- Zend Studio 10.6.0正式版注册破解
- 如何修复excel文件损坏
- Remote 激光微粒计数器参数是什么
- 培训考试计算机系统,企业培训考试系统
- mysql特殊字段名称_Mysql必读MySQL创建带特殊字符的数据库名称方法示例
- Java设计模式面试题总结
- 伪类选择器:checked
- delphi Inc函数和Dec函数的用法
- powerpc linux交叉编译器,[转载]搭建PowerPC交叉编译器 二
- php 公众号 模板消息id如何获取_酒吧WYSIWYG演出模板集赞分享