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 ...
随机推荐
- git 入门与应用
git可视化界面的项目版本控制软件,适用于git项目管理 SourceTree 安装方法 https://blog.csdn.net/qq_26230421/article/details/79921 ...
- hive join on 条件 与 where 条件区别
1. select * from a left join b on a.id = b.id and a.dt=20181115; 2. select * from a left join b on a ...
- 在虚拟机上的ubuntu 1.6 系统中sudo apt-get失败的问题
在虚拟机上sudo apt-get update 失败.可能是网络dns问题,把nameserver \设为你路由器的内网ip地址就没事了; 详细: 1/打开sudo gedit /etc/resol ...
- cached_property的使用
cached_property修饰过的函数,变成是对象的属性,该对象第一次引用该属性时,会调用函数,对象第二次引用该属性时就直接从词典中取了,这也说明引用属性是经过__getattritue__. c ...
- java面试题复习(三)
21.静态嵌套类和内部类的不同? 答:静态嵌套类是被声明为静态(static)的内部类,它可以不依赖于外部类实例被实例化.而通常的内部类需要在外部类实例化后才能实例化.//还是考的static的知识 ...
- sublime text3 离线安装插件方法 package control
package control 在线安装 一般会出现各种错误 不推荐 离线安装 推荐step1: 打开package control官网https://packagecontrol.io/ step2 ...
- 路由对象route
路由对象是不可变 (immutable) 的,每次成功的导航后都会产生一个新的对象.不过你可以 watch (监测变化) 它. 通过 this.$route 访问当前路由,还可以通过router.ma ...
- python module -- sys
sys模块主要是用于提供对python解释器相关的操作 http://www.cnblogs.com/pycode/p/sysos.html http://blog.csdn.net/pipisorr ...
- 36_react_ui_antd
1:最流行的开源react ui组件库 1.1:material-ui(国外) 1.2:ant-design(推荐:国内蚂蚁金服) 2.如何使用 方式一(页面引入): 在<head>标签内 ...
- Java学习笔记 -- Java定时调度工具Timer类
1 关于 (时间宝贵的小姐姐请跳过) 本教程是基于Java定时任务调度工具详解之Timer篇的学习笔记. 什么是定时任务调度 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务. 在Ja ...