您的位置:首页 > 其它

poj 3162(dfs+单调队列)

2013-10-06 15:18 267 查看
Walking Race

Time Limit: 10000MSMemory Limit: 131072K
Total Submissions: 2388Accepted: 557
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 Ncheck-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.

#include <iostream>
#include <cstdio>
using namespace std;

const int maxn = 1000010;

struct tree{
int l;
int r;
int mi;
int ma;
}a[4*maxn];

struct edge{
int u , v  ,f;
edge(int a = 0, int b = 0, int c = 0){
u = a , v = b , f = c;
}
}e[3*maxn];
int N , M , cnt;
int head[maxn] , next[maxn];

int t_minn , t_maxn , d[maxn],dx[maxn],dy[maxn];
int q1[maxn] , q2[maxn];

void add(int u , int v , int c){
e[cnt] = edge(u , v , c) , next[cnt] = head[u] , head[u] = cnt++;
e[cnt] = edge(v, u , c) , next[cnt] = head[v] , head[v] = cnt++;
}

void initial(){
for(int i = 0;i < maxn;i++){
head[i] = -1;
next[i] = -1;
d[i] = 0;
dx[i] = 0;
dy[i] = 0;
}
cnt = 0;
}

void readcase(){
int v, f;
for(int i = 0;i < N-1;i++){
scanf("%d%d" , &v , &f);
add(i+2 , v , f);
}
}

void dfs(int u,int fa,int dis,int *d){
for(int i=head[u];i!=-1;i=next[i]){
int v=e[i].v,f=e[i].f;
if(v!=fa) dfs(v,u,d[v]=dis+f,d);
}
}

void solve1(){
int x=1,y=1;
dfs(1,-1,d[1]=0,d);
for(int i=1;i<=N;i++) if(d[x]<d[i]) x=i;
dfs(x,-1,dx[x]=0,dx);
for(int i=1;i<=N;i++) if(dx[y]<dx[i]) y=i;
dfs(y,-1,dy[y]=0,dy);
for(int i=1;i<=N;i++) d[i]=max(dx[i],dy[i]);
//for(int i=1;i<=n;i++) cout<<"dis["<<i<<"]:"<<d[i]<<endl;
}

void computing(){
solve1();
int ans = 0;
int l = 1;
int r = 1;
int head1 = 0, rear1 = 0 , head2 = 0, rear2 = 0;
q1[0] = 1;
q2[0] = 1;
while(l <= r && r <= N){
while(d[q1[rear1]] <= d[r] && rear1 >= head1){
rear1--;
}
q1[++rear1] = r;
while(d[q2[rear2]] >= d[r] && rear2 >= head2){
rear2--;
}
q2[++rear2] = r;
if(d[q1[head1]] - d[q2[head2]] <= M){
ans = max(ans , r-l+1);
r++;
}else{
if(q1[head1] <= q2[head2]){
l = q1[head1++];
l++;
}else{
l = q2[head2++];
l++;
}
}
}
cout << ans << endl;
}

int main(){
while(~scanf("%d%d" , &N ,&M)){
initial();
readcase();
computing();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: