【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 ...
随机推荐
- const和readonly关键字
不知道大家对const和readonly这两个关键字的区别有什么了解,原来自己之前还真不清楚它们到底是怎么回事,那么如果你也不是很清楚的话,可以一起来探讨一下.在了解这两个关键字的时候我们先来了解一下 ...
- Scrapy使用问题整理(转载)
转载自:http://blog.csdn.net/heu07111121/article/details/50832999 最近尝试使用Scrapy进行数据抓取,并尝试在windows7 64位系 ...
- spring-cloud-config笔记
忽略元数据末尾 回到原数据开始处 spring-cloud-config 简单来讲就是spring-cloud实现的分布式配置中心.与之前介绍的开源配置服务方案 disconf是一样的,spring- ...
- 把一个base64编码的图片绘制到canvas (canvas的图片在转成dataurl)
把一个base64编码的图片绘制到canvas 需要引入jquery. <canvas id="myCanvas" width="800" height= ...
- unity, access standard shared emission by script
unity 5.1.1f1 personal 用下面方法在脚本中设置standard shader的emssion: gameObject.GetComponent<MeshRenderer&g ...
- 156. Merge Intervals【easy】
Given a collection of intervals, merge all overlapping intervals. Example Given intervals => me ...
- html 基本标签 ---字体
<b> </b>加粗 <i> </i> 斜体 <del> </del> 删除 <ins> </ins> ...
- python学习笔记1--python简介
Python翻译中文是蟒蛇.发明人guido喜欢蟒蛇马戏团,故起名python. python发展简史: --CNRI时期.CNRI资助Python发展的重要单位,python1.5版之前的成果大部分 ...
- centos7.4 install docker-ce
1.uninstall old version docker yum -y remove docker-common docker container-selinux docker-selinux d ...
- Check the difficulty of problems - poj 2151 (概率+DP)
有 T(1<T<=1000) 支队伍和 M(0<M<=30) 个题目,已知每支队伍 i 解决每道题目 j 的的概率 p[i][j],现在问:每支队伍至少解决一道题,且解题最多的 ...