【模板】tyvjP1520 树的直径 [2017年5月计划 清北学堂51精英班Day3]
描述
输入格式
第一行是一个正整数n,表示这棵树的结点数
接下来的n-1行,每行三个正整数a,b,w。表示结点a和结点b之间有一条边,长度为w
数据保证一定是一棵树,不必判错。
输出格式
第一行仅一个数,表示这棵树的最远距离
测试样例1
输入
4
1 2 10
1 3 12
1 4 15
输出
27
备注
40%的数据满足1<=n<=100
100%的数据满足1<=n<=10000 1<=a,b<=n 1<=w<=10000Tgotmacp
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))
#define lowbit(a) ((a) & (-(a))) int read()
{
int x = ;char ch = getchar();char c = ch;
while(ch > '' || ch < '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')return -x;
return x;
} const int INF = 0x3f3f3f3f;
const int MAXN = + ;
const int MAXE = MAXN * ; int head[MAXN];
int cnt;
int n;
int tmp1,tmp2,tmp3;
struct Edge
{
int u,v,w,next;
}edge[MAXE];
inline void insert(int a,int b,int c)
{
edge[++cnt] = Edge{a,b,c,head[a]};
head[a] = cnt;
}
inline void init()
{
n = read();
for(int i = ;i < n;i ++)
{
tmp1 = read();tmp2 = read();tmp3 = read();
insert(tmp1, tmp2, tmp3);
insert(tmp2, tmp1, tmp3);
}
} int queue[MAXN];
int ans;
bool b[MAXN];
int lenth[MAXN];//lenth[i]表示节点i到根节点的长度
int bfs(int root)
{
memset(queue, , sizeof(queue));
memset(b, , sizeof(b));
int head = ,tail = ;
int a = ;
queue[tail] = root;
lenth[root] = ;
b[root] = true; int mnode = root,m = ;
do
{
int x = queue[head];
head ++;
for(int pos = ::head[x];pos;pos = edge[pos].next)
{
int tmp = edge[pos].v;
if(!b[tmp])
{
b[tmp] = true;
queue[++tail] = tmp;
lenth[tmp] = lenth[x] + edge[pos].w;
if(lenth[tmp] > m)m = lenth[tmp],mnode = tmp;
a ++;
}
}
}while(a < n);
ans = max(ans, m);
return mnode;
}
inline void work()
{
bfs(bfs());
}
inline void shuchu()
{
printf("%d", ans);
}
int main()
{
init();
work();
shuchu();
return ;
}
方法2:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) > (b) ? (b) : (a))
#define lowbit(a) ((a) & (-(a))) int read()
{
int x = ;char ch = getchar();char c = ch;
while(ch > '' || ch < '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')return -x;
return x;
} const int INF = 0x3f3f3f3f;
const int MAXN = + ;
const int MAXE = MAXN * ; int head[MAXN];
int cnt;
int n;
int tmp1,tmp2,tmp3;
struct Edge
{
int u,v,w,next;
}edge[MAXE];
inline void insert(int a,int b,int c)
{
edge[++cnt] = Edge{a,b,c,head[a]};
head[a] = cnt;
}
inline void init()
{
n = read();
for(int i = ;i < n;i ++)
{
tmp1 = read();tmp2 = read();tmp3 = read();
insert(tmp1, tmp2, tmp3);
insert(tmp2, tmp1, tmp3);
}
} int queue[MAXN];
int ans;
bool b[MAXN];
int dp1[MAXN];//最长路
int dp2[MAXN];//次长路
int lenth[MAXN];//lenth[i]表示节点i到根节点的长度
void bfs(int root)
{
int head = ,tail = ;
int a = ;
queue[tail] = root;
lenth[root] = ;
b[root] = true; int mnode = root,m = ;
do
{
int x = queue[head];
head ++;
for(int pos = ::head[x];pos;pos = edge[pos].next)
{
int tmp = edge[pos].v;
if(!b[tmp])
{
b[tmp] = true;
queue[++tail] = tmp;
a ++;
}
}
}while(a < n);
for(int i = tail;i >= ;i --)
{
int x = queue[i];
if(!::head[i])
{
b[i] = false;
}
else
{
int m1 = ,m2 = ;
for(int pos = ::head[x];pos;pos = edge[pos].next)
{
int tmp = edge[pos].v;
if(!b[tmp])
{
if(m1 < dp1[tmp] + edge[pos].w)
{
m2 = m1;
m1 = dp1[tmp] + edge[pos].w;
}
else if(m2 < dp1[tmp] + edge[pos].w)
{
m2 = dp1[tmp] + edge[pos].w;
}
}
}
dp1[x] = m1;dp2[x] = m2;
}
b[x] = false;
ans = max(ans, dp1[x] + dp2[x]);
}
}
inline void work()
{
bfs();
}
inline void shuchu()
{
printf("%d", ans);
}
int main()
{
init();
work();
shuchu();
return ;
}
【模板】tyvjP1520 树的直径 [2017年5月计划 清北学堂51精英班Day3]的更多相关文章
- 【模板】倍增LCA [2017年5月计划 清北学堂51精英班 Day3]
P3379 [模板]最近公共祖先(LCA) 题目描述 如题,给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 输入输出格式 输入格式: 第一行包含三个正整数N.M.S,分别表示树的结点个数.询 ...
- 洛谷P1352 没有上司的舞会 [2017年5月计划 清北学堂51精英班Day3]
P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子 结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职 ...
- 【模板】KMP [2017年5月计划 清北学堂51精英班Day2]
Day2就搞一个KMP把 马拉车.AC自动机等准备省选的时候再说.. 模板题: 1204 寻找子串位置 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze ...
- 洛谷P1080 [NOIP2012提高组D1T2]国王游戏 [2017年5月计划 清北学堂51精英班Day1]
P1080 国王游戏 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右 手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排 ...
- 洛谷P1650 赛马[2017年5月计划 清北学堂51精英班Day1]
P1650 赛马 题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负者这 ...
- 洛谷P2258 子矩阵[2017年5月计划 清北学堂51精英班Day1]
题目描述 给出如下定义: 子矩阵:从一个矩阵当中选取某些行和某些列交叉位置所组成的新矩阵(保持行与列的相对顺序)被称为原矩阵的一个子矩阵. 例如,下面左图中选取第2.4行和第2.4.5列交叉位置的元素 ...
- 洛谷P2327 [SCOI2005]扫雷 [2017年5月计划 清北学堂51精英班Day1]
P2327 [SCOI2005]扫雷 题目描述 输入输出格式 输入格式: 第一行为N,第二行有N个数,依次为第二列的格子中的数.(1<= N <= 10000) 输出格式: 一个数,即第一 ...
- 【模板】 递归线段树 [2017年五月计划 清北学堂51精英班Day4]
P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
随机推荐
- 深入浅出 Java Concurrency (30): 线程池 part 3 Executor 生命周期[转]
我们知道线程是有多种执行状态的,同样管理线程的线程池也有多种状态.JVM会在所有线程(非后台daemon线程)全部终止后才退出,为了节省资源和有效释放资源关闭一个线程池就显得很重要.有时候无法正确的关 ...
- JavaScript事件(随笔)
1. Javascript事件介绍 JavaScript中的事件和现实生活中的事件类似,现实生活中发生的一些事情,例如:交通事件,当这些事情发生时,我们需要提供处理方案: 在JavaScript中事件 ...
- MyBatis与JPA的区别
参考博客: https://www.cnblogs.com/llywy/p/10103136.html https://www.jianshu.com/p/32ce87c163d6 MyBatis分为 ...
- Access数据库连接字符串
<connectionStrings> <add name="connStr" connectionString="server=.;uid=home; ...
- Vue报错——Unknown custom element: <shop-slide> - did you register the component correctly?
参考: https://blog.csdn.net/jiangyu1013/article/details/85676292 解决:除了import组件外,还要在components中添加 <t ...
- Hadoop构架概览
hadoop是一个开源的软件框架,是一个利用商业硬件处理和存储大型数据的软件.从下到上主要有五个主要的组成部分: 集群,是一套主机(节点)组成的.节点可以以机架划分.这个是硬件级别的构架. YARN构 ...
- 附录C 准备NCDC气象数据(加解释)
附录C 准备NCDC气象数据 这里首先简要介绍如何准备原始气象数据文件,以便我们能用Hadoop对它们进行分析.如果打算得到一份数据副本供Hadoop处理,可按照本书配套网站(网址为http://ww ...
- 阿里云HBase Ganos全新升级,推空间、时空、遥感一体化基础云服务
1.HBase Ganos是什么 Ganos是阿里云时空PaaS服务的自研核心引擎.Ganos已作为云数据库时空引擎与数据库平台融合,建立了以自研云原生数据库POALRDB为基础,联合NoSQL大数据 ...
- LUOGU P2962 [USACO09NOV]灯Lights
题目描述 Bessie and the cows were playing games in the barn, but the power was reset and the lights were ...
- 通过js 存取cookie
//存cookie function setCookie(name,value){ var Days = 30; var exp = new Date(); exp.setTime(exp.getTi ...