hdu 2196(方法1:经典树形DP+方法2:树的直径)
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5194 Accepted Submission(s): 2620
school bought the first computer some time ago(so this computer's id is
1). During the recent years the school bought N-1 new computers. Each
new computer was connected to one of settled earlier. Managers of school
are anxious about slow functioning of the net and want to know the
maximum distance Si for which i-th computer needs to send signal (i.e.
length of cable to the most distant computer). You need to provide this
information.
Hint:
the example input is corresponding to this graph. And from the graph,
you can see that the computer 4 is farthest one from 1, so S1 = 3.
Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is
the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
file contains multiple test cases.In each case there is natural number N
(N<=10000) in the first line, followed by (N-1) lines with
descriptions of computers. i-th line contains two natural numbers -
number of computer, to which i-th computer is connected and length of
cable used for connection. Total length of cable does not exceed 10^9.
Numbers in lines of input are separated by a space.
1 1
2 1
3 1
1 1
2
3
4
4
///题意:求树上每个点到离它最远的点的距离.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 10050
using namespace std; struct Edge{
int u,v,w,next;
}edge[*N];
int head[N];
int dp[N][]; ///dp[i][0]代表以i为根子树中的最长路,dp[i][1]代表子树中的次长路,dp[i][2]代表父亲树中的最长路 void addEdge(int u,int v,int w,int &k){
edge[k].u = u,edge[k].v = v,edge[k].w = w;
edge[k].next = head[u],head[u]=k++;
} void dfs(int u,int fa){ ///找子树中的最大值和次大值
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(v==fa) continue;
//printf("%d\n",v);
dfs(v,u);
if(dp[u][]<dp[v][]+edge[k].w){
dp[u][] = dp[v][] + edge[k].w;
if(dp[u][]>dp[u][]){
swap(dp[u][],dp[u][]);
}
}
}
}
void dfs1(int u,int fa){///找从父亲延伸过去的最大值
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w = edge[k].w;
if(v==fa) continue;
dp[v][] = max(dp[u][] , dp[v][]+edge[k].w==dp[u][]?dp[u][]:dp[u][]) + edge[k].w;
dfs1(v,u);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
int tot=;
for(int u=;u<=n;u++){
int v,w;
scanf("%d%d",&v,&w);
addEdge(u,v,w,tot);
addEdge(v,u,w,tot);
}
dfs(,-);
dfs1(,-);
for(int i=;i<=n;i++){
printf("%d\n",max(dp[i][],dp[i][]));
}
}
return ;
}
方法二:先对任意一个点进行搜索的到离它最远的端点,这个点必定是树的直径(树的直径指树中的最长路)的其中一个端点,然后以这个端点开始又进行搜索,得到一个离他最远
的店,这个点是直径的另外一个端点,我们在找的时候分别更新所有点到两个端点的距离,对于每个点我们区大值就是结果。
///题意:求树上每个点到离它最远的点的距离.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define N 10050
using namespace std; struct Edge{
int u,v,w,next;
}edge[*N];
int head[N]; void addEdge(int u,int v,int w,int &k){
edge[k].u = u,edge[k].v = v,edge[k].w = w;
edge[k].next = head[u],head[u]=k++;
}
int dis[N],dis1[N];
int vis[N];
void BFS(int x){
memset(vis,,sizeof(vis));
queue<int> q;
q.push(x);
vis[x]=;
while(!q.empty()){
int t = q.front();
q.pop();
for(int k=head[t];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(!vis[v]){
vis[v]=;
q.push(v);
dis[v] =max(dis[v],dis[t]+w);
}
}
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
memset(dis,,sizeof(dis));
memset(dis1,,sizeof(dis1));
int tot=;
for(int u=;u<=n;u++){
int v,w;
scanf("%d%d",&v,&w);
addEdge(u,v,w,tot);
addEdge(v,u,w,tot);
}
BFS();
int START=,END=;
int len = -;
for(int i=;i<=n;i++) {
if(dis[i]>len) {len = dis[i],START = i;}
}
memset(dis,,sizeof(dis));
BFS(START);
len = -;
for(int i=;i<=n;i++) {
dis1[i]=dis[i];
if(dis[i]>len) {len = dis[i],END = i;}
}
memset(dis,,sizeof(dis));
BFS(END);
for(int i=;i<=n;i++){
printf("%d\n",max(dis[i],dis1[i]));
} }
return ;
}
hdu 2196(方法1:经典树形DP+方法2:树的直径)的更多相关文章
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- HDU 4616 Game(经典树形dp+最大权值和链)
http://acm.hdu.edu.cn/showproblem.php?pid=4616 题意:给出一棵树,每个顶点有权值,还有存在陷阱,现在从任意一个顶点出发,并且每个顶点只能经过一次,如果经过 ...
- hdu 1054 Strategic Game 经典树形DP
Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj3534(树形dp,求树的直径的条数)
题目链接:https://vjudge.net/problem/HDU-3534 题意:给出一棵树,求树上最长距离(直径),以及这样的距离的条数. 思路:如果只求直径,用两次dfs即可.但是现在要求最 ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- HDU 1561 The more, The Better 经典树形DP
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 3534 Tree (经典树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3534 题意: 给你一棵树,问你有多少对点的距离等于树的直径. 思路: dp[i][0]表示在i的子树中 ...
随机推荐
- 微信小程序—setTimeOut定时器的坑
原文地址: http://fanjiajia.cn/2018/06/27/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E2%80%94setTimeOu ...
- Packet filtering with Linux & NAT
http://www.linuxfocus.org/ChineseGB/May2003/article289.shtml Gateway, Proxy-Arp 和 Ethernet Bridge ? ...
- 【SSH】——Hibernate实现简单的自动建表
[与ORM] Object Relational Mapping,对象关系映射,将对象和关系联系了起来.面向对象是从耦合.聚合.封装等的基础上发展起来的,而关系数据库则是从数学理论发展而来的,两套理论 ...
- jsp实用过滤器写法
使用过滤器来给servlet设置编码 public class CharacterEncodingFilter implements Filter{ @Override public void des ...
- 魔法物品(magic.pas/c/cpp)
有两种类型的物品:普通物品和魔法物品.普通物品没有魔法属性,而魔法物品拥有一些魔法属性.每种普通物品有一个价值P,但每种魔法物品有两种价值:鉴定前的价值P.和鉴定后的价值P2(当然,P2总是大于P.) ...
- c# 以多个字符串分隔字符串数据 分组 分隔 split 正则分组
string str="aaa[##]ccc[##]ddd[##]bb" Regex regex=new Regex("[##]");//以 [##] 分割 s ...
- BZOJ2097: [Usaco2010 Dec]Exercise 奶牛健美操 贪心+伪树dp+二分
//论全局变量的杀伤力....QAQ#include<cstdio> #include<iostream> #include<cstdlib> #include&l ...
- bzoj2724: [Violet 6]蒲公英 分块 区间众数 论algorithm与vector的正确打开方式
这个,要处理各个数的话得先离散,我用的桶. 我们先把每个块里的和每个块区间的众数找出来,那么在查询的时候,可能成为[l,r]区间的众数的数只有中间区间的众数和两边的数. 证明:若不是这里的数连区间的众 ...
- pmap用法小计
By francis_hao Aug 4,2017 pmap-报告进程的内存映射. 概要 pmap [options] pid [...] 描述 pmap命令用来报告一个或多个进程的 ...
- 几种不同的json格式解析
转连接: http://blog.csdn.net/whx405831799/article/details/42171191 内容很好 给服务端发送请求后,服务端会返回一连串的数据,这些数据在大部分 ...