http://www.lydsy.com/JudgeOnline/problem.php?id=1468
Divide and conquer is really a big thing on the door ...
Good god ...
The best data for tree division and treatment are: QZC's application of divide-and-conquer algorithm in tree path problem
I say what I understand:
Points = center of gravity + divide and conquer
Finding the center of gravity is especially important because it's about time complexity.
For recursive
$ $T (n) =at (n/b) +o (D (n)) $$
This type of recursion, if you can guarantee that each layer is $o (D (n)) $, then the time complexity will be greatly reduced. (See chapter III and fourth)
For a tree, if we have a linear approach to the current layer after we have found the center of gravity, we can process the complete tree at log level, for example:
Recursive type
$ $T (n.) =at (n/a) +o (n) $$
is a very general point division of the Division, where the complexity of $o (NLGN) $, the specific proof to see the guide. (Very simple .....)
So if we can process each layer in a linear time, the problem can be solved in nlgn time ...
The bare path of the subject is asked ........... ...... For this type of problem, consider the path through each point ....
Preprocess the information of all the subtrees of the current root, then add the root and then merge to get a path through the root! And then it's OK ..... Whatever you do, it's linear.
As long as the current root of all the sub-tree nodes to the root of the distance all run out, after sorting because of monotonous maintenance can ....
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }const int n=40005, oo=~0u>>1;int ihead[n], cnt, K;struct dat {int Next, to, W;} e[n<<1];void Add (int u, int v, int w) {e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;e[++cnt].next =IHEAD[V]; ihead[v]=cnt; E[cnt].to=u; E[cnt].w=w;} int dep[n], d[n], cdep, ans, mn;int root, Sz[n], vis[n];void getroot (int x, int fa, int sum) {sz[x]=1; int y, MX=0;RDM (x, i) if (!vis[y=e[i].to] && e[i].to!=fa) {getroot (y, x, sum) sz[x]+=sz[y];mx=max (MX, sz[y]);} Mx=max (MX, sum-mx); if (mx<mn) mn=mx, root=x;} void Getdep (int x, int fa) {dep[++cdep]=d[x]; int y;//printf ("x:%d\tfa:%d\tdep:%d\n", X, FA, Dep[x]), RDM (x, i) if (!vis[y= E[i].to] && e[i].to!=fa) {d[y]=d[x]+e[i].w;getdep (y, x);}} int cal (int x, int last=0) {cdep=0; d[x]=last;//printf ("Root is:%d\n", x); GETDEP (x,-1);//puts ("======================= = = = "), int ret=0, front=1, Tail=cdep;sort (dep+1, DEP+1+CDEP), while (Front<tail) {while (Front<tail && dep[ tail]+dep[front]>k)--tail;ret+=tail-front;++front;} return ret;} void Dfs (int x, int all) {vis[x]=1; int y;ans+=cAl (x); printf ("root:%d\n", X), RDM (x, i) if (!vis[y=e[i].to]) {ans-=cal (y, E[I].W); int s=sz[y]>sz[x]?all-sz[x]:sz[y]; root=0; Mn=oo; Getroot (y, x, s);d FS (root, s);}} int main () {int n=getint (); Rep (I, n-1) {int u=getint (), V=getint (), W=getint (); Add (U, V, W);} Read (K); Mn=oo;getroot ((n+1) >>1,-1, N);d FS (root, N);p rintf ("%d\n", ans); return 0;}
Description gives you a tree, and the distance from the top of the tree. Ask how many pairs of points they are the distance between the two is less than or equal to KINPUTN (n<=40000) Next n-1 line to describe the pipeline, according to the input written in the title next is the Koutput line, How many pairs of points the distance is less than or equal to Ksample Input7
1 6 13
6 3 9
3 5 7
4 1 3
2 4 20
4 7 2
10
Sample OUTPUT5
HINT Source
LTC Man Eight Questions series
"Bzoj" 1468:tree (Point Division)