POJ 3162 Walking Race(树形dp+双指针+线段树 or 树形dp+二分+单调队列)

Walking Race

Time Limit: 10000MS Memory Limit: 131072K
Total Submissions: 5030 Accepted: 1276
Case Time Limit: 3000MS

Description

flymouse’s sister wc is very capable at sports and her favorite event is walking race. Chasing after the championship in an important competition, she comes to a training center to attend a training course. The center has N check-points numbered 1 through N. Some pairs of check-points are directly connected by two-way paths. The check-points and the paths form exactly a tree-like structure. The course lasts N days. On the i-th day, wc picks check-point i as the starting point and chooses another check-point as the finishing point and walks along the only simple path between the two points for the day’s training. Her choice of finishing point will make it that the resulting path will be the longest among those of all possible choices.

After every day’s training, flymouse will do a physical examination from which data will obtained and analyzed to help wc’s future training be better instructed. In order to make the results reliable, flymouse is not using data all from N days for analysis. flymouse’s model for analysis requires data from a series of consecutive days during which the difference between the longest and the shortest distances wc walks cannot exceed a bound M. The longer the series is, the more accurate the results are. flymouse wants to know the number of days in such a longest series. Can you do the job for him?

Input

The input contains a single test case. The test case starts with a line containing the integers N (N ≤ 106) and M (M < 109). Then follow N − 1 lines, each containing two integers fi and di (i = 1, 2, …, N − 1), meaning the check-points i + 1 and fi are connected by a path of length di.

Output

Output one line with only the desired number of days in the longest series.

Sample Input

3 2
1 1
1 3

Sample Output

3

Hint

Explanation for the sample:

There are three check-points. Two paths of lengths 1 and 3 connect check-points 2 and 3 to check-point 1. The three paths along with wc walks are 1-3, 2-1-3 and 3-1-2. And their lengths are 3, 4 and 4. Therefore data from all three days can be used for analysis.

思路:
求每个点所能到的最远距离:先跑一边dfs,得出每个点能到其子树的最远距离以及次远距离。
并记录这两条路分别走的是哪两个子节点。然后若v的父亲节点fa的id1[fa]!=v,则v的最远距离
是d1[v]=max(d1[v],d1[fa]+dist[fa][v]) 否则d1[v]=max(d1[v],d2[fa]+dist[fa][v])。
求最长区间:
双指针l,r枚举区间,线段树判断l~r的最大值和最小值。
或者二分枚举区间长度,双端队列求滑动最大值和最小值。

代码:

线段树:

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
#define ll long long
const int maxn = 1e6 + 10;
struct node
{
	int to, cost, next;
} G[maxn * 2];
int head[maxn];
int n, m, tol;
int id1[maxn], id2[maxn];
ll d1[maxn], d2[maxn];
ll max_tree[maxn << 2], min_tree[maxn << 2], max_dist, min_dist;
void add ( int a, int b, int c )
{
	G[tol].to = b;
	G[tol].cost = c;
	G[tol].next = head[a];
	head[a] = tol++;
}
void dfs ( int v, int fa )
{
	d1[v] = d2[v] = 0;
	for ( int i = head[v]; i != -1; i = G[i].next )
	{
		node e = G[i];
		if ( e.to == fa ) continue;
		dfs ( e.to, v );
		if ( d1[e.to] + e.cost > d1[v] )
		{
			d2[v] = d1[v];
			id2[v] = id1[v];
			d1[v] = d1[e.to] + e.cost;
			id1[v] = e.to;
		}
		else if ( d1[e.to] + e.cost > d2[v] )
		{
			d2[v] = d1[e.to] + e.cost;
			id2[v] = e.to;
		}
	}
}
void DFS ( int v, int fa )
{
	for ( int i = head[v]; i != -1; i = G[i].next )
	{
		node e = G[i];
		if ( e.to == fa ) continue;
		if ( id1[v] != e.to )
		{
			if ( d1[v] + e.cost >= d1[e.to] )
			{
				d1[e.to] = d1[v] + e.cost;
				id1[e.to] = v;
			}
			else if ( d1[v] + e.cost >= d2[e.to] )
			{
				d2[e.to] = d1[v] + e.cost;
				id2[e.to] = v;
			}
		}
		else
		{
			if ( d2[v] + e.cost >= d1[e.to] )
			{
				d1[e.to] = d2[v] + e.cost;
				id1[e.to] = v;
			}
			else if ( d2[v] + e.cost >= d2[e.to] )
			{
				d2[e.to] = d2[v] + e.cost;
				id2[e.to] = v;
			}
		}
		DFS ( e.to, v );
	}
}
void build ( int l, int r, int rt )
{
	if ( l == r )
	{
		max_tree[rt] = min_tree[rt] = d1[l];
		return;
	}
	int mid = ( l + r ) / 2;
	build ( l, mid, rt << 1 );
	build ( mid + 1, r, rt << 1 | 1 );
	max_tree[rt] = max ( max_tree[rt << 1], max_tree[rt << 1 | 1] );
	min_tree[rt] = min ( min_tree[rt << 1], min_tree[rt << 1 | 1] );
}
void query ( int L, int R, int l, int r, int rt )
{
	if ( l >= L && r <= R )
	{
		max_dist = max ( max_dist, max_tree[rt] );
		min_dist = min ( min_dist, min_tree[rt] );
		return;
	}
	int mid = ( l + r ) / 2;
	if ( L <= mid ) query ( L, R, l, mid, rt << 1 );
	if ( R > mid ) query ( L, R, mid + 1, r, rt << 1 | 1 );
}
int main()
{
	while ( ~scanf ( "%d%d", &n, &m ) )
	{
		memset ( head, -1, sizeof ( head ) );
		for ( int i = 2; i <= n; i++ )
		{
			int x, y;
			scanf ( "%d%d", &x, &y );
			add ( x, i, y );
			add ( i, x, y );
		}
		dfs ( 1, 0 );
		DFS ( 1, 0 );
		build ( 1, n, 1 );
		int l = 1, r = 1, ans = 0;
		while ( r <= n )
		{
			max_dist = 0, min_dist = 1e18;
			query ( l, r, 1, n, 1 );
			if ( max_dist - min_dist <= m ) ans = max ( ans, r - l + 1 ), r++;
			while ( max_dist - min_dist > m )
			{
				max_dist = 0, min_dist = 1e18;
				l++;
				query ( l, r, 1, n, 1 );
			}
		}
		printf ( "%d\n", ans );
	}
	return 0;
}

双端队列:

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
#define ll long long
const int maxn = 1e6 + 10;
struct node
{
	int to, cost, next;
} G[maxn * 2];
int head[maxn];
int n, m, tol;
int id1[maxn], id2[maxn];
int deq1[maxn], deq2[maxn];
ll d1[maxn], d2[maxn];
void add ( int a, int b, int c )
{
	G[tol].to = b;
	G[tol].cost = c;
	G[tol].next = head[a];
	head[a] = tol++;
}
void dfs ( int v, int fa )
{
	d1[v] = d2[v] = 0;
	for ( int i = head[v]; i != -1; i = G[i].next )
	{
		node e = G[i];
		if ( e.to == fa ) continue;
		dfs ( e.to, v );
		if ( d1[e.to] + e.cost > d1[v] )
		{
			d2[v] = d1[v];
			id2[v] = id1[v];
			d1[v] = d1[e.to] + e.cost;
			id1[v] = e.to;
		}
		else if ( d1[e.to] + e.cost > d2[v] )
		{
			d2[v] = d1[e.to] + e.cost;
			id2[v] = e.to;
		}
	}
}
void DFS ( int v, int fa )
{
	for ( int i = head[v]; i != -1; i = G[i].next )
	{
		node e = G[i];
		if ( e.to == fa ) continue;
		if ( id1[v] != e.to )
		{
			if ( d1[v] + e.cost >= d1[e.to] )
			{
				d1[e.to] = d1[v] + e.cost;
				id1[e.to] = v;
			}
			else if ( d1[v] + e.cost >= d2[e.to] )
			{
				d2[e.to] = d1[v] + e.cost;
				id2[e.to] = v;
			}
		}
		else
		{
			if ( d2[v] + e.cost >= d1[e.to] )
			{
				d1[e.to] = d2[v] + e.cost;
				id1[e.to] = v;
			}
			else if ( d2[v] + e.cost >= d2[e.to] )
			{
				d2[e.to] = d2[v] + e.cost;
				id2[e.to] = v;
			}
		}
		DFS ( e.to, v );
	}
}
bool check ( int x )
{
	int s1 = 0, t1 = 0, s2 = 0, t2 = 0;
	for ( int i = 1; i <= n; i++ )
	{
		while ( s1 < t1 && d1[deq1[t1 - 1]] >= d1[i] ) t1--;
		deq1[t1++] = i;
		while ( s2 < t2 && d1[deq2[t2 - 1]] <= d1[i] ) t2--;
		deq2[t2++] = i;
		if ( i >= x ) if ( d1[deq2[s2]] - d1[deq1[s1]] <= m ) return 1;
		if ( i - deq1[s1] + 1 == x ) s1++;
		if ( i - deq2[s2] + 1 == x ) s2++;
	}
	return 0;
}
int main()
{
	while ( ~scanf ( "%d%d", &n, &m ) )
	{
		memset ( head, -1, sizeof ( head ) );
		for ( int i = 2; i <= n; i++ )
		{
			int x, y;
			scanf ( "%d%d", &x, &y );
			add ( x, i, y );
			add ( i, x, y );
		}
		dfs ( 1, 0 );
		DFS ( 1, 0 );
		int l = 1, r = n, ans = 1;
		while ( l <= r )
		{
			int mid = ( l + r ) / 2;
			if ( check ( mid ) ) ans = mid, l = mid + 1;
			else r = mid - 1;
		}
		printf ( "%d\n", ans );
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值