[NOIP 2007] 树网的核
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1999
[算法]
树的直径 + 单调队列
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 500010
const int INF = 2e9; int i,n,s,ans,head,tail,m,t,tot,u,v,w,l,r;
int pre[MAXN],dis[MAXN],h[MAXN],f[MAXN],mx[MAXN],q[MAXN];
bool visited[MAXN]; struct edge
{
int to,w,nxt;
} e[MAXN << ];
vector< pair<int,int> > path; inline void addedge(int u,int v,int w)
{
tot++;
e[tot] = (edge){v,w,h[u]};
h[u] = tot;
}
inline int bfs(int s)
{
int i,cur,v,w,m;
queue< int > q;
q.push(s);
dis[s] = ;
pre[s] = ;
while (!q.empty())
{
cur = q.front();
q.pop();
for (i = h[cur]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (!dis[v] && v != s)
{
dis[v] = dis[cur] + w;
q.push(v);
pre[v] = cur;
}
}
}
m = ;
for (i = ; i <= n; i++)
{
if (dis[i] > dis[m])
m = i;
}
return m;
}
inline void bfsII(int s)
{
int i,cur,v,w;
queue< int > q;
q.push(s);
while (!q.empty())
{
cur = q.front();
q.pop();
for (i = h[cur]; i; i = e[i].nxt)
{
v = e[i].to;
w = e[i].w;
if (!visited[v] && !f[v])
{
visited[v] = true;
f[v] = f[cur] + w;
mx[s] = max(mx[s],f[v]);
q.push(v);
}
}
}
}
int main()
{ scanf("%d%d",&n,&m);
for (i = ; i < n; i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
s = bfs();
memset(dis,,sizeof(dis));
t = bfs(s);
for (i = t; i; i = pre[i]) visited[i] = true;
for (i = t; i; i = pre[i]) bfsII(i);
q[] = t;
l = r = t;
head = tail = ;
ans = INF;
for (l = t; l; l = pre[l])
{
while (pre[r] && dis[l] - dis[pre[r]] <= m)
{
r = pre[r];
while (head <= tail && mx[q[tail]] <= mx[r]) tail--;
q[++tail] = r;
ans = min(ans,max(mx[q[head]],max(dis[r] - dis[s],dis[t] - dis[l])));
}
if (q[head] == l) head++;
}
printf("%d\n",ans); return ; }
[NOIP 2007] 树网的核的更多相关文章
- NOIP 2007树网的核
题目描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各边长度的集合,并 ...
- 树网的核 2007年NOIP全国联赛提高组(floyed)
树网的核 2007年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description [问题描述]设 T= ...
- noip2007 树网的核
P1099 树网的核 112通过 221提交 题目提供者该用户不存在 标签动态规划树形结构2007NOIp提高组 难度提高+/省选- 提交该题 讨论 题解 记录 题目描述 设T=(V, E, W) ...
- 洛谷 P1099 树网的核
P1099 树网的核 题目描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W ...
- [NOIP2007] 提高组 洛谷P1099 树网的核
题目描述 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各边长度的集合,并 ...
- 洛谷1099 [NOIP2007] 树网的核
链接https://www.luogu.org/problemnew/show/P1099 题目描述 设T=(V,E,W)是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称TTT为 ...
- [bzoj1999]树网的核
从下午坑到网上..noip的数据太弱,若干的地方写挂结果还随便过= = 最坑的就是网上有些题解没考虑周全... 第一步是找直径,用两次bfs(或者dfs,Linux下系统栈挺大的..)解决.找出其中一 ...
- BZOJ1999或洛谷1099&BZOJ2282或洛谷2491 树网的核&[SDOI2011]消防
一道树的直径 树网的核 BZOJ原题链接 树网的核 洛谷原题链接 消防 BZOJ原题链接 消防 洛谷原题链接 一份代码四倍经验,爽 显然要先随便找一条直径,然后直接枚举核的两个端点,对每一次枚举的核遍 ...
- BZOJ1999 树网的核[数据加强版]
1999: [Noip2007]Core树网的核 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1214 Solved: 336[Submit][St ...
随机推荐
- VHDL之concurrent之block
1 Simple BLOCK The simple block represents only a way of partitioning the code. It allows concurrent ...
- QT设计UI:QT模式对话框打开文件
使用QT模式对话框,并使显示框 为背景色: 方法使用了QCheckBox *native; #include <QCheckBox> 初始化函数代码: //设置默认打开图像位置 nat ...
- 重新理解 Monad
对于大多数刚刚入门函数式编程的同学来说,monad(单子.又叫单体)可能是这里面的一道坎.你可能对 map . flatMap 以及 filter 再熟悉不过,可是到了高阶的抽象层次上就又会变得一脸懵 ...
- 浅析Python3中的bytes和str类型 (转)
原文出处:https://www.cnblogs.com/chownjy/p/6625299.html#undefined Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文 ...
- Python总结1
时间:24日下午输入:input()输出:print()格式化输出通过某种占位符用于替换字符串中某个位置的字符.占位符:%s:可以替换任意类型%d:可以替换数字类型 需要掌握的#1.strip去左右两 ...
- 【Android】进程间通信IPC——AIDL
AIDL官网定义AIDL(Android 接口定义语言)与您可能使用过的其他 IDL 类似. 您可以利用它定义客户端与服务使用进程间通信 (IPC) 进行相互通信时都认可的编程接口. 在 Androi ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- HLPP算法 一种高效的网络最大流算法
#include <algorithm> #include <cstdio> #include <cctype> #include <queue> #d ...
- [luogu1627 CQOI2009] 中位数 (乱搞)
传送门 Solution 好水的题(ーー;) Code //By Menteur_Hxy #include <map> #include <queue> #include &l ...
- nexus3修改admin密码
Nexus是通过内置的orientdb数据库管理,是以需要进入到库里面修改密码. 不能像这篇博客(https://blog.csdn.net/tianya6607/article/details/53 ...