树形dp--hdu 3534 Tree
Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 906 Accepted Submission(s): 268
the edge is len.
1 2 100
2 3 50
2 4 50
4
1 2 100
2 3 50
3 4 50
200 1
题意:给出一颗树(n个顶点,n-1条边)
求最长的路径,以及最长路径的条数。
路径无非就是连接两个点直接的路。
因为是一颗树,所以连接两个点肯定是唯一的路径。
其实就是求两点间距离的最大值,以及这个最大值有多少个。
首先统计出结点到叶子结点的最长距离和次长距离。
然后找寻经过这个点的,在这个为根结点的子树中的最长路径个数目。
...思路参考于kuangbin博客
代码:
#include "stdio.h" //poj 3534 Tree 树形dp
#include "string.h" #define N 100100 struct node
{
int x,y;
int weight;
int next;
}edge[*N];
int idx,head[N]; void Init()
{
idx = ;
memset(head,-,sizeof(head));
} void Add(int x,int y,int weight)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = weight;
edge[idx].next = head[x];
head[x] = idx++;
} int maxn[N]; //最长距离
int smaxn[N];//次长距离
int maxn_num[N]; //最长距离数目
int smaxn_num[N]; //次长距离数目
int path[N]; //经过点i的最长距离长度
int num[N]; //经过点i的最长距离数目 void DFS(int x,int father)
{
int i,y;
maxn[x] = smaxn[x] = ;
maxn_num[x] = smaxn_num[x] = ;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
DFS(y,x);
if(maxn[x] < maxn[y]+edge[i].weight)
{
smaxn[x] = maxn[x];
smaxn_num[x] = maxn_num[x];
maxn[x] = maxn[y]+edge[i].weight;
maxn_num[x] = maxn_num[y];
}
else if(maxn[x]==maxn[y]+edge[i].weight)
maxn_num[x] += maxn_num[y];
else if(smaxn[x] < maxn[y]+edge[i].weight)
{
smaxn[x] = maxn[y]+edge[i].weight;
smaxn_num[x] = maxn_num[y];
}
else if(smaxn[x] == maxn[y]+edge[i].weight)
smaxn_num[x] += maxn_num[y];
}
if(maxn_num[x]==) //叶子节点,赋初值
{
maxn[x] = smaxn[x] = ;
maxn_num[x] = smaxn_num[x] = ;
path[x] = ;
num[x] = ;
return ;
}
int c1; //统计以x为根节点的最长距离数目
int c2; //统计以x为根节点的次长距离数目
c1 = c2 = ;
for(i=head[x]; i!=-; i=edge[i].next)
{
y=edge[i].y;
if(y==father) continue;
if(maxn[x] == maxn[y]+edge[i].weight)
c1++;
else if(smaxn[x] == maxn[y]+edge[i].weight)
c2++;
}
path[x] = ;
num[x] = ;
if(c1>=)
{
int tmp = ;
path[x] = maxn[x]*;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
if(maxn[x]==maxn[y]+edge[i].weight)
{
num[x] += tmp*maxn_num[y];
tmp += maxn_num[y];
}
}
}
else if(c1>= && c2>=)
{
path[x] = maxn[x]+smaxn[x];
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
if(maxn[x]==maxn[y]+edge[i].weight)
{
num[x] = smaxn_num[x]*maxn_num[y];
}
}
}
else
{
path[x] = maxn[x];
num[x] = maxn_num[x];
}
} int main()
{
int n;
int i,j;
int x,y,k;
while(scanf("%d",&n)!=EOF)
{
Init();
for(i=; i<n; ++i)
{
scanf("%d %d %d",&x,&y,&k);
Add(x,y,k);
Add(y,x,k);
}
DFS(,-);
int dist=,snum=;
for(i=; i<=n; ++i)
{
if(path[i]>dist)
{
dist = path[i];
snum = num[i];
}
else if(path[i]==dist)
{
snum += num[i];
}
}
printf("%d %d\n",dist,snum);
}
return ;
}
树形dp--hdu 3534 Tree的更多相关文章
- fwt优化+树形DP HDU 5909
//fwt优化+树形DP HDU 5909 //见官方题解 // BestCoder Round #88 http://bestcoder.hdu.edu.cn/ #include <bits/ ...
- 【树形dp】Apple Tree
[poj2486]Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10800 Accepted: 3 ...
- HDU 3534 Tree (经典树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534 题意: 给你一棵树,问你有多少对点的距离等于树的直径. 思路: dp[i][0]表示在i的子树中 ...
- HDU - 1054 Strategic Game(二分图最小点覆盖/树形dp)
d.一颗树,选最少的点覆盖所有边 s. 1.可以转成二分图的最小点覆盖来做.不过转换后要把匹配数除以2,这个待细看. 2.也可以用树形dp c.匈牙利算法(邻接表,用vector实现): /* 用ST ...
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
Colorful Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。
Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...
- hdu 5909 Tree Cutting [树形DP fwt]
hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...
随机推荐
- 2015腾讯暑期实习生 Web前端开发 面试经历
[2015腾讯暑期实习生 Web前端开发 面试经历] 好吧,首先声明,我被刷了,应该是跪在二面 微信查到的面试状态一直呈现复试中 .. 整整四天了.. 看来是没希望了 不过也是一次经历,记录一下还是可 ...
- css 布局absolute与relative的区别
absolute:当使用时,表示在文档流中没有实际存在位置(浮动),在不设置任何方位值时,只能按兵不动,当设置了方位值之后,会紧接着去寻找距离最近的能够将它包含住的父级元素,然后进行定位. relat ...
- 可以调整gif动画图片尺寸的很实用的php类
类的使用demo: <?php //http://www.cnblogs.com/roucheng/ require_once "roucheng.php"; $gr = n ...
- PHP入门:在Windows中安装PHP工作环境
PHP入门:在Windows系统中分别安装PHP工作环境 一.什么是LAMP? Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是 ...
- bootstrap dialog自行控制窗口的关闭
在使用dialog的时候,我们通常不希望点击btn的时候自动隐藏dialog,通常需要做一些清理或者ajax操作,在bootstrap dialog中,这是通过 data-dismiss=" ...
- 解决Spring MVC @ResponseBody返回html中中文字符串乱码问题
最近有个应用,通过responsebody返回完整的html页面时出现乱码是异常的问题,因为是通过responsebody返回,所以一开始设置了text/plain的字符集,如下: <mvc:a ...
- Sigleton 单例模式 的简单应用
需求:一个简单的后台java程序,收集信息,并将信息发送到远端服务器. 实现:实现一个后台线程,实时处理发送过来的信息,并将信息发送到服务器. 技术要点: 1.单例模式 2.队列 并没有实现全部代码, ...
- Sass学习之路(1)——Sass简介
Sass是CSS的一种预处理器语言,类似的语言还有Less,Stylus等. 那么什么是CSS预处理器? CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些 ...
- android Adapter剖析理解
UI控件都是跟Adapter(适配器)打交道的 Adapter: 是用来帮助控件填充数据的中间桥梁 (在开发中大多数Textview控件的内容是依靠数据库传递并显示的如:新闻类) Adapter: 将 ...
- ABAP:区别CALL SCREEN/SET SCREEN/LEAVE TO SCREEN
1,CALL SCREEN XXXX将在Screen调用栈(CALL STACK)上面添加一层调用(进栈),调用XXXX的PBO和PAI,如果XXXX的Next Screen不为0,那么将继续其Nex ...