树形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的方案数 ...
随机推荐
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Ext.GridPanel 用法总结(一)—— Grid基本用法
Ext.GridPanel 用法总结(一)—— Grid基本用法 摘自:http://www.cnblogs.com/luluping/archive/2009/08/01/1536645.html ...
- sencha panel的头header上添加刷新按钮
var plet3=Ext.create('portaltest3.view.Portlet', { title: '提醒', ...
- C#中弹出文件选择窗体和判断是否下载提示窗体的源码
1.创建一个window窗体
- Fluent NHibernate and Spring.net
http://blog.bennymichielsen.be/2009/01/04/using-fluent-nhibernate-in-spring-net/ http://comments.gma ...
- 基于 ANSIBLE 自动化运维实践
摘要:运维这个话题很痛苦,你做任何的产品都离不开运维.不管你用什么语言.什么平台.什么技术,真正能够决定你产品成熟度的很有可能就是你运维的能力.取自 云巴 CEO 张虎在 ECUG 大会上的分享. 云 ...
- 西邮Linux兴趣小组2016免试题
4.28的宣讲会圆满结束(就在写这段话之前不久),对于西邮Linux兴趣小组这一次纳新,身为局外人表示:还是有历史,还是会玩,还是厉害哈. 华丽的分割线里面是自己之前的攻关战略,最后补充了宣讲会上学长 ...
- C#在图片上添加文字代码
创建.NET WinForm程序,设置项目的默认命名空间为Keleyi.Com,在窗体上添加一个PictureBox控件pictureBox_keleyi_com和一个Button控件button_A ...
- javascript中apply()和call()方法的区别
一.方法的定义 call方法: 语法:call(thisObj,Object)定义:调用一个对象的一个方法,以另一个对象替换当前对象.说明:call 方法可以用来代替另一个对象调用一个方法.call ...