Description
A tree, the statistical distance is not greater than \ (K\) the number of points.
Sol
Point division of the rule.
Find yourself quick to forget the point of separation of the clean ...
Find the center of gravity so that all the son's maximum value as small as possible, and then every time to deal with all the subtree, and then subtract the contribution of each subtree, so that the sub-tree contributions, and then the subtree can be, this is a sub-problem.
Code
/************************************************************** problem:3365 User:beiyu language:c++ Result : Accepted time:352 ms memory:6128 kb****************************************************************/#include <c stdio> #include <utility> #include <vector> #include <queue> #include <algorithm> #include <iostream>using namespace Std; typedef pair< INT,INT > PR; #define MPR Make_pair#define Debug (a) cout<< #a << "=" <<a<< "" const int N = 40005; int n,m,k,rt,sz,ans;vector<pr> g[n];int s[n],f[n],b[n];vector<int> d;int q[N],h,t; inline int in (int x=0,char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x= (x<<3) + (x<<1) +ch-' 0 ', Ch=getchar (); return x; }void getroot (int u,int fa) {s[u]=1,f[u]=0; for (int i=0,v;i<g[u].size (); i++) if ((V=g[u][i].first)!=fa &&!b[v]) {getroot (v,u), S[u]+=s[v],f[u]=max (f[ U],S[V]); }f[u]=mAx (F[u],sz-s[u]); if (F[u]<f[rt]) rt=u;} void Getdeep (int u,int fa,int dep) {d.push_back (DEP), s[u]=1; for (int i=0,v;i<g[u].size (); i++) if ((V=g[u][i].first)!=fa &&!b[v]) getdeep (V,u,dep+g[u][i].second), S[u] +=S[V];} int Calc (int u,int w) {d.clear (), Getdeep (U,U,W); Sort (D.begin (), D.end ()); int res=0; for (int l=0,r=d.size () -1;l<r;) if (d[l]+d[r]<=k) Res+=r-l,l++;else r--; return res;} void Getans (int u) {ans+=calc (u,0); b[u]=1; for (int i=0,v;i<g[u].size (); i++) if (!b[v=g[u][i].first]) Ans-=calc (V,g[u][i].second), F[0]=sz=s[v],getroot (v,rt =0), Getans (RT);} int main () {//Freopen ("In.in", "R", stdin); N=in (), M=in (); for (int i=1,u,v,w;i<=m;i++) u=in (), V=in (), W=in (), G[u].push_back (MPR (V,W)), G[v].push_back (MPR (U,W)); K=in (); F[rt=0]=sz=n; Getroot (1,0); Getans (RT); cout<<ans<<endl; return 0;}
Bzoj 3365: [Usaco2004 feb]distance Statistics Distance Statistics