【BZOJ1509】[NOI2003]逃学的小孩 直径
【BZOJ1509】[NOI2003]逃学的小孩
Description

Input
Output
Sample Input
1 2 1
2 3 1
3 4 1
Sample Output
题解:显然A和B是直径的两端点吧~(自己瞎证一下,或者瞎yy一下就行)
然后枚举C就做完了。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=200010;
typedef long long ll;
int n,cnt,r1,r2,rt;
ll ans;
int to[maxn<<1],next[maxn<<1],head[maxn];
ll val[maxn<<1],dep[maxn<<1],f[maxn<<1];
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
inline void add(int a,int b,int c)
{
to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x,int fa)
{
if(dep[x]>dep[rt]) rt=x;
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa) dep[to[i]]=dep[x]+val[i],dfs(to[i],x);
}
int main()
{
n=rd(),rd();
int i,a,b,c;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++) a=rd(),b=rd(),c=rd(),add(a,b,c),add(b,a,c);
dfs(1,0),r1=rt;
dep[r1]=0,dfs(r1,0),r2=rt,memcpy(f,dep,sizeof(dep));
dep[r2]=0,dfs(r2,0);
for(i=1;i<=n;i++) ans=max(ans,dep[r1]+min(dep[i],f[i]));
printf("%lld",ans);
return 0;
}
【BZOJ1509】[NOI2003]逃学的小孩 直径的更多相关文章
- BZOJ1509: [NOI2003]逃学的小孩(树的直径)
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1126 Solved: 567[Submit][Status][Discuss] Description ...
- BZOJ1509 [NOI2003]逃学的小孩 树型DP
题目: 分析: 首先明确我们是要求 min(dist[C][A],dist[C][B])+dist[A][B]. 我们把C当成树根,第一我们可以发现min里面取dist[C][A]或者dist[C][ ...
- BZOJ1509 NOI2003 逃学的小孩
Description: Input: 第一行是两个整数N(3 N 200000)和M,分别表示居住点总数和街道总数.以下M行,每行给出一条街道的信息.第i+1行包含整数Ui.Vi.Ti(1 ...
- BZOJ1509: [NOI2003]逃学的小孩 (树形DP)
题意:给一棵树 选三个点A,B,C 求A到B的再从B到C的距离最大值 需要满足AB的距离小于AC的距离 题解:首先树上的最大距离就想到了直径 但是被样例误导了TAT BC两点构成了直径 我一开始以为A ...
- [NOI2003]逃学的小孩(树的直径)
[NOI2003]逃学的小孩 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?"一听 ...
- BZOJ 1509: [NOI2003]逃学的小孩( 树形dp )
树形dp求出某个点的最长3条链a,b,c(a>=b>=c), 然后以这个点为交点的最优解一定是a+2b+c.好像还有一种做法是求出树的直径然后乱搞... ----------------- ...
- [NOI2003]逃学的小孩【观察+树的直径】
Online Judge:Bzoj1509,Luogu P4408 Label:观察,树的直径 题目描述 输入 第一行是两个整数N(\(3≤N≤200000\))和M,分别表示居住点总数和街道总数.以 ...
- LUOGU P4408 [NOI2003]逃学的小孩(树的直径)
题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:“喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?”一听说要考试,Chris的父母就心急如焚,他们决定在尽 ...
- BZOJ1509 & 洛谷4408:[NOI2003]逃学的小孩——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1509 https://www.luogu.org/problemnew/show/P4408 sb ...
随机推荐
- C# 字符串数组转换为整形数组
/// <summary> /// 字符串数组转换整形数组 /// </summary> /// <param name="Content">字 ...
- test_login
import unittest,requestsimport ddtfrom BeautifulReport import BeautifulReport as bffrom urllib impor ...
- linux sudo命令详解
--sudo命令重启网卡 sudo service network restart http://bestchenwu.iteye.com/blog/1450292
- DOS命令 bat-call的用法
call 从批处理程序调用另一个批处理程序 call有几种用法 第一种用法,也就是最常用的一种,调用另一个批处理,在被调用的批处理执行完后在执行call下面的命令.如: @echo off call ...
- laydate 和 Vue 奇怪的清空问题
laydate的input,会自动被清空,当别的input修改的时候.改成这样既可解决 <td><input type="text" id="retur ...
- 将ViewState放在Session里(转载)
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Ent ...
- Atitit.swt 线程调用ui控件的方法
Atitit.swt 线程调用ui控件的方法 1 SwingUtilities.invokeLater1 2 display.asyncExec方法1 3 display.timerExec(500 ...
- 绝不能错过的10款最新OpenStack网络运维 & 监控工具
摘要 今天我们要推荐给大家的是关于奥斯汀OpenStack Summit的OpenStack网络方面功能与工具相关的技术演讲. 希望可以帮助国内的开发者.架构师和用户更好地了解OpenStack在SD ...
- hMailServer之允许用户自己修改密码
使用hMailServer搭建邮件系统,使用webmail实现web收发邮件,但是又个问题是在webmail中用户自己无法修改密码. 可以使用hMailServer自带的PhpWebAdmin来实现让 ...
- Qt 槽函数的使用
今天在代码中遇到这样一个问题,自己感觉槽和函数都写的没错,但是就是不执行槽函数,因为是一个定时器的使用,即定时时间到了就执行槽函数. SeventhWizardPage::SeventhWizardP ...