【HDU 2196】 Computer(树的直径)
【HDU 2196】 Computer(树的直径)
题链http://acm.hdu.edu.cn/showproblem.php?pid=2196
这题可以用树形DP解决,自然也可以用最直观的方法解,先求出树的直径,那么树的任意节点的最远点必然是直径上的两个端点之一,证明可以通过反证法构造:
设端点为a,b;设任意点为i,假设存在一点c到i的距离大鱼i到a,b的距离,那么a与c又能形成一个距离更长的点对,与ab是直径的假设不符,因此不存在c,证明完成。
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 10000000000000
#define mod 1000000007
using namespace std;
ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=1e4+10;
const int M=2e4+10;
struct Edge{
int cost,to,nxt;
}Path[M];
int last[N],cnt;
void Addedge(int u,int v,int w){
Path[cnt]=(Edge){w,v,last[u]};
last[u]=cnt++;
}
int q[N*2],d[N],X;
void bfs(int x)
{
X=0;
int head=0,tail=1;
q[0]=x;
memset(d,0,sizeof(d));
d[x]=1;
while(head!=tail){
int cur=q[head];
head++;
if(d[cur]>d[X]) X=cur;
for(int i=last[cur];i;i=Path[i].nxt){
int v=Path[i].to;
if(!d[v]&&v!=x){
d[v]=d[cur]+Path[i].cost;
q[tail++]=v;
}
}
}
}
void Init()
{
memset(last,0,sizeof(last));
cnt=1;
}
int d2[N];
int main()
{
int n;
while(~scanf("%d",&n)){
Init();
for(int v=2;v<=n;v++){
int u=read(),w=read();
Addedge(u,v,w);
Addedge(v,u,w);
}
bfs(1);int ans1=X;
bfs(X);int ans2=X;
for(int i=1;i<=n;i++) d2[i]=d[i]-1;
bfs(X);
for(int i=1;i<=n;i++){
printf("%d\n",max(d[i]-1,d2[i]));
}
}
return 0;
}
【HDU 2196】 Computer(树的直径)的更多相关文章
- hdu 2196 Computer 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...
- HDU 2196 Computer (树dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...
- 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)
Problem Description A school bought the first computer some time ago(so this computer's id is 1). Du ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2196 Computer 树形DP经典题
链接:http://acm.hdu.edu.cn/showproblem.php? pid=2196 题意:每一个电脑都用线连接到了还有一台电脑,连接用的线有一定的长度,最后把全部电脑连成了一棵树,问 ...
- hdu 2196 computer
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 ...
随机推荐
- 标准字符cp功能
#include<stdio.h> #include<fcntl.h> int main(int argc,char *argv[]) { FILE *src_fp,*des_ ...
- JAVA 毕业设计 辅导
JAVA 毕业设计 辅导 =======================================================
- saltstack实战笔记
#运维管理工具 ansible #支持ssh,不需要客户端 saltstack #也是只是ssh,不需要客户端的 安装架构是,master /minion 安装salt的依赖模块 python zer ...
- Qt事件系统之一:Qt中的事件处理与传递
一.简介 在Qt中,事件作为一个对象,继承自 QEvent 类,常见的有键盘事件 QKeyEvent.鼠标事件 QMouseEvent 和定时器事件 QTimerEvent 等,与 QEvent 类的 ...
- [Usaco2003 Open]Lost Cows
Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular di ...
- _bzoj1500 [NOI2005]维修数列【真·Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 注意MAX-SUM的时候,不可以是空串. #include <cstdio> ...
- ORA-28002错误原因及解决办法
在oracle database 11g中,默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致.密码过期后,业务进程连接数据库异常,影响业务使用.数据库密码过 ...
- [转][ASP.NET MVC]如何定制Numeric属性/字段验证消息
本文转自:http://www.cnblogs.com/artech/archive/2012/02/13/NumericPropertyValidation.html 对于一个Numeric属性/字 ...
- Android开发学习——android与服务器端数据交互
1.首先搭建服务器端. 使用MyEclipse开发工具 public class MyServlet extends HttpServlet { @Override protected void do ...
- php安装ionCube