bzoj千题计划134:bzoj3124: [Sdoi2013]直径
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
3 1 1000
1 4 10
4 2 100
4 5 50
4 6 100
Sample Output
2
【样例说明】
直径共有两条,3 到2的路径和3到6的路径。这两条直径都经过边(3, 1)和边(1, 4)。
HINT
对于100%的测试数据:2≤N≤200000,所有点的编号都在1..N的范围内,
边的权值≤10^9。
bzoj千题计划134:bzoj3124: [Sdoi2013]直径的更多相关文章
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...
- bzoj千题计划267:bzoj3129: [Sdoi2013]方程
http://www.lydsy.com/JudgeOnline/problem.php?id=3129 如果没有Ai的限制,就是隔板法,C(m-1,n-1) >=Ai 的限制:m减去Ai &l ...
- bzoj千题计划133:bzoj3130: [Sdoi2013]费用流
http://www.lydsy.com/JudgeOnline/problem.php?id=3130 第一问就是个最大流 第二问: Bob希望总费用尽量大,那肯定是把所有的花费加到流量最大的那一条 ...
- bzoj千题计划268:bzoj3131: [Sdoi2013]淘金
http://www.lydsy.com/JudgeOnline/problem.php?id=3131 如果已知 s[i]=j 表示有j个<=n数的数码乘积=i 那么就会有 s[a1]*s[a ...
- bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器
http://www.lydsy.com/JudgeOnline/problem.php?id=3122 等比数列求和公式+BSGS #include<map> #include<c ...
- bzoj千题计划258:bzoj3123: [Sdoi2013]森林
http://www.lydsy.com/JudgeOnline/problem.php?id=3123 启发式合并主席树 #include<cmath> #include<cstd ...
- bzoj千题计划196:bzoj4826: [Hnoi2017]影魔
http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...
- bzoj千题计划121:bzoj1033: [ZJOI2008]杀蚂蚁antbuster
http://www.lydsy.com/JudgeOnline/problem.php?id=1033 经半个下午+一个晚上+半个晚上 的 昏天黑地调代码 最终成果: codevs.洛谷.tyvj上 ...
- bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...
随机推荐
- C++自学随笔(2)
引用 就像人的别名,人不能只有别名,变量也不能只有引用. 指针类型的引用:*&指针引用名 = 指针. 如int a = 10;int *p =&a;int *&q =p1 co ...
- json反序列化对象
这个是同事研究的wcf中中根据type类型反序列化json的示例 /// <summary> /// json转对象 /// </summary> /// <param ...
- 【转】(C#)OPC客户端源码
本例下载/Files/badnewfish/OPC测试通过.rar 转载申明 申明:本文为转载,如需转载本文,请获取原文作者大尾巴狼啊的同意,谢谢合作! 转自:大尾巴狼啊 原文出处:http://ww ...
- Mscomm控件安装问题 License information for TMSComm not found.
操作步骤: 1.打开delphi,菜单选择compoents->import Activex control,然后选择那个mscomm32.ocx安装即可. 2.注册MScomm控件 开始- ...
- 处理编译错误"0" is an invalid value for the "DebugInformation" parameter of the "DCC" task.
在安装一个从XE6复制到XE4的控件时出现编译错误: [MSBuild Error] "0" is an invalid value for the "DebugInfo ...
- “Unable to open kernel device \\.\Global\vmx86
启动vm中虚拟机中的时候,弹出窗口的时候,弹出窗口 Unable to open kernel device \\.\Global\vmx86;系统找不到指定的文件,Did you reboot af ...
- 项目上线,php的错误信息必须不让其在页面中显示给客户,
对于PHP开发者来 说,一旦某个产品投入使用,应该立即将 display_errors选项关闭,以免因为这些错误所透露的路径.数据库连接.数据表等信息而遭到黑客攻击.但是,任何一个产品在投入使用后,都 ...
- QTime的本质上是一个int,QDateTime本质上是一个qint64
研究这个问题的起因发现使用<=比较时间的不准确,所以怀疑是一个浮点数(Delphi里的time就是一个浮点数).结果却发现是一个int class Q_CORE_EXPORT QTime { e ...
- 微信小程序组件 自定义弹出框
<!-- 点击立即抢拼弹出框 --> <view class='add-rob' bindtap="setModalStatus" data-status=&qu ...
- C#基础知识(base、this、new、override、abstract、virtual、static)
前言 本文主要来讲解一下C#中,自己觉得掌握的不怎么样或者用的不多,不太熟悉的关键字,主要包括base.this.new.override.abstract.virtual以及针对static字段和s ...