Walking Race
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.
题意:
给你一棵树,直接相连的两个节点的边有权值,求出第1,2,3,4,…,n个节点的最长距离。d[1],d[2],…,d[n];
然后你要找到最大的r - l,使得这个[l, r]区间的最大值减去最小值 < k;
第一个很好求,dfs两次求出直径端点,最后在对最后求出的端点做一次dfs,每次dfs的时候同时更新d[u]。
第二个,要么用线段树优化,要么用单调队列优化,分别维护区间的最大和最小值,然后滑动窗口。
具体见代码:
//
// Created by Running Photon on 2015-09-08
// Copyright (c) 2015 Running Photon. All rights reserved.
//
//
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <sstream>
#include <set>
#include <vector>
#include <stack>
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x,begin())
#define ll long long
#define CLR(x) memset(x, 0, sizeof x)
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 2e6 + 10;
const int maxv = 1e6 + 10;
const double eps = 1e-9;
inline int read() {
char c = getchar();
int f = 1;
while(!isdigit(c)) {
if(c == '-') f = -1;
c = getchar();
}
int x = 0;
while(isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int n, k;
int head[maxv], pnt[maxn], cnt, nxt[maxn], cost[maxn];
int dis[maxv], root1, root2, d[maxv];
void add_edge(int u, int v, int val) {
pnt[cnt] = v;
cost[cnt] = val;
nxt[cnt] = head[u];
head[u] = cnt++;
}
void findRoot(int u, int fa, int &root) {
for(int i = head[u]; ~i; i = nxt[i]) {
int v = pnt[i];
if(v == fa) continue;
dis[v] = dis[u] + cost[i];
findRoot(v, u, root);
}
if(dis[u] > dis[root]) {
root = u;
}
d[u] = max(d[u], dis[u]);//dfs的同时更新d
}
void work() {
root1 = 1, root2 = 1;
findRoot(1, 1, root1);//找到根1
dis[root1] = 0;
findRoot(root1, root1, root2);找到根2
dis[root2] = 0;
findRoot(root2, root2, root1);//最后再对根2进行dfs
}
void sol() {
int ans = 0;
deque <int> dequemax, dequemin;
int l = 1, r = 1;
for(; r <= n; r++) {
while(dequemax.size() && d[r] >= d[dequemax.back()]) {
dequemax.pop_back();
}
dequemax.push_back(r);
while(dequemin.size() && d[r] <= d[dequemin.back()]) {
dequemin.pop_back();
}
dequemin.push_back(r);
if(d[dequemax[0]] - d[dequemin[0]] > k) {
while(d[dequemax[0]] - d[dequemin[0]] > k) {
l = min(dequemax[0], dequemin[0]) + 1;
while(dequemax[0] < l) dequemax.pop_front();
while(dequemin[0] < l) dequemin.pop_front();
}
ans = max(ans, r - l + 1);
}
ans = max(ans, r - l + 1);
}
printf("%d\n", ans);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
while(scanf("%d%d", &n, &k) != EOF) {
cnt = 0;
CLR(d);
memset(head, -1, sizeof head);
for(int i = 2; i <= n; i++) {
int u = read(), val = read();
add_edge(i, u, val);
add_edge(u, i, val);
}
work();
sol();
}
return 0;
}