HDU 2196.Computer 树形dp 树的直径
Computer
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31049 Accepted Submission(s): 3929
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.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
#define PI acos(-1.0)
#define eps 1e-8
typedef long long ll;
typedef pair<int,int > P;
const int N=1e5+,M=1e5+;
const int inf=0x3f3f3f3f;
const ll INF=1e18+,mod=1e9+;
struct edge
{
int from,to;
ll w;
int next;
};
edge es[M];
int cnt,head[N];
ll dp[N][];
void init()
{
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,ll w)
{
cnt++;
es[cnt].from=u,es[cnt].to=v;
es[cnt].w=w;
es[cnt].next=head[u];
head[u]=cnt;
}
void dfs1(int u,int fa)
{
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa) continue;
dfs1(e.to,u);
ll d=dp[e.to][]+e.w;
if(d>dp[u][]) swap(d,dp[u][]);
if(d>dp[u][]) swap(d,dp[u][]);
}
}
void dfs2(int u,int fa)
{
for(int i=head[u]; i!=-; i=es[i].next)
{
edge e=es[i];
if(e.to==fa) continue;
if(dp[u][]==dp[e.to][]+e.w)
dp[e.to][]=max(dp[u][],dp[u][])+e.w;
else
dp[e.to][]=max(dp[u][],dp[u][])+e.w;
dfs2(e.to,u);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=,j; i<=n; i++)
{
ll w;
scanf("%d%lld",&j,&w);
addedge(i,j,w);
addedge(j,i,w);
}
memset(dp,,sizeof(dp));
dfs1(,);
dfs2(,);
for(int i=; i<=n; i++)
printf("%d\n",max(dp[i][],dp[i][]));
}
return ;
}
树形dp
HDU 2196.Computer 树形dp 树的直径的更多相关文章
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- computer(树形dp || 树的直径)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2196 Computer 树形DP 经典题
给出一棵树,边有权值,求出离每一个节点最远的点的距离 树形DP,经典题 本来这道题是无根树,可以随意选择root, 但是根据输入数据的方式,选择root=1明显可以方便很多. 我们先把边权转化为点权, ...
- hdu-2169 Computer(树形dp+树的直径)
题目链接: Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 2196 Computer(树形DP)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2196 Computer 树形dp模板题
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2196 Computer(树形DP经典)
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Computer(HDU2196+树形dp+树的直径)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2196 题目: 题意:有n台电脑,每台电脑连接其他电脑,第i行(包括第一行的n)连接u,长度为w,问你每 ...
- hdu 4607 树形dp 树的直径
题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n)个点,至少需要走多少距离(每条边的距离是1): 思路:树形dp求树的直径r: a:若k<=r+1 ...
随机推荐
- 2. spring定时任务
1.添加命名空间 <!-- 定时注解驱动 --> <task:annotation-driven /> <!-- 进行定时任务的类,将其定义为一个bean --> ...
- 如何用Bat批处理自制自解压文件
转载▼http://blog.sina.com.cn/s/blog_48462a890102e0nu.html 1.在桌面上新建一个文本文档,如:“新建 文本文档.txt”,方法是:在桌面的空 ...
- Python第7天
其他内置函数: abs() 绝对值 all()均为真则为True any()有一个为真就为True bin()十进制->二进制 bool() 空,0,None为False,其余为True byt ...
- pytorch安装 caffe2 安装:git 慢 caffe2 cannot find -lopencv_dep_cudart ,undefined reference to 'pthread_create'
最开始的问题: caffe2目前已经不维护了.怎么编译都报错,并且有third_party的代码链接已经飞了.下载不全代码了.建议 不要再折腾了,直接安装Pytorch. 首先,基本环境是ubuntu ...
- 【398】COMP9021 - Polynomial
构建 Polynomial 类,实现 +, -, , / and +=, -=, =, /= 参考:如何用python编程求解二元一次方程组.如x+y=3;x-y=1 参考:python对重载运算符的 ...
- css3:神秘的弹性盒子flexbox
请先运行demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- 1506.01186-Cyclical Learning Rates for Training Neural Networks
1506.01186-Cyclical Learning Rates for Training Neural Networks 论文中提出了一种循环调整学习率来训练模型的方式. 如下图: 通过循环的线 ...
- Django中的auth模块
from django.contrib import auth authenticate() 提供了用户认证功能,即验证用户名以及密码是否正确,一般需要username .password两个关键字参 ...
- scrapy 爬取豆瓣互联网图书
安装scrapy conda install scrapy 生成一个scrapy项目 scrapy startproject douban settings文件 # -*- coding: utf-8 ...
- Excel基本操作
一.excel公式下拉 1.选择最长一列excel ,按CTRL+↓移到最后一个单元格,2光标移到下拉公式的那一列,3.再按CTRL+SHIFT+↑,4.再按CTRL+D 二.输入身份证号等长数字 方 ...