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 Ndays. 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. Source
POJ Monthly--2006.12.31, flymouse
|
题意:
对一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到d[1],d[2],d[3]……d[n] ,在数列的d中求一个最长的区间,使得区间中的最大值与最小值的差不超过m
思路:
先求出d数组,再用2个单调队列分别维护最大与最小值,扫描d数组,同时更新答案。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 7;
typedef long long ll;
struct node
{
int en, next, len;
}Edge[2*maxn];
int num, head[maxn];
void add(int st, int en,int len)
{
Edge[num].en = en, Edge[num].len = len;
Edge[num].next = head[st], head[st] = num++;
}
ll f[maxn][2], d[maxn];
int N, M;
bool vis[maxn];
ll dfs1(int u)
{
vis[u] = true;
for (int i = head[u]; i != -1; i = Edge[i].next)
{
int v = Edge[i].en;
if (vis[v]) continue;
f[u][0] = max(f[u][0], dfs1(v) + Edge[i].len);
}
return f[u][0];
}
void dfs2(int u)
{
vis[u] = true;
int max1 = 0, max2 = 0, v1;
for (int i = head[u]; i != -1; i = Edge[i].next)
{
int v = Edge[i].en;
if (vis[v]) continue;
int temp = f[v][0] + Edge[i].len;
if (temp>max1)
{
max2 = max1;
max1 = temp;
v1 = v;
}
else if (temp == max1 || temp>max2)
max2 = temp;
}
if (u != 1)
{
int temp = f[u][1];
if (temp>max1)
{
max2 = max1;
max1 = temp;
v1 = -1;
}
else if (temp == max1 || temp>max2)
max2 = temp;
}
for (int i = head[u]; i != -1; i = Edge[i].next)
{
int v = Edge[i].en;
if (vis[v]) continue;
if (v == v1) f[v][1] = max2 + Edge[i].len;
else f[v][1] = max1 + Edge[i].len;
dfs2(v);
}
}
int qmax[maxn], qmin[maxn];
void solve()
{
int front1, front2, rear1, rear2, ans, i, j;
front1 = front2 = rear1 = rear2 = ans = 0;
for (i = 1, j = 1;j <= N;j++)
{
while (rear1 > front1&&d[qmin[rear1 - 1]] >= d[j]) rear1--;
qmin[rear1++] = j;
while (rear2 > front2&&d[qmax[rear2 - 1]] <= d[j]) rear2--;
qmax[rear2++] = j;
if (d[qmax[front2]] - d[qmin[front1]] > M)
{
ans = max(ans, j - i);
while (d[qmax[front2]] - d[qmin[front1]] > M)
{
i = min(qmax[front2], qmin[front1]) + 1;
while (front1 < rear1&&qmin[front1] < i)
front1++;
while (front2 < rear2&&qmax[front2] < i)
front2++;
}
}
}
ans = max(ans, j - i);
printf("%d\n", ans);
}
int main()
{
while (scanf("%d%d", &N, &M) != EOF)
{
num = 0;
memset(head, -1, sizeof(head));
for (int i = 2;i <= N;i++)
{
int x, y;
scanf("%d%d", &x, &y);
add(i, x, y);
add(x, i, y);
}
memset(f, 0, sizeof(f));
memset(vis, 0, sizeof(vis));
dfs1(1);
memset(vis, 0, sizeof(vis));
dfs2(1);
for (int i = 1;i <= N;i++)
d[i] = max(f[i][0], f[i][1]);
solve();
}
return 0;
}