[Winter DAY1T3] Tree

Source: Internet
Author: User

Title Description Description
For a tree of n nodes without root, the number of simple paths of path length =k
Enter a description input Description

First row two positive integers n,k

Next N-1 line, two positive integers per line x, Y, representing an edge (x, y)

Outputs description output Description
One line an integer, the answer
Sample input to sample
4 21 22) 32 4
Sample output Sample Outputs
3
Data Size & Hint
For 30% 2<=k,n<=1000

For 100%,2<=k,n<=10^5

Or a point division of the problem, and POJ1741 the only difference is that one is to find the path <=k, one is to find equals K. The only difference between the two algorithms is the difference between dealing with the dis array. For the number of O (n) in an ordered table to satisfy the sum of two numbers and equal to K, I can not think of any better method, so the processing of the ANS when the writing of the more ugly.

#include <iostream>#include<cmath>#include<algorithm>#include<cstring>#include<cstdio>#include<queue>using namespaceStd;typedefLong LongLL;#definePi ACOs (-1.0)inlineintRead () {intx=0, f=1;CharC=GetChar ();  while(!isdigit (c)) {if(c=='-') f=-1; c=GetChar ();}  while(IsDigit (c)) {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}Const intmaxn=100010;structedge{intU,v,next; Edge () {} Edge (int_1,int_2,int_3): U (_1), V (_2), Next (_3) {}}e[2*MAXN];intFirst[maxn],n,k,a,b,size[maxn],masize[maxn],now_size,root,dis[maxn],end;BOOLVIS[MAXN]; LL ans;voidAddedge (intIintAintb) {E[i]=Edge (A,b,first[a]); First[a]=i;}voidGetsintUintPA) {Size[u]=1; Masize[u]=0;  for(inti=first[u];i!=-1; i=e[i].next)if(!VIS[E[I].V] && e[i].v!=PA)            {gets (e[i].v,u); Size[u]+=SIZE[E[I].V]; Masize[u]=Max (Size[e[i].v],masize[u]); }}voidGETR (intRintUintPA) {Masize[u]=max (masize[u],size[r]-Size[u]); if(masize[u]<now_size) now_size=masize[u],root=u;  for(inti=first[u];i!=-1; i=e[i].next)if(!VIS[E[I].V] && e[i].v!=pa) getr (r,e[i].v,u);}voidGETD (intUintPaintd) {Dis[end++]=D;  for(inti=first[u];i!=-1; i=e[i].next)if(!VIS[E[I].V] && e[i].v!=pa) getd (e[i].v,u,d+1);} LL Calc (intUintd) {End=0; GETD (U,-1, D); LL ret=0; intL=0, r=end-1, L,r; Sort (Dis,dis+end); //cout<<u<< ":";//for (int i=0;i<end;i++) cout<<dis[i]<< ";//cout<<endl;     while(l<r) { while(dis[l]+dis[r]>k && l<r) r--; if(dis[l]+dis[r]==k) {if(dis[l]==Dis[r]) {ret+ = ((LL) (r-l+1) * (LL) (r-l)/2);  Break; }            Else{L=r=1;  while(dis[r-1]==dis[r]) R--, r++;  while(dis[l+1]==DIS[L]) l++,l++; RET+ = (ll) l* (LL) R); L=r=0; R--;l++; }        }        Elsel++; }    returnret;}voidDfsintu) {Now_size=N; Gets (U,-1); Getr (U,u,-1); Ans+=calc (Root,0); Vis[root]=1;  for(inti=first[root];i!=-1; i=e[i].next)if(!VIS[E[I].V]) {ans-=calc (E[I].V,1);        DFS (E[I].V); }    return;}intMain () {Freopen ("tree.in","R", stdin); Freopen ("Tree.out","W", stdout); memset (First,-1,sizeof(first)); N=read (); k=read ();  for(intI=0; i<n-1; i++) {a=read (); b=read (); Addedge (i*2, A, b); Addedge (i*2+1, B,a); } DFS (1); printf ("%lld\n", ans); return 0;}

[Winter DAY1T3] Tree

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.