The Weight of Tree
来源:互联网 发布:增发纤维 知乎 编辑:程序博客网 时间:2024/06/07 02:08
http://acm.nyist.net/JudgeOnline/problem.php?pid=697
这是我们比赛一道题,当时不会做
说是给你n个节点的树,每个节点都有个权值,让你求出个子树的权值最大,树形dp,让我觉得只是道搜索
#include<stdio.h>#include<algorithm>#include<cstring>#include<iostream>using namespace std;#define MAXN 100006int dp[MAXN];bool B[MAXN];int top;typedef struct{ int v; int next;}Edge;typedef struct{ int head;}Head;Head H[MAXN];Edge E[MAXN];void Add_Edge(int u,int v){ E[top].v=v; E[top].next=H[u].head; H[u].head=top; top++;}void Read_Graph(int n){ int u,v; for(int i=1;i<n;i++){ cin>>u>>v; Add_Edge(u,v); Add_Edge(v,u); } return ;}void dfs(int u){ B[u]=true; for(int i=H[u].head;i!=-1;i=E[i].next){ int j=E[i].v; if(!B[j]){ dfs(j); if(dp[j]>0) dp[u]+=dp[j]; } }}int main(){ int ncase; cin>>ncase; while(ncase--){ memset(B,false,sizeof(B)); memset(H,-1,sizeof(H)); top=0; int n; cin>>n; for(int i=1;i<=n;i++) cin>>dp[i]; Read_Graph(n); dfs(1); int Max=dp[1]; for(int i=2;i<=n;i++) Max=max(Max,dp[i]); cout<<Max<<endl; } return 0;}
- The Weight of Tree
- NYOJ 679 The Weight of Tree 搜索+dp+邻接表
- Sichuan University Programming Contest 2011 Preliminary(for Non-SCUers) / B The Weight of Tree
- The height of binary tree
- CareerCup Find the diameter of the tree
- Path of Equal Weight
- [深度学习论文笔记][Weight Initialization] Understanding the difficulty of training deep feedforward neural
- NOJ1322求子集重量之和(Calculate the sum of a subset's weight)
- Algorithm: The implementation of Trie Tree (C++)
- The diameter of a binary tree
- hdu 3999The order of a Tree
- Hdu 3999 - The order of a Tree
- HDU3999:The order of a Tree
- HDU 3999 The order of a Tree
- The order of a Tree HDU
- CareerCup Binary Tree the Maximum of 人
- hdu 3999:The order of a Tree
- the longest distance of a binary tree
- js/jQuery简化编程(提取公共函数)
- 性能说明
- 小批量MD5解密
- linux进程控制块
- mysql存储过程之循环
- The Weight of Tree
- Android Fragments 详细使用
- 每个设计师应该阅读的8本书
- ISO8583报文协议(转)
- jquery.cookie 使用方法
- QT 设置QPushButton 颜色
- LibGdx--TexturePacker的使用
- usb 设备类代号
- JavaScript十进制、十六进制或者八进制转换