【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 ...
随机推荐
- Kubernetes集群认证
1.集群搭建:https://www.kubernetes.org.cn/3808.html 2.集群验证:https://www.kubernetes.org.cn/1861.html
- Zznu 1913: yifan and matrix (多路归并)
题目链接: 1913: yifan and matrix 题目描述: 有一个n*n的矩阵,在每一行取出一个数,可以得到n个数的和,问前n小的和分别是多少? 解题思路: 对于两个数组a[n],b[n], ...
- 暴力 hihoCoder 1178 计数
题目传送门 /* 暴力:这题真是醉了,直接暴力竟然就可以了!复杂度不会分析,不敢写暴力程序.. 枚举x,在不重复的情况下+ans,超过范围直接break */ #include <cstdio& ...
- ACM_给你100块钱
给你100块钱 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小光见到昨晚旭能神没拿到一血,又损失了一百块,很同情他.但是为了不 ...
- 题解报告:hdu 2030 汉字统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2030 Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先 ...
- Android 线程池系列教程(1)目录
Sending Operations to Multiple Threads 1.Dependencies and prerequisites Android 3.0 (API Level 11) o ...
- 【原】无脑操作:Eclipse + Maven + jFinal + MariaDB 环境搭建
一.开发环境 1.windows 7 企业版 2.Eclipse IDE for Enterprise Java Developers Version: 2019-03 (4.11.0) 3.JDK ...
- SQL常用自定义函数
1.字符串转Table(Func_SplitToTable) CREATE FUNCTION [dbo].[Func_SplitToTable] ( @SplitString ...
- Farseer.net轻量级ORM开源框架 V1.2.1版本升级消息
提交版本V1.2.11.修复实体未设置主键时,无法找到主键ID字段,改为无主键时默认为"ID”字段2.新增:SqlServer2000Provider数据库驱动3.新增:DbContextI ...
- (转)SpringMVC学习(十一)——SpringMVC实现Resultful服务
http://blog.csdn.net/yerenyuan_pku/article/details/72514034 Restful就是一个资源定位及资源操作的风格,不是标准也不是协议,只是一种风格 ...