题目地址:
http://poj.org/problem?id=3162
题目意思:
有N个点,分贝是1~N,然后要你求出每个点在这个图上的最长路
然后找一个连续的子序列,使得这个序列的最大值和最小值之差小于等于M,求最长的子序列的长度
解题思路:
首先是求最长路,这个和HDU的COMPUTER简直就是一模一样
但是要注意,因为这个数据量很大, 所以不建议使用STL,我用的是指针加提前申请好一个数组
这样避免了重复的去申请内存。用链表来建立邻接链表
然后就是求这个区间
我这个是从博客上看来的,用滑动窗口的方法来做
就是设置这个窗口的左右分别为L,R,一开始都是1,然后如果当前区间符合要求则R后移,如果不符合则L左移
求这个区间的最大值和最小值可以使用线段树,看DISCUSS里面说用RMQ会超内存,所以就用线段树了
这个题目是多种技术的融合,好题。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int maxn = 1100000;
struct node
{
int v,len;
node *next;
}*head[maxn],mem[maxn<<1];
int dis[maxn];
int n,m;
int tree_min[maxn<<2];
int tree_max[maxn<<2];
int mempos;
void insert_into(int a,int b,int l)
{
mem[mempos].v = b;
mem[mempos].len = l;
mem[mempos].next = head[a];
head[a] = &mem[mempos];
mempos++;
}
void init()
{
for(int i=0;i<=n;i++)
head[i]=NULL;
mempos=0;
for(int i=1;i<=n;i++)
dis[i]=0;
int a,l;
node tmp;
for(int i=2;i<=n;i++)
{
scanf("%d%d",&a,&l);
insert_into(i,a,l);
insert_into(a,i,l);
}
}
void dfs_to_son(int root,int fa)
{
node *p = head[root];
while(p != NULL)
{
int v = p->v;
if(v==fa)
{
p=p->next;
continue;
}
dfs_to_son(v,root);
dis[root] = max(dis[root],p->len+dis[v]);
p->len = p->len+dis[v];
p=p->next;
}
}
void dfs_find_longest_path(int root,int fa)
{
int not_son_longest=0;
node *p = head[fa];
while(p != NULL)
{
int v=p->v;
if(v==root)
{
p=p->next;
continue;
}
not_son_longest = max(not_son_longest,p->len);
p=p->next;
}
p = head[root];
while(p!=NULL)
{
int v = p->v;
if(v==fa)
{
p->len = not_son_longest + p->len;
break;
}
p=p->next;
}
p = head[root];
while(p!=NULL)
{
dis[root] = max(dis[root],p->len);
p=p->next;
}
p = head[root];
while(p!=NULL)
{
int v = p->v;
if(v==fa)
{
p=p->next;
continue;
}
dfs_find_longest_path(v,root);
p=p->next;
}
}
void pushup(int rt)
{
tree_min[rt] = min(tree_min[rt<<1],tree_min[rt<<1|1]);
tree_max[rt] = max(tree_max[rt<<1],tree_max[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
tree_max[rt]=tree_min[rt] = dis[l];
return;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
int query_max(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
return tree_max[rt];
int mid = (l+r)>>1;
int ret = 0;
if(L<=mid)
ret = max(ret,query_max(L,R,lson));
if(R>mid)
ret = max(ret,query_max(L,R,rson));
return ret;
}
int query_min(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R)
return tree_min[rt];
int mid = (l+r)>>1;
int ret = 0x3f3f3f3f;
if(L<=mid)
ret = min(ret,query_min(L,R,lson));
if(R>mid)
ret = min(ret,query_min(L,R,rson));
return ret;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
//printf("a\n");
dfs_to_son(1,0);
//printf("a2\n");
dfs_find_longest_path(1,0);
//printf("a3\n");
build(1,n,1);
int l,r;
int _max,_min;
l=r=1;
_max=_min=dis[1];
int ans = 0;
while(l<=r && r<=n)
{
if(_max-_min <= m)
{
ans = max(ans,r-l+1);
r++;
_max = max(_max,dis[r]);
_min = min(_min,dis[r]);
}
else
{
l++;
_max = query_max(l,r,1,n,1);
_min = query_min(l,r,1,n,1);
}
if(n-l+1<ans) break;
}
printf("%d\n",ans);
}
return 0;
}