http://www.lydsy.com/JudgeOnline/problem.php?id=3124

第一问:

dfs1、dfs2

dfs2中记录dis[i]表示点i距离最长链左端点的距离

第二问:

所有直径的交集一定是最长链上连续的一段

dfs3记录最长链,

从最长链上每个点i开始dfs4,记录能到达的非最长链点的最远距离mx

如果mx==最长链-dis[i],更新交集的左端点

如果mx==dis[i],找到交集的右端点,退出

#include<cstdio>
#include<iostream>
#include<algorithm> using namespace std; typedef long long LL;
#define N 200001 int tot;
int front[N],nxt[N<<],to[N<<],val[N<<]; LL mx,max_len;
int wh; LL dis[N]; int path[N],num; bool use[N]; int cf[N]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v,int w)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w;
} void dfs1(int x,int y,LL d)
{
if(d>mx) mx=d,wh=x;
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==y) continue;
dfs1(to[i],x,d+val[i]);
}
} void dfs2(int x,int y,LL d)
{
dis[x]=d;
if(d>max_len) max_len=d,wh=x;
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==y) continue;
dfs2(to[i],x,d+val[i]);
}
} void dfs3(int x,LL d)
{
path[++num]=x;
use[x]=true;
for(int i=front[x];i;i=nxt[i])
{
if(dis[to[i]]==d-val[i]) dfs3(to[i],d-val[i]);
}
} void dfs4(int x,int y,LL d)
{
mx=max(mx,d);
for(int i=front[x];i;i=nxt[i])
{
if(to[i]==y || use[to[i]]) continue;
dfs4(to[i],x,d+val[i]);
}
} int main()
{
int n;
read(n);
int u,v,w;
for(int i=;i<n;++i)
{
read(u); read(v); read(w);
add(u,v,w);
}
dfs1(,,);
dfs2(wh,,);
cout<<max_len<<'\n';
dfs3(wh,max_len);
int L=,R=num;
for(int i=;i<num;++i)
{
mx=;
dfs4(path[i],,);
if(mx==max_len-dis[path[i]]) L=i;
if(mx==dis[path[i]]) { R=i; break; }
}
cout<<R-L;
}

3124: [Sdoi2013]直径

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 1261  Solved: 603
[Submit][Status][Discuss]

Description

小Q最近学习了一些图论知识。根据课本,有如下定义。树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度。如果一棵树有N个节点,可以证明其有且仅有N-1 条边。 路径:一棵树上,任意两个节点之间最多有一条简单路径。我们用 dis(a,b)
表示点a和点b的路径上各边长度之和。称dis(a,b)为a、b两个节点间的距离。  
 直径:一棵树上,最长的路径为树的直径。树的直径可能不是唯一的。 
现在小Q想知道,对于给定的一棵树,其直径的长度是多少,以及有多少条边满足所有的直径都经过该边。

Input

第一行包含一个整数N,表示节点数。 
接下来N-1行,每行三个整数a, b, c ,表示点 a和点b之间有一条长度为c
的无向边。

Output

共两行。第一行一个整数,表示直径的长度。第二行一个整数,表示被所有
直径经过的边的数量。

Sample Input

6
3 1 1000
1 4 10
4 2 100
4 5 50
4 6 100

Sample Output

1110
2

【样例说明】
直径共有两条,3 到2的路径和3到6的路径。这两条直径都经过边(3, 1)和边(1, 4)。

HINT

对于100%的测试数据:2≤N≤200000,所有点的编号都在1..N的范围内,

边的权值≤10^9。

bzoj千题计划134:bzoj3124: [Sdoi2013]直径的更多相关文章

  1. bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块

    http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...

  2. bzoj千题计划267:bzoj3129: [Sdoi2013]方程

    http://www.lydsy.com/JudgeOnline/problem.php?id=3129 如果没有Ai的限制,就是隔板法,C(m-1,n-1) >=Ai 的限制:m减去Ai &l ...

  3. bzoj千题计划133:bzoj3130: [Sdoi2013]费用流

    http://www.lydsy.com/JudgeOnline/problem.php?id=3130 第一问就是个最大流 第二问: Bob希望总费用尽量大,那肯定是把所有的花费加到流量最大的那一条 ...

  4. bzoj千题计划268:bzoj3131: [Sdoi2013]淘金

    http://www.lydsy.com/JudgeOnline/problem.php?id=3131 如果已知 s[i]=j 表示有j个<=n数的数码乘积=i 那么就会有 s[a1]*s[a ...

  5. bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器

    http://www.lydsy.com/JudgeOnline/problem.php?id=3122 等比数列求和公式+BSGS #include<map> #include<c ...

  6. bzoj千题计划258:bzoj3123: [Sdoi2013]森林

    http://www.lydsy.com/JudgeOnline/problem.php?id=3123 启发式合并主席树 #include<cmath> #include<cstd ...

  7. bzoj千题计划196:bzoj4826: [Hnoi2017]影魔

    http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...

  8. bzoj千题计划121:bzoj1033: [ZJOI2008]杀蚂蚁antbuster

    http://www.lydsy.com/JudgeOnline/problem.php?id=1033 经半个下午+一个晚上+半个晚上 的 昏天黑地调代码 最终成果: codevs.洛谷.tyvj上 ...

  9. bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪

    http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...

随机推荐

  1. Alpha 冲刺报告3

    队名 massivehard 组员一(组长:晓辉) 今天完成了哪些任务 .整理昨天的两个功能,补些bug 写了一个初步的loyaut github 还剩哪些任务: 后台的用来处理自然语言的服务器还没架 ...

  2. Week-2-作业1

    第一章 概论 1.什么是程序? 答:在学习软件工程导论前,我们已经学习了一些计算机语言和数据结构这样的课程,并深刻的知道“程序=数据结构+算法”,但在学习中还是会产生如书中1.1讲所提到的那些疑问,二 ...

  3. oh my god 四则运算

    Week1地址:https://git.coding.net/leiqh549/four.git 需求分析: 1.一个生成n道四则运算的程序,要求数字在0-100间,运算符在3-5个之间且运算符至少包 ...

  4. 在虚拟机中安装Ubuntu详细过程

    参考:http://blog.csdn.net/u013142781/article/details/50529030

  5. Android Toast的多功能封装——Android开发之路1

    Android封装实现各种功能的Toast GitHub地址:https://github.com/SibreiaDante/ToastUtils 效果图: 方法封装如下: showSingleton ...

  6. ISCC2018(misc)

    ISCC2018 misc writeup(部分) 这些天做个了iscc题目,有些题目不是很难,网上都有相同的题或者类似的题目,但是我很菜,没做出来多少. #misc1:Where is the FL ...

  7. 微信小程序使用函数的三种方法

    使用来自不同页面的函数 函数写在util.js页面 function formatTime(date) { var year = date.getFullYear() var month = date ...

  8. [转帖] sqlserver CAL 授权模式下 只能够有20个core的使用问题

    http://www.cnblogs.com/diabloxl/p/3623640.html?utm_source=tuicool&utm_medium=referral 公司这边性能组老师进 ...

  9. Qss 样式表的尝试

    QLineEdit{ border:1px solid #137eb6; padding:2px; background-color:#F5F5F5; } QToolTip{ border:1px s ...

  10. springmvc接收date类型参数

    springmvc在表单提交接收date类型参数的时候会报错:Cannot convert value of type [java.lang.String] to required type [jav ...