hdu 4123 樹形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php?pid=4123
Problem Description
Bob wants to hold a race to encourage people to do sports. He has got trouble in choosing the route. There are N houses and N - 1 roads in his village. Each road connects two houses, and all houses are connected together. To make the race more interesting,
he requires that every participant must start from a different house and run AS FAR AS POSSIBLE without passing a road more than once. The distance difference between the one who runs the longest distance and the one who runs the shortest distance is called
“race difference” by Bob. Bob does not want the “race difference”to be more than Q. The houses are numbered from 1 to N. Bob wants that the No. of all starting house must be consecutive. He is now asking you for help. He wants to know the maximum number of
starting houses he can choose, by other words, the maximum number of people who can take part in his race.
Input
There are several test cases.
The first line of each test case contains two integers N and M. N is the number of houses, M is the number of queries.
The following N-1 lines, each contains three integers, x, y and z, indicating that there is a road of length z connecting house x and house y.
The following M lines are the queries. Each line contains an integer Q, asking that at most how many people can take part in Bob’s race according to the above mentioned rules and under the condition that the“race difference”is no more than Q.
The input ends with N = 0 and M = 0.
(N<=50000 M<=500 1<=x,y<=N 0<=z<=5000 Q<=10000000)
The first line of each test case contains two integers N and M. N is the number of houses, M is the number of queries.
The following N-1 lines, each contains three integers, x, y and z, indicating that there is a road of length z connecting house x and house y.
The following M lines are the queries. Each line contains an integer Q, asking that at most how many people can take part in Bob’s race according to the above mentioned rules and under the condition that the“race difference”is no more than Q.
The input ends with N = 0 and M = 0.
(N<=50000 M<=500 1<=x,y<=N 0<=z<=5000 Q<=10000000)
Output
For each test case, you should output the answer in a line for each query.
Sample Input
5 5
1 2 3
2 3 4
4 5 3
3 4 2
1
2
3
4
5
0 0
Sample Output
1
3
3
3
5
/**
hdu 4123 樹形DP+RMQ
題目大意:給定一棵樹,每一個點都從當前位置走到距離最遠的位置,1~n的連續區間中最大並且走的最遠距離差值不超過Q的區間右多大
解題思路:兩遍dfs求出在樹中到當前點的最長距離:dfs1求出以當前節點為根節點的子樹中到該節點的最長距離和次長距離,dfs2將上一步求出的
最長距離和經過根節點的最長距離比較取最大。利用RMQ查詢尋找最大區間
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int N=50050;
int head[N],ip;
struct note
{
int v,w,next;
}edge[N*2];
void init()
{
memset(head,-1,sizeof(head));
ip=0;
}
void addedge(int u,int v,int w)
{
edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}
int maxn[N],smaxn[N],maxid[N],smaxid[N];
void dfs1(int u,int pre)
{
maxn[u]=smaxn[u]=maxid[u]=smaxid[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(pre==v)continue;
dfs1(v,u);
if(maxn[v]+edge[i].w>smaxn[u])
{
smaxid[u]=v;
smaxn[u]=maxn[v]+edge[i].w;
if(maxn[u]<smaxn[u])
{
swap(maxn[u],smaxn[u]);
swap(maxid[u],smaxid[u]);
}
}
}
}
void dfs2(int u,int pre)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(pre==v)continue;
if(maxid[u]==v)
{
if(smaxn[u]+edge[i].w>smaxn[v])
{
smaxn[v]=smaxn[u]+edge[i].w;
smaxid[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxid[v],smaxid[v]);
}
}
}
else
{
if(maxn[u]+edge[i].w>smaxn[v])
{
smaxn[v]=maxn[u]+edge[i].w;
smaxid[v]=u;
if(maxn[v]<smaxn[v])
{
swap(maxn[v],smaxn[v]);
swap(maxid[v],smaxid[v]);
}
}
}
dfs2(v,u);
}
}
int a[N],n,m;
int dp1[N][30];
int dp2[N][30];
void RMQ_init(int n)
{
for(int i=1;i<=n;i++)
{
dp1[i][0]=a[i];
dp2[i][0]=a[i];
}
for(int j=1;(1<<j)<=n;j++)
{
for(int i=1;i+(1<<j)-1<=n;i++)///白書上的模板,次行稍作改動,否則dp陣列要擴大一倍防止RE
{
dp1[i][j]=max(dp1[i][j-1],dp1[i+(1<<(j-1))][j-1]);
dp2[i][j]=min(dp2[i][j-1],dp2[i+(1<<(j-1))][j-1]);
}
}
}
int rmq(int x,int y)
{
int k=0;
while((1<<(k+1))<=y-x+1)k++;
return max(dp1[x][k],dp1[y-(1<<k)+1][k])-min(dp2[x][k],dp2[y-(1<<k)+1][k]);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)break;
init();
for(int i=1;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dfs1(1,-1);
dfs2(1,-1);
for(int i=1;i<=n;i++)
{
a[i]=maxn[i];
}
RMQ_init(n);
while(m--)
{
int Q;
scanf("%d",&Q);
int ans=0;
int id=1;
for(int i=1;i<=n;i++)
{
while(id<=i&&rmq(id,i)>Q)id++;
ans=max(ans,i-id+1);
}
printf("%d\n",ans);
}
}
return 0;
}
相關文章
- HDU 6035 Colorful Tree(樹形DP)
- 樹:基本樹形
- 線段樹 transformation——hdu 4578ORM
- HDU2665 Kth number【主席樹】
- HDU4417 Super Mario【主席樹】
- 樹形DP!
- 樹形DP
- 樹上染色(樹形dp)
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- 樹形結構
- HDU1251 統計難題 【字典樹】
- HDU-4348 - To the moon (主席樹+區間修改)
- HDU 1541 & POJ 2352 Stars (樹狀陣列)陣列
- HDU 1394 Minimum Inversion Number (暴力+線段樹)
- HDU 2795 Billboard(線段樹 區間最大)
- Transformation HDU - 4578線段樹綜合操作ORM
- 劃分樹模板+模板題--hdu4251
- hdu--4417Super Mario+劃分樹
- HDU6035-Colorful Tree-虛樹思想
- [筆記]樹形dp筆記
- oracle樹形查詢Oracle
- 樹上的等差數列 [樹形dp]
- [樹形dp][HAOI2015]樹上染色
- HDU 2689 Sort it【樹狀陣列求逆序對】陣列
- HDU 3074 Multiply game(線段樹 單點更新)GAM
- HDU 3333 Turing Tree(線段樹+離線操作)
- hdu 1698 線段樹 一段更新染色
- jQuery zTree 展示樹形表格jQuery
- 樹形DP二三知識
- 樹形問題選講
- 樹形結構處理
- HDU 1754 I Hate It (線段樹 區間最值)
- vue的樹形控制元件Vue控制元件
- 前端框架——Layui(樹形表格treeGrid )前端框架UI
- 第歸方法建立樹形圖
- markdown樹形結構生成工具
- 遞迴函式-樹形列表遞迴函式
- treeSelect樹形選擇器使用
- HDU 1542 Atlantis (線段樹+離散化+掃描線)