Point Division Treatment
How to call the tree title again ... It's really drunk.
This paper is titled "The Application of divide and conquer algorithm in tree path problem"
Puzzle: http://blog.csdn.net/sdj222555/article/details/7893862
http://blog.csdn.net/yang_7_46/article/details/9966455
Since it is a point of treatment, in order to ensure that the complexity is not degraded, it is necessary to find the center of gravity of the tree, this step can be implemented by a DFS: any node as the root (for convenience on the 1th node is selected) to find the size of each node tree, then the current nodes will be divided into size[x],n-size[x] two parts, We go to the larger part as the subtree size of the current node (just record a value, don't care which one is the root), and then we can compare it to the global variable root at DFS to each node f[x] and F[root] if F[x]<f[root] root= X (the center of gravity is represented here) (because of F[X]>=N/2, so f[x] the smallest x is the most average "center of gravity" to divide the whole tree
When you find the center of gravity, you need to count all points (n-1) to the center of gravity, counting how many points are less than or equal to K. The same time Dfs is implemented, and then the distance of all nodes to the center of gravity exists in an array, a quick row ~ and then the use of monotonicity, a single-to-one search in O (n) time resolved. But this is an extra count: two points in the same subtree are not counted, so subtract two points in the same subtree (the recursive solution to the subtree will be counted once). Of course, we have to recursively count the points in each subtree.
1 //POJ 17412#include <cstdio>3#include <vector>4#include <cstring>5#include <cstdlib>6#include <iostream>7#include <algorithm>8 #defineRep (i,n) for (int i=0;i<n;++i)9 #defineF (i,j,n) for (int i=j;i<=n;++i)Ten #defineD (i,j,n) for (int i=j;i>=n;--i) One #definePB Push_back A using namespacestd; - voidReadint&v) { -v=0;intsign=1;CharCh=GetChar (); the while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} - while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} -v*=Sign ; - } + /******************tamplate*********************/ - Const intn=10086; + structedge{ A intto,l; at }; - intn,k,size,s[n],f[n],root,d[n],ans,k; -Vector<edge>G[n]; -vector<int>DEP; - BOOLVis[n]; - in voidGetroot (intXintFA) { - inty; tos[x]=1; f[x]=0; + Rep (I,g[x].size ()) { -y=g[x][i].to; the if(Y!=FA &&!)Vis[y]) { * Getroot (y,x); $s[x]+=S[y];Panax Notoginsengf[x]=Max (f[x],s[y]); - } the } +F[x]=max (f[x],size-s[x]); A if(F[x]<f[root]) root=x; the } + voidGETDEP (intXintFA) { - inty; $ DEP.PB (d[x]); $s[x]=1; - Rep (I,g[x].size ()) { -y=g[x][i].to; the if(Y!=FA &&!)Vis[y]) { -d[y]=d[x]+G[X][I].L;Wuyi GETDEP (y,x); thes[x]+=S[y]; - } Wu } - } About intCalcintXintinit) { $Dep.clear (); d[x]=Init; -GETDEP (x,0); - sort (Dep.begin (), Dep.end ()); - intans=0; A for(intL=0, R=dep.size ()-1; l<R;) + if(dep[l]+dep[r]<=k) ans+=r-l++; the Elser--; - returnans; $ } the voidWorkintx) { the inty; theAns+=calc (x,0); thevis[x]=1; - Rep (I,g[x].size ()) { iny=g[x][i].to; the if(!Vis[y]) { theans-=Calc (Y,G[X][I].L); Aboutf[0]=size=S[y]; theGetroot (y,root=0); the Work (root); the } + } - } the intMain () {Bayi //freopen ("1741.in", "R", stdin); the while(SCANF ("%d%d", &n,&k) = =2){ the if(n==0&& k==0) Break; - intx, y, z -F (I,1, N) G[i].clear (); thememset (Vis,0,sizeofvis); theF (I,2, N) { the Read (x); Read ( y); read (z); the G[X].PB (Edge) {y,z}); G[Y].PB (Edge) {x,z}); - } thef[0]=size=N; theGetroot (1, root=0); theans=0;94 Work (root); theprintf"%d\n", ans); the } the return 0;98}
View Code
"POJ" "1741" Tree