题意:
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.
翻译成汉语,每个样例输入一个n,m,然后再给出n - 1个边的点权。n个点组成一颗树,求出每个点的离它最远的距离记为a【i】,问连续的区间中差值全部小于等于m的最长区间的长度。
思路:
对于求出每个点离它最远的距离a【i】,我们可以通过树形dp来求解(注意需要用链式前向星或者模拟vector来求解,否则TLE),然后再通过尺取+单调队列来求最长长度(当然用线段树也可以求解)。单调队列需要开两个,一个存最大值队列,一个存最小值队列。
代码:
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <deque>
#define N 1000005
#define ll long long
using namespace std;
struct node {
int inx;
ll val;
};
int n, m;
node point[N * 2];
node dp[N][3];
int last[N * 2], fin[N];
deque<int>maxn, minn;
inline int read() {
char ch = getchar();
int x = 0;
while(ch < '0' || ch > '9') {
ch = getchar();
}
while('0' <= ch && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
void init() {
int a, b;
int tot = 0;
node c;
n = read();
m = read();
for (int i = 2; i <= n; i++) {
a = read();
b = read();
c.inx = a;
c.val = b;
point[++tot] = c;
last[tot] = fin[i];
fin[i] = tot;
c.inx = i;
point[++tot] = c;
last[tot] = fin[a];
fin[a] = tot;
}
}
void dfs (int inx, int father) {
for (int i = fin[inx]; i != 0; i = last[i]) {
node son = point[i];
if (son.inx == father) continue;
dfs(son.inx, inx);
if (dp[inx][0].inx == 0) {
dp[inx][0].val = dp[son.inx][0].val + son.val;
dp[inx][0].inx = son.inx;
} else {
if (dp[inx][0].val < dp[son.inx][0].val + son.val) {
dp[inx][1].val = dp[inx][0].val;
dp[inx][1].inx = dp[inx][0].inx;
dp[inx][0].val = dp[son.inx][0].val + son.val;
dp[inx][0].inx = son.inx;
} else if (dp[inx][1].val < dp[son.inx][0].val + son.val) {
dp[inx][1].val = dp[son.inx][0].val + son.val;
dp[inx][1].inx = son.inx;
}
}
}
return;
}
void dfs1 (int inx, int father) {
for (int i = fin[inx]; i != 0; i = last[i]) {
node son = point[i];
if (son.inx == father) continue;
if (dp[inx][0].inx == son.inx) {
dp[son.inx][2].val = son.val + max(dp[inx][1].val, dp[inx][2].val);
} else {
dp[son.inx][2].val = son.val + max(dp[inx][0].val, dp[inx][2].val);
}
dfs1(son.inx, inx);
}
return;
}
int main () {
int res = 0;
init();
dfs(1, -1);
dp[1][2].val = 0;
dfs1(1, -1);
int start = 1;
for (int i = 1; i <= n; i++) {
dp[i][0].val = max(dp[i][0].val, dp[i][2].val);
while(!maxn.empty() && dp[maxn.back()][0].val < dp[i][0].val) {
maxn.pop_back();
}
maxn.push_back(i);
while(!minn.empty() && dp[minn.back()][0].val > dp[i][0].val) {
minn.pop_back();
}
minn.push_back(i);
while(dp[maxn.front()][0].val - dp[minn.front()][0].val > m) {
if (maxn.front() < minn.front()) {
start = maxn.front() + 1;
maxn.pop_front();
} else {
start = minn.front() + 1;
minn.pop_front();
}
}
res = max(i - start + 1, res);
}
printf("%d\n", res);
return 0;
}
如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢