Tree
| Time Limit: 1000MS |
|
Memory Limit: 30000K |
| Total Submissions: 11570 |
|
Accepted: 3626 |
Description
Give a tree with n vertices,each edge have a length (positive integer less than 1001).
Define Dist (u,v) =the min Distance between node U and v.
Give An integer k,for every pair (u,v) of vertices are called valid if and only if Dist (u,v) not exceed K.
Write a program that would count how many pairs which is valid for a given tree.
Input
The input contains several test cases. The first line of all test case contains-integers n, K. (n<=10000) The following n-1 lines each contains three int Egers u,v,l, which means there is an edge between node U and V of length L.
The last test was followed by the zeros.
Output
The For each test case output the answer to a single line.
Sample Input
5 41 2 31 3 11 4 23 5 10 0
Sample Output
8
Source
[email protected]
My point of Division of the first question ~ ~ ~
The paths on the tree fall into two categories:
Through the root node, not through the root node.
Then we can choose each point as the root, and then calculate the path through the length of the root node <=k, and then calculate the number of paths in all of his subtrees in the same way, so that it does not leak.
How do I calculate the number of path strips that pass through the root node and <=k length?
It is possible to subtract all of them in the same subtrees tree.
How to calculate the path of all lengths <=k?
Using DFS to find the depth of each point, there is an array, and then from small to large order, with two pointer sweep: L=1,r=tot, in the process of l added to meet the Dep[l]+dep[r]<=k R pointer is not increased, so O (n) can be solved, plus the O ( NLOGN), the complexity of this operation is O (NLOGN).
So the total complexity is O (recursive layer *nlogn), how to minimize the number of recursive layers?
Each time the center of gravity (looking for the center of Gravity "POJ 1655") is the root node, the number of recursive layers is logn.
So the total complexity of O (nlog^2n) ~
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include < Cstdlib> #define M 10005using namespace std;struct edge{int y,ne,l;} E[m*100];int ans,all,h[m],f[m],done[m],s[m],dep[m],tot=0,size,n,k,root;void addedge (int x,int y,int l) {Tot++;e[tot] . Y=y;e[tot].ne=h[x];e[tot].l=l;h[x]=tot;} void getroot (int x,int fa) {f[x]=0;s[x]=1;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (y==fa| | Done[y]) continue; Getroot (y,x); S[x]+=s[y];f[x]=max (F[x],s[y]);} F[x]=max (f[x],size-s[x]); if (F[x]<f[root]) root=x;} void getdep (int x,int fa,int de) {dep[++all]=de;s[x]=1;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y,l=e[i].l;if (y==fa| | Done[y]) continue; GETDEP (y,x,de+l); s[x]+=s[y];} int calc (int x,int de) {int an=0;all=0; GETDEP (X,0,de); sort (dep+1,dep+1+all); for (int l=1,r=all;l<r;) if (dep[l]+dep[r]<=k) an+=r-l++; else R--;return an;} void Solve (int x) {Ans+=calc (x,0);d one[x]=true;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (done[y]) continue;ans-= Calc (Y,e[i].l); size=f[0]=s[y]; Getroot (y,root=0); Solve (root);}} int main () {while (scanf ("%d%d", &n,&k) ==2) {if (n==k&&n==0) break;tot=0;for (int i=1;i<=n;i++) h[i]= 0,done[i]=false;for (int i=1;i<n;i++) {int x,y,l;scanf ("%d%d%d", &x,&y,&l); Addedge (x,y,l); Addedge (y,x,l);} Ans=0;f[0]=size=n; Getroot (1,root=0); Solve (Root);p rintf ("%d\n", ans); return 0;}
Sentiment:
1.TLE is because solve (root), written solve (y).
2. The key to point division is to know that the path is divided through the root node and not through the root node, thus finding a recursive method.
"POJ 1741" Tree