Social Net ZOJ, socialnetzoj
Social Net ZOJ-3649
Question:
I cannot understand the original question...
Reference: http://www.cnblogs.com/names-yc/p/4922867.html
A graph is provided to find the maximum Spanning Tree and the sum of output edge weights, and query the tree: Two node numbers x and y are given, and the path from x to y is obtained, the range in a sequence consisting of the weights of each node-requires that the subtrahend be followed by the subtrahend, that is, the {a1, a2... Aj... Ak... An}, calculate the maximum value of ak-aj (k> = j.
Practice:
First, kruskal calculates the maximum spanning tree. Then multiply dp.
P [I] indicates the weight of node I.
Anc [I] [j] indicates the 2nd ^ j-level ancestor of node I
According to the multiplication principle, there are:
Maxn [I] [j] indicates to start from node I and go up. A total of 2 ^ j nodes (including node I) are taken over. The maximum value of the midpoint weight of these nodes
$ Maxn [I] [0] = p [I] $
$ Maxn [I] [j] = max (maxn [I] [J-1], maxn [anc [I] [J-1] [J-1]) $
Minn [I] [j] indicates that the node starts from node I and goes up. A total of 2 ^ j nodes (including node I) are taken over. The minimum value of the midpoint weight of these nodes
$ Minn [I] [0] = p [I] $
$ Minn [I] [j] = min (minn [I] [J-1], minn [anc [I] [J-1] [J-1]) $
Maxans [I] [j] indicates to start from node I and go up. A total of 2 ^ j nodes (including node I) are passed ), the weights of these nodes are sequential {a1, a2... Aj... Ak... In an}, calculate the maximum value of ak-aj (k> = j.
$ Maxans [I] [0] = 0 $
$ Maxans [I] [j] = max (maxans [I] [J-1], maxans [anc [I] [J-1] [J-1], maxn [anc [I] [J-1] [J-1]-minn [I] [J-1]) $
It indicates that the maximum difference value is either generated in the first half or in the last half, or the maximum value in the last half minus the minimum value in the first half.
Minans [I] [j] indicates to start from node I and go up. A total of 2 ^ j nodes (including node I) are passed ), the weights of these nodes are sequential {a1, a2... Aj... Ak... In an}, calculate the maximum value of ak-aj (k <= j. (The name of this array is not really correct... don't worry about it)
$ Minans [I] [0] = 0 $
$ Minans [I] [j] = max (minans [I] [J-1], minans [anc [I] [J-1] [J-1], maxn [I] [J-1]-minn [anc [I] [J-1] [J-1]) $
It indicates that the maximum difference value is either generated in the first half or in the last half, or the maximum value in the first half minus the minimum value in the last half.
All of the above can be completed in the dfs process.
The process of finding the result can also be considered to multiply the lca. However, in the process of "moving up", the answer of the moved part must be updated to the current answer.
Set lca (x, y) to z. The path from x to y can be regarded as x to z, z and then to y. The answer is max (the maximum answer from x to z, the maximum answer from z to y, and the minimum value from z-x to z, the maximum-z weight in the path z to y, and the maximum answer in the path z to y-x to the maximum answer in the path z to y ).
When moving x up, the biggest answer after moving up is max (the biggest answer that x has produced, the maximum value of the part to be moved up-the minimum value of the part that x has passed, the maximum answer to the part to be moved up (in maxans )).
When moving y up, the biggest answer after moving up is max (the biggest answer that y has generated, the maximum answer that y has passed-the minimum value of the part to be moved up, the maximum answer to the part to be moved up (in minans )).
Error records (local ):
1. Because x and y are ordered, 47 rows cannot be written.
2. Lines and 64 are missing
3. lines-78 are missing. Due to the write in this method, the current point will not be updated to the existing answer during the migration process.
Http://www.xuebuyuan.com/1162519.html
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 struct E1 7 { 8 int a,b,w; 9 friend bool operator<(const E1& a,const E1& b) 10 { 11 return a.w>b.w; 12 } 13 }e1[50100]; 14 struct Edge 15 { 16 int to,d,nxt; 17 }e[100100]; 18 int fa[50100],p[50100],n,m,ne,f1[50100],log2n,deep[50100],q,ans1; 19 int minn[50100][17],maxn[50100][17],minans[50100][17],maxans[50100][17],anc[50100][17]; 20 int find(int x) 21 { 22 return fa[x]==x?x:fa[x]=find(fa[x]); 23 } 24 void dfs(int x,int fa) 25 { 26 int i,k; 27 minn[x][0]=maxn[x][0]=p[x]; 28 //minans[x][0]=maxans[x][0]=0; 29 for(i=1;i<=log2n;i++) 30 { 31 anc[x][i]=anc[anc[x][i-1]][i-1]; 32 minn[x][i]=min(minn[x][i-1],minn[anc[x][i-1]][i-1]); 33 maxn[x][i]=max(maxn[x][i-1],maxn[anc[x][i-1]][i-1]); 34 minans[x][i]=max(max(minans[anc[x][i-1]][i-1],minans[x][i-1]),maxn[x][i-1]-minn[anc[x][i-1]][i-1]); 35 maxans[x][i]=max(max(maxans[anc[x][i-1]][i-1],maxans[x][i-1]),maxn[anc[x][i-1]][i-1]-minn[x][i-1]); 36 } 37 for(k=f1[x];k!=0;k=e[k].nxt) 38 if(e[k].to!=fa) 39 { 40 deep[e[k].to]=deep[x]+1; 41 anc[e[k].to][0]=x; 42 dfs(e[k].to,x); 43 } 44 } 45 int get(int x,int y) 46 { 47 //if(deep[x]<deep[y]) swap(x,y); 48 int t,i,ansx=0,ansy=0,minx=p[x],maxy=p[y]; 49 for(t=deep[x]-deep[y],i=0;t>0;t>>=1,i++) 50 if(t&1) 51 { 52 ansx=max(ansx,max(maxans[x][i],maxn[x][i]-minx)); 53 minx=min(minx,minn[x][i]); 54 x=anc[x][i]; 55 } 56 for(t=deep[y]-deep[x],i=0;t>0;t>>=1,i++) 57 if(t&1) 58 { 59 ansy=max(ansy,max(minans[y][i],maxy-minn[y][i])); 60 maxy=max(maxy,maxn[y][i]); 61 y=anc[y][i]; 62 } 63 if(x==y) 64 return max(max(ansx,ansy),maxy-minx); 65 for(i=log2n;i>=0;i--) 66 if(anc[x][i]!=anc[y][i]) 67 { 68 ansx=max(ansx,max(maxans[x][i],maxn[x][i]-minx)); 69 minx=min(minx,minn[x][i]); 70 ansy=max(ansy,max(minans[y][i],maxy-minn[y][i])); 71 maxy=max(maxy,maxn[y][i]); 72 x=anc[x][i]; 73 y=anc[y][i]; 74 } 75 ansx=max(ansx,max(maxans[x][0],maxn[x][0]-minx)); 76 minx=min(minx,minn[x][0]); 77 ansy=max(ansy,max(minans[y][0],maxy-minn[y][0])); 78 maxy=max(maxy,maxn[y][0]); 79 return max(max(max(ansx,ansy),maxy-minx),max(p[anc[x][0]]-minx,maxy-p[anc[y][0]])); 80 } 81 int main() 82 { 83 int i,t1,t2,a,b; 84 while(scanf("%d",&n)==1) 85 { 86 log2n=log2(n); 87 ne=0;ans1=0; 88 memset(f1,0,sizeof(f1)); 89 memset(minn,0x3f,sizeof(minn)); 90 memset(maxn,0,sizeof(maxn)); 91 memset(minans,0,sizeof(minans)); 92 memset(maxans,0,sizeof(maxans)); 93 for(i=1;i<=n;i++) 94 scanf("%d",&p[i]); 95 scanf("%d",&m); 96 for(i=1;i<=m;i++) 97 scanf("%d%d%d",&e1[i].a,&e1[i].b,&e1[i].w); 98 sort(e1+1,e1+m+1); 99 for(i=1;i<=n;i++)100 fa[i]=i;101 for(i=1;i<=m;i++)102 {103 t1=find(e1[i].a);104 t2=find(e1[i].b);105 if(t1==t2) continue;106 e[++ne].to=e1[i].b;107 e[ne].nxt=f1[e1[i].a];108 e[ne].d=e1[i].w;109 f1[e1[i].a]=ne;110 e[++ne].to=e1[i].a;111 e[ne].nxt=f1[e1[i].b];112 e[ne].d=e1[i].w;113 f1[e1[i].b]=ne;114 fa[t1]=t2;115 ans1+=e1[i].w;116 }117 printf("%d\n",ans1);118 dfs(1,0);119 scanf("%d",&q);120 while(q--)121 {122 scanf("%d%d",&a,&b);123 printf("%d\n",get(a,b));124 }125 }126 return 0;127 }