您的位置:首页 > 其它

poj 3162 walking race 树形dp 求符合条件区间

2016-10-01 12:19 381 查看
Walking Race

Time Limit: 10000MS Memory Limit: 131072K
Total Submissions: 3736 Accepted: 934
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.

题意:一棵树,求出从每个结点出发能到走的最长距离(每个结点最多只能经过一次),将这些距离按排成一个数组得到d[1],d[2],d[3]……d
,在数列的d中求一个最长的区间,使得区间中的最大值与最小值的差不超过m。

这题正解应该是树的直径加单调队列(可以点这看单调队列),因为只用保存最大值最小值。

但是觉得onlogn的也能过,所以用了直径加rmq,但是时间是可以但是爆内存。。尴尬。。。贴上rmq(比较好理解,可以改成线段树能过)和单调队列。

rmq:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long int64;
const int INF = 0x3f3f3f3f;
const int MAXN = 1000010;
int tot;
struct Node
{
int to,w;
int next;
};
struct Node edges[MAXN*2];
int head[MAXN];
void AddNode(int from, int to,int w)
{
edges[tot].to=to;
edges[tot].w=w;
edges[tot].next=head[from];
head[from]=tot++;
}
int n, m;
int f[MAXN][2];
int vis[MAXN];
int dfs1(int u){
vis[u] = true;
f[u][0] = 0;
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v=edges[i].to,w=edges[i].w;
if(vis[v]) continue;
f[u][0] = max(f[u][0], dfs1(v)+w);
}
return f[u][0];
}
void dfs2(int u, int fa_w){
vis[u] = true;
int max1=0, v1, max2=0, v2;
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v=edges[i].to,w=edges[i].w;
if(vis[v]) continue;
int tmp = f[v][0] + w;
if(tmp > max1){
max2 = max1; v2 = v1;
max1 = tmp; v1 = v;
}else if(tmp == max1 || tmp>max2){
max2 = tmp;
v2 = v;
}
}
if(u != 1){
int tmp = f[u][1];
int v = -1;
if(tmp > max1){
max2 = max1; v2 = v1;
max1 = tmp; v1 = v;
}else if(tmp == max1 || tmp>max2){
max2 = tmp;
v2 = v;
}
}
for(int i=head[u];i!=-1;i=edges[i].next)
{
int v=edges[i].to,w=edges[i].w;
if(vis[v]) continue;
if(v==v1){
f[v][1] = max2 + w;
}else{
f[v][1] = max1 + w;
}
dfs2(v, w);
}
}
int mmax[MAXN][21],mmin[MAXN][21];
void rmq_isit()
{
for(int i=1;i<=n;i++)
mmax[i][0]=mmin[i][0]=max(f[i][0],f[i][1]);
for(int j=1;(1<<j)<=n;j++)//表示区间的长度,从1开始递增
{
for(int i=1;i+(1<<j)-1<=n;i++)
{
mmax[i][j]=max(mmax[i][j-1],mmax[i+(1<<(j-1))][j-1]);
mmin[i][j]=min(mmin[i][j-1],mmin[i+(1<<(j-1))][j-1]);
}
}
}
int rmq(int l,int r,int ok)//查询,l->r区间的最大值与最小值的差值
{
int k=0;
while((1<<(k+1))<=r-l+1)
k++;
int ans;
//printf("%d %d %d %d\n",l,l+(1<<k),r-(1<<k)+1,r-(1<<k)+1+(1<<k));
if(ok)
ans=max(mmax[l][k],mmax[r-(1<<k)+1][k]);
else
ans=min(mmin[l][k],mmin[r-(1<<k)+1][k]);
return ans;
}
void init()
{

memset(f,0,sizeof(f));
memset(vis,0,sizeof(vis));
dfs1(1);
memset(vis,0,sizeof(vis));
dfs2(1,0);
rmq_isit();
}
void solve()
{
//printf("ok");
int left = 1, right = 1;
int ans=-1;
int tpmax =mmax[1][0],tpmin = mmax[1][0];
while (left <= right && right <= n) {
if (tpmax - tpmin <= m) {
ans = max(ans, right - left + 1);
right++;
tpmax = max(tpmax,mmax[right][0]);
tpmin = min(tpmin,mmin[right][0]);
}
else {
left++;
tpmax = rmq(left,right,1);
//cout<<left<<" "<<right<<" "<<tpmax<<endl;
tpmin = rmq(left,right,0);
//cout<<left<<" "<<right<<" "<<tpmin<<endl;
}
if (n - left < ans) break;
}
printf("%d\n", ans);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
int x,y;
tot=0;
memset(head,-1,sizeof(head));
for(int i=2;i<=n;i++)
{
scanf("%d%d",&x,&y);
AddNode(i,x,y);
AddNode(x,i,y);
}
init();
solve();
}
}


单调队列(网上流传最广的版本):
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1000001
int n,m,e;
int first
,next[N<<1],v[N<<1],w[N<<1];
int dx
,dy
,d
;
int qmin
,qmax
;
void init()
{
e=0;
memset(first+1,-1,sizeof(first[0])*n);
}
void add(int a,int b,int c)
{
v[e]=b;
w[e]=c;
next[e]=first[a];
first[a]=e++;
}
void dfs(int a,int fa,int dist,int *d)
{
int i,b;
for(i=first[a];~i;i=next[i])
{
b=v[i];
if(b^fa)    dfs(b,a,d[b]=dist+w[i],d);
}
}
void solve()
{
int i,j,front1,front2,rear1,rear2;
int ans=0;
front1=rear1=0;
front2=rear2=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(qmin[front1],qmax[front2])+1;
while(front1<rear1&&qmin[front1]<i)   front1++;
while(front2<rear2&&qmax[front2]<i)
edb1
front2++;
}
}
}
ans=max(ans,j-i);
printf("%d\n",ans);
}
int main()
{
int i,a,b,c,x,y;
while(~scanf("%d%d",&n,&m))
{
init();
for(a=2;a<=n;a++)
{
scanf("%d%d",&b,&c);
add(a,b,c);
add(b,a,c);
}
dfs(1,0,dx[1]=0,dx);

for(x=1,i=2;i<=n;i++)
{
if(dx[i]>dx[x]) x=i;
}
dfs(x,0,dx[x]=0,dx);
for(y=1,i=2;i<=n;i++)
{
if(dx[i]>dx[y]) y=i;
}
dfs(y,0,dy[y]=0,dy);
for(int i=1;i<=n;i++)   d[i]=max(dx[i],dy[i]);
solve();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: