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;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/81209620