Walking RaceDescription
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 3Sample Output
3Hint
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[n] ,在数列的d中求一个最长的区间,使得区间中的最大值与最小值的差不超过m。
【分析】
实际上这是一道很水的题目。
这里涉及到一个 树的直径问题,树的直径就是树上距离最远的一条路径。而从一个点出发的最长路径,一定是以直径的一个端点为终点的。
不过不知道也没所谓,反正两个dfs也可以求最长路径啦。
后面就是一个序列求最长区间,使max-min<=m的问题了。
设l[i]为以i为末尾的最长合法区间长度,我们知道这个是有一定单调性的,即l[i]<=l[i-1]+1,所以我们弄个指针不断右移就好了。
现在问题变成判断这个区间是否合法,那就是要求出max[a...b]和min[a...b],b不断递增,max和min都有单调性,可以用单调队列维护。
时间复杂度是O(n)。
我一开始用RMQ求最值MLE了,听说用线段树求最值可以过~~
RMQ的MLE代码如下:
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define Maxn 1000001 struct node { int x,y,c,next; }t[2*Maxn];int len; int first[Maxn]; void ins(int x,int y,int c) { t[++len].x=x;t[len].y=y;t[len].c=c; t[len].next=first[x];first[x]=len; } int mymax(int x,int y) {return x>y?x:y;} int mymin(int x,int y) {return x<y?x:y;} int n,m; int f[Maxn],g[Maxn],d[Maxn],v[Maxn]; void dfs(int x,int fa) { for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa) { int y=t[i].y; v[y]=t[i].c; dfs(y,x); f[x]=mymax(f[x],f[y]+t[i].c); } } void dfs2(int x,int fa) { int st=-1,now=-1; for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa) { int y=t[i].y; if(st==-1||f[y]+v[y]>f[st]+v[st]) st=y; } for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa&&t[i].y!=st) { int y=t[i].y; if(now==-1||f[now]+v[now]<f[y]+v[y]) now=y; g[y]=mymax(f[st]+v[st],g[x])+t[i].c; dfs2(y,x); } if(st!=-1) { g[st]=mymax(g[x],f[now]+v[now])+v[st]; dfs2(st,x); } } int mx[Maxn][18],mn[Maxn][18]; void rmq_init() { for(int i=1;i<=n;i++) mx[i][0]=mn[i][0]=d[i]; for(int i=n;i>=1;i--) for(int j=1;i+(1<<j)-1<=n;j++) { mx[i][j]=mymax(mx[i][j-1],mx[i+(1<<j-1)][j-1]); mn[i][j]=mymin(mn[i][j-1],mn[i+(1<<j-1)][j-1]); } } int get_ans(int l,int r) { int st=0; while(l+(1<<st)<=r) st++; st--; return mymax(mx[l][st],mx[r-(1<<st)+1][st])-mymin(mn[l][st],mn[r-(1<<st)+1][st]); } int main() { scanf("%d%d",&n,&m); memset(first,0,sizeof(first)); for(int i=2;i<=n;i++) { int x,y; scanf("%d%d",&x,&y); ins(i,x,y);ins(x,i,y); } memset(f,0,sizeof(f)); memset(g,0,sizeof(g)); memset(d,0,sizeof(d)); dfs(1,0); dfs2(1,0); for(int i=1;i<=n;i++) d[i]=mymax(f[i],g[i]); memset(mx,0,sizeof(mx)); memset(mn,63,sizeof(mn)); rmq_init(); int now=1,ans=0; for(int i=1;i<=n;i++) { while(get_ans(now,i)>m&&now<i) now++; ans=mymax(ans,i-now+1); } printf("%d ",ans); return 0; }
单调队列AC代码如下:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define Maxn 1000100 8 9 struct node 10 { 11 int x,y,c,next; 12 }t[2*Maxn];int len; 13 int first[Maxn]; 14 15 void ins(int x,int y,int c) 16 { 17 t[++len].x=x;t[len].y=y;t[len].c=c; 18 t[len].next=first[x];first[x]=len; 19 } 20 21 int mymax(int x,int y) {return x>y?x:y;} 22 int mymin(int x,int y) {return x<y?x:y;} 23 24 int n,m; 25 int f[Maxn],g[Maxn],d[Maxn],v[Maxn]; 26 void dfs(int x,int fa) 27 { 28 for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa) 29 { 30 int y=t[i].y; 31 v[y]=t[i].c; 32 dfs(y,x); 33 f[x]=mymax(f[x],f[y]+t[i].c); 34 } 35 } 36 37 void dfs2(int x,int fa) 38 { 39 int st=-1,now=-1; 40 for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa) 41 { 42 int y=t[i].y; 43 if(st==-1||f[y]+v[y]>f[st]+v[st]) st=y; 44 } 45 for(int i=first[x];i;i=t[i].next) if(t[i].y!=fa&&t[i].y!=st) 46 { 47 int y=t[i].y; 48 if(now==-1||f[now]+v[now]<f[y]+v[y]) now=y; 49 g[y]=mymax(f[st]+v[st],g[x])+t[i].c; 50 dfs2(y,x); 51 } 52 if(st!=-1) 53 { 54 g[st]=mymax(g[x],f[now]+v[now])+v[st]; 55 dfs2(st,x); 56 } 57 } 58 59 int mx[Maxn],mn[Maxn],edx[Maxn],edn[Maxn]; 60 int sx=0,sn=0; 61 62 void add(int x,int y) 63 { 64 while(mx[sx]<=y&&sx>0) sx--; 65 mx[++sx]=y;edx[sx]=x; 66 while(mn[sn]>=y&&sn>0) sn--; 67 mn[++sn]=y;edn[sn]=x; 68 } 69 70 int ax=1,an=1; 71 int get_ans(int x) 72 { 73 if(ax>sx) ax=sx; 74 while(x>edx[ax]) ax++; 75 if(an>sn) an=sn; 76 while(x>edn[an]) an++; 77 return mx[ax]-mn[an]; 78 } 79 80 int main() 81 { 82 scanf("%d%d",&n,&m); 83 memset(first,0,sizeof(first)); 84 for(int i=2;i<=n;i++) 85 { 86 int x,y; 87 scanf("%d%d",&x,&y); 88 ins(i,x,y);ins(x,i,y); 89 } 90 memset(f,0,sizeof(f)); 91 memset(g,0,sizeof(g)); 92 memset(d,0,sizeof(d)); 93 dfs(1,0); 94 dfs2(1,0); 95 for(int i=1;i<=n;i++) d[i]=mymax(f[i],g[i]); 96 memset(mx,0,sizeof(mx)); 97 memset(mn,63,sizeof(mn)); 98 int now=1,ans=0; 99 for(int i=1;i<=n;i++) 100 { 101 add(i,d[i]); 102 while(get_ans(now)>m&&now<i) now++; 103 ans=mymax(ans,i-now+1); 104 } 105 printf("%d ",ans); 106 return 0; 107 }
2016-10-17 14:03:00