HDU3534(SummerTrainingDay13-C tree dp)
Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1574 Accepted Submission(s): 511
Problem Description
Input
Output
Sample Input
1 2 100
2 3 50
2 4 50
4
1 2 100
2 3 50
3 4 50
Sample Output
200 1
Source
//2017-08-16
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int INF = 0x3f3f3f3f; //链式前向星存图
int head[N], tot;
struct Edge{
int to, next, w; }edge[N<<]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void init(){
tot = ;
memset(head, -, sizeof(head));
} //dp[u]记录以u为根的子树,过u往下的最长路径。
//cnt[u]记录子树u上最长路径的数目。
int dp[N], cnt[N], ans, num; void dfs(int u, int fa){
dp[u] = ;
cnt[u] = ;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(v == fa)continue;
dfs(v, u);
if(dp[u]+dp[v]+w > ans){
ans = dp[u]+dp[v]+w;
num = cnt[u]*cnt[v];
}else if(dp[u]+dp[v]+w == ans)
num += cnt[u]*cnt[v];
if(dp[u] < dp[v]+w){
dp[u] = dp[v]+w;
cnt[u] = cnt[v];
}else if(dp[u] == dp[v]+w)
cnt[u] += cnt[v];
}
} int main()
{
//freopen("input.txt", "r", stdin);
int n;
while(scanf("%d", &n)!=EOF){
int u, v, w;
init();
for(int i = ; i < n-; i++){
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
add_edge(v, u, w); }
ans = -INF;
num = ;
dfs(, );
printf("%d %d\n", ans, num);
} return ; }
HDU3534(SummerTrainingDay13-C tree dp)的更多相关文章
- 96. Unique Binary Search Trees (Tree; DP)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- HDU 4359——Easy Tree DP?——————【dp+组合计数】
Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- TYOI Day1 travel:Tree dp【处理重复走边】
题意: 给你一棵树,n个节点,每条边有长度. 然后有q组询问(u,k),每次问你:从节点u出发,走到某个节点的距离mod k的最大值. 题解: 对于无根树上的dp,一般都是先转成以1为根的有根树,然后 ...
- HDU 4359 Easy Tree DP?
Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- Codeforces 442D Adam and Tree dp (看题解)
Adam and Tree 感觉非常巧妙的一题.. 如果对于一个已经建立完成的树, 那么我们可以用dp[ i ]表示染完 i 这棵子树, 并给从fa[ i ] -> i的条边也染色的最少颜色数. ...
- HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)
Tree chain problem Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- Partial Tree(DP)
Partial Tree http://acm.hdu.edu.cn/showproblem.php?pid=5534 Time Limit: / MS (Java/Others) Memory Li ...
- DP Intro - Tree DP Examples
因为上次比赛sb地把一道树形dp当费用流做了,受了点刺激,用一天时间稍微搞一下树形DP,今后再好好搞一下) 基于背包原理的树形DP poj 1947 Rebuilding Roads 题意:给你一棵树 ...
- HDU 5534/ 2015长春区域H.Partial Tree DP
Partial Tree Problem Description In mathematics, and more specifically in graph theory, a tree is an ...
随机推荐
- solr 下载 有dist目录的(6需要8)
http://archive.apache.org/dist/lucene/solr/ solr6 需要java8
- Xshell 配色方案
[wsp] text=cOc0c0 cyan(bold)=50ebfc text(bold)=9999e8 magenta=7b5175 green=008000 green(bold)=1cc470 ...
- git小乌龟工具TortoiseGit记住你的账号密码
在使用TortoiseGit的过程中,发下每次push或者pull都要重复输入账号密码,非常麻烦 怎么设置记住密码 在[系统盘]:\Users[你的用户名](比如C:\User\Administrat ...
- XAMPP中MySQL无法启动解决办法
如图 问题出在mysql的路径上,其实报错已经讲得听清楚了 预期应该是这样 结果却是这样 所以解决办法当然就是修改这个路径,出现这个报错原因大多因为之前电脑装过mysql,所以电脑默认启动是原来的my ...
- vue教程3-04 vue.js vue-devtools 调试工具的下载安装和使用
vue教程3-04 vue.js vue-devtools vue调试工具的安装和使用 一.vue-devtools 下载与安装 1.需要 fan qiang 2.打开谷歌浏览器设置--->扩展 ...
- Git使用(3)
1.查看本地和远程分支 git branch -a 删除本地分支 git branch -D branchName(D要大写) 删除远程分支 git push origin :branchName 2 ...
- Numpy 常用矩阵计算函数
基本属性 在做一些数据分析的时候,我们通常会把数据存为矩阵的形式,然后python本身对于矩阵的操作是不够的,因此出现了numpy这样一个科学开发库来进行python在次上面的不足. Numpy's ...
- C#基础篇十小练习
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace P03练 ...
- 如何精准实现OCR文字识别?
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云计算基础发表于云+社区专栏 前言 2018年3月27日腾讯云云+社区联合腾讯云智能图像团队共同在客户群举办了腾讯云OCR文字识别-- ...
- asp.net mvc 微信公众号token验证
本人的公众号要申请成为开发者,必须经过token认证.微信公众号的官方代码只列出了PHP代码的实例,明显是歧视.net用户.我用的asp.net mvc中的web api,结果调了好久都没有成功,最后 ...