The point division of the tree
Can see 09 years of Lacquer Super paper, said very detailed.
Tree
| Time Limit: 1000MS |
|
Memory Limit: 30000K |
| Total Submissions: 12650 |
|
Accepted: 4025 |
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]
/* ***********************************************author:ckbosscreated time:2015 April 24 Friday 09:37 44 seconds file Name :P oj1741_2.cpp************************************************ * * #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <cstdlib > #include <vector> #include <queue> #include <set> #include <map>using namespace Std;const int Inf=0x3f3f3f3f;const int maxn=10100;struct Node {int to,len;}; int n,k;vector<node> g[maxn];bool done[maxn];int size,root;int s[maxn],f[maxn],d[maxn];vector<int> dep; int ans;void findRoot (int u,int fa) {s[u]=1; f[u]=0;int rest=0;for (int i=0,sz=g[u].size (); i<sz;i++) {int v=g[u][i].to ; if (v==fa| | DONE[V]) continue;findroot (v,u); Rest+=s[v];f[u]=max (F[u],s[v]);} S[u]+=rest;f[u]=max (F[u],size-s[u]); if (F[u]<f[root]) root=u;} void getdeep (int u,int fa) {dep.push_back (d[u]); s[u]=1;for (int i=0,sz=g[u].size (); i<sz;i++) {int v=g[u][i].to;if (v==fa| | DONE[V]) Continue;int len=g[u][i].len;d[v]=d[u]+len;getdeep (v,u); s[u]+=s[v];}} int calc (int u,int init) {dep.clear (); D[u]=init;getdeep (u,-1); sort (Dep.begin (), Dep.end ()), int ret=0;for (int l=0,r= Dep.size () -1;l<r;) {if (dep[l]+dep[r]<=k) Ret+=r-l++;else r--;} return ret;} void solve (int u) {ans+=calc (u,0);d one[u]=true;for (int i=0,sz=g[u].size (); i<sz;i++) {int v=g[u][i].to;if (DONE[V]) Continue;int Len=g[u][i].len;ans-=calc (V,len);///recursive subtree size=s[v]; root=0; FindRoot (v,u); solve (root);}} int main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); F[0]=inf;while (scanf ("%d%d", &n,&k)!=eof) {if (n==0&&k==0) break; memset (done,false,sizeof (done)), for (int i=0;i<=n+10;i++) g[i].clear (), for (int i=0;i<n-1;i++) {int a,b,c;scanf ("%d%d%d", &a,&b,&c); G[a].push_back (Node) {b,c}); G[b].push_back ((Node) {a,c});} Size=n; Ans=0;findroot (1,-1); solve (root);p rintf ("%d\n", ans);} return 0;}
POJ 1741 Tree + point division