POJ 3162 Walking Race(树的直径+单调队列)
题目大意:对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到dis[1],dis[2],dis[3]……dis[n] ,在数列的dis中求一个最长的区间,使得区间中的最大值与最小值的差不超过m。
思路:先找出树的直径的两个端点来,那么树当中的其它节点能走的最大距离一定是到这个两个点当中的其中一个的。所以三遍bfs就可以求出来这个最远的距离,那么,这个最远的距离求出来之后再用两个单调队列来维护最大值和最小值。
/*************************************************************************
> File Name: poj_3162.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年09月05日 星期六 14时00分47秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + ;
int tot, head[maxn];
struct Edge {
int to, next, w;
}edge[maxn * ];
typedef pair<int, int> pii;
int dis1[maxn], dis2[maxn], dis[maxn];
void init()
{
tot = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
int pos, maxx;
bool vis[maxn];
void bfs(int u, int *dist)//从u点开始搜索到各个点的距离,保存在dist当中
{
maxx = ;
queue<pii> Q;
memset(vis, false, sizeof(vis));
pii cur, nex;
cur.first = u; cur.second = ;
vis[u] = true;
dist[u] = ;
Q.push(cur);
while (!Q.empty())
{
cur = Q.front();
Q.pop();
for (int i = head[cur.first]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (vis[v]) continue;
vis[v] = true;
nex.first = v; nex.second = cur.second + edge[i].w;
dist[v] = nex.second;
if (maxx < nex.second)
{
maxx = nex.second;
pos = v;//直径的一个端点保存在pos当中
}
Q.push(nex);
}
}
}
int Q_max[maxn], Q_min[maxn];
int n, m;
int solve()//用两个单调队列分别维护最大值和最小值
{
int ans = ;
int front1, front2, tail1, tail2, i, j;
front1 = front2 = ; tail1 = tail2 = ;
for (j = , i = ; i <= n; i++)
{
while (tail1 >= front1 && dis[Q_max[tail1]] <= dis[i]) tail1--;
Q_max[++tail1] = i; while (tail2 >= front2 && dis[Q_min[tail2]] >= dis[i]) tail2--;
Q_min[++tail2] = i; if (dis[Q_max[front1]] - dis[Q_min[front2]] > m)
{
ans = max(ans, i - j);
while (dis[Q_max[front1]] - dis[Q_min[front2]] > m)
{
j = min(Q_max[front1], Q_min[front2]) + ;
while (tail1 >= front1 && Q_max[front1] < j) front1++;
while (tail2 >= front2 && Q_min[front2] < j) front2++;
}
}
}
ans = max(ans, i - j);
return ans;
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
int v, w;
init();
for (int i = ; i < n; i++)
{
scanf("%d %d", &v, &w);
addedge(i + , v, w);
addedge(v, i + , w);
}
bfs(, dis1);
bfs(pos, dis1);
bfs(pos, dis2);
for (int i = ; i <= n; i++)
dis[i] = max(dis1[i], dis2[i]);
printf("%d\n", solve());
}
return ;
}
POJ 3162 Walking Race(树的直径+单调队列)的更多相关文章
- POJ 3162 Walking Race(树形dp+单调队列 or 线段树)
http://poj.org/problem?id=3162 题意:一棵n个节点的树.有一个屌丝爱跑步,跑n天,第i天从第i个节点开始跑步,每次跑到距第i个节点最远的那个节点(产生了n个距离),现在要 ...
- HDU 4123 Bob’s Race 树的直径+单调队列
题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...
- POJ 3162 Walking Race (树的直径,单调队列)
题意:给定一棵带边权的n个节点的树,首先要求出每个点的最长路,然后写成序列d[1],d[2]...d[n],然后求满足 区间最大值-区间最小值<=k 的最大区间长度为多少? 思路: 分两步进行: ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- POJ - 3162 Walking Race 树形dp 单调队列
POJ - 3162Walking Race 题目大意:有n个训练点,第i天就选择第i个训练点为起点跑到最远距离的点,然后连续的几天里如果最远距离的最大值和最小值的差距不超过m就可以作为观测区间,问这 ...
- 【题解】poj 3162 Walking Race 树形dp
题目描述 Walking RaceTime Limit: 10000MS Memory Limit: 131072KTotal Submissions: 4941 Accepted: 1252Case ...
- HDU 4123 Bob's Race:树的直径 + 单调队列 + st表
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4123 题意: 给你一棵树,n个节点,每条边有长度. 然后有m个询问,每个询问给定一个q值. 设dis[ ...
- HDU 4123(树的直径+单调队列)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 3162 Walking Race 树形DP+线段树
给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...
随机推荐
- 练习2 A - ASCII码排序
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 输入三 ...
- Asp.Net MVC5 格式化输出时间日期
刚好用到这个,网上找的全部是输出文本框内容的格式化日期时间 而我需要只是在一个表格中的单元个中输出单纯的文字 最后在MSDN上找到 HtmlHelper.FormatValue 方法 public s ...
- Java库使用----xstream1.3.1
package com.xstream; import java.util.Map; /** * XStream可以自动生成相关的xml配置 */ public class XstreamTest { ...
- Unity干中学——如何实现类似Windows Store 应用程序和Android Toast的通知?
要实现通知中心功能,首先要创建一个游戏物体,在上面挂载GUITeture和GUIText脚本.注意GUITexture和GUIText脚本的顺序,GUITexture在前,GUIText在后,否则GU ...
- C#,Java,C++中的finally关键字
博客原文:http://hankjin.blog.163.com/blog/static/33731937201031511305338/ 先说C++,标准C++不支持finally, 如果要实现fi ...
- [BC Round#26] Card 【各种水】
题目链接:HDOJ - 5159 这道题的做法太多了..BC的第二题也是可以非常水的.. 算法一 我在比赛的时候写的算法是这样的.. 预处理出所有的答案,然后对于每个询问直接输出. 询问 (a, b) ...
- poj1980
首先想到费用流,但m<=100000还是算了吧那就感觉要用dp了,首先将a,b排序贪心一下可知,a,b的配对肯定不可能出现交叉这样就可以dp了,复杂度O(nm)还是过不去在贪心一下会发现,对于a ...
- 【转】Android4.3 蓝牙BLE初步
原文网址:http://www.cnblogs.com/savagemorgan/p/3722657.html 一.关键概念: Generic Attribute Profile (GATT) 通过B ...
- 数据结构(堆):POJ 1442 Black Box
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10658 Accepted: 4390 Descri ...
- 解决SQL Server管理器无法连接远程数据库Error: 1326错误
解决SQL Server管理器无法连接远程数据库Error: 1326错误 我们在在使用SQL Server时都会遇到使用SQL Server Management Studio无法连接远程数据库实例 ...