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 ...
随机推荐
- Spring Boot中使用Swagger2构建RESTful API文档
在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...
- Shell - 简明Shell入门04 - 判断语句(If)
示例脚本及注释 #!/bin/bash var=$1 # 将脚本的第一个参数赋值给变量var if test $var # test - check file types and compare va ...
- linq查询时查询语句中附带多个查询时“已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭”
主要原因是因为EF采用的 DataReader来进行数据的存储,此时connection使用的是同一个. 例如: list = _tzNewsService.GetAll().Where(w => ...
- Ubuntu 14.04TLS和CentOS-6(64bit)上安装Nginx
Ubuntu 14.04上安装Nginx [参考地址]https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on- ...
- 安装Centos7 随手记
1.老笔记本安装Centos7 配置:酷睿I3 内存8G 2.原有系统Win7 将原来的硬盘空间,调整出60G 给Centos7 用. 3.安装Centos7 图形介面的,和windows安装过程类 ...
- PyCharm下载与激活
1.集成开发环境(IDE:Integrated Development Environment)PyCharm下载地址:https://www.jetbrains.com/pycharm/downlo ...
- Storm的acker确认机制
Storm的acker消息确认机制... ack/fail消息确认机制(确保一个tuple被完全处理) 在spout中发射tuple的时候需要同时发送messageid,这样才相当于开启了消息确认机制 ...
- php -- 获取函数参数
----- 015-parameter.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv=&q ...
- 《松本行弘的程序世界》读书笔记(上)——面向对象、程序块、设计模式、ajax
1. 前言 半个月之前买了这本书,还是经园子里的一位网友推荐的.到现在看了一半多,基础的都看完了,剩下的几章可做高级部分来看.这本书看到现在,可以说感触很深,必须做一次读书笔记! 关于这本书,不了解的 ...
- Vue笔记:在项目中使用 SCSS
背景概述 1. CSS预处理器 css预处理器定义了一种新的编程语言,编译后成正常的CSS文件.为CSS增加一些编程的特性,无需考虑浏览器的兼容问题,让CSS更加简洁,适应性更强,可读性更佳,更易于代 ...