A study record of point division in a tree

Source: Internet
Author: User

Follow the engine room of the trend of the point of Division, found that this thing is actually very good to write, learn to think, it is easy to yy out. directly on the exercise.
POJ 1741 TREE
Point division of the template problem, the first place x To the current subtree and r o o t Span style= "Display:inline-block; width:0px; Height:2.563em; " > The distance is g x , it satisfies g x + g y ≤k You can add the answer, but note that if x y Span style= "Display:inline-block; width:0px; Height:2.723em; " > In the same subtrees tree, it is necessary to delete the contribution to the answer, thinking x y Span style= "Display:inline-block; width:0px; Height:2.723em; " > is calculated once at the China in which it resides. Note that there is no root tree to the root tree process, you need to select the tree's center of gravity to prevent complexity from O(N log 2 N) Back to o ( n 2 ) Code

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define INF 100000Using namespace Std;struct hp{intU,v,w;} a[20001];struct hq{int size;}tree[10001];bool done[10001];intpoint[10001],Next[20001];intlen[10001];intNm, e=0, t=0;intsize,root,sizenow,tot,ans=0; void Add (intUintVintW) {e++; a[e].u=u; a[e].v=v; a[e].w=w;Next[E]=point[u];    Point[u]=e; e++; A[e].u=v; A[e].v=u; A[e].w=w;Next[E]=point[v]; Point[v]=e;} void Findsize (intNowint  Last){inti,tmp=0; sizenow++; for(i=point[now];i;i=Next[i])if(a[i].v!= Last&AMP;&AMP;!DONE[A[I].V]) findsize (A[i].v,now);} void Findroot (intNowint  Last){inti,tmp=0; Tree[now].size=1; tmp=0; for(i=point[now];i;i=Next[i])if(a[i].v!= Last&AMP;&AMP;!DONE[A[I].V]) {findroot (A[i].v,now); Tree[now].size+=tree[a[i].v].size;if(tree[a[i].v].size>tmp) Tmp=tree[a[i].v].size; }if(sizenow-tree[now].size>tmp) Tmp=sizenow-tree[now].size;if(tmp<size) {root=now; size=tmp;}} void Findroad (intNowint  Last,intRoad) {inti,tmp=0; Len[++t]=road;if(road>m)return; for(i=point[now];i;i=Next[i])if(a[i].v!= Last&AMP;&AMP;!DONE[A[I].V]) findroad (A[I].V,NOW,ROAD+A[I].W);}intCalcintK) {inti,l,r,ret=0;Sort(len+1, len+t+1); for(l=1,r=t;l<r;) {if(len[l]+len[r]<=k) ret+=r-l++;Elser--; }returnRET;} void Work (intNow) {intI sizenow=0; t=0;    Size=inf; Findsize (now,0); Findroot (now,0); Findroad (Root,0,0);    Done[root]=true; Ans+=calc (m); for(i=point[root];i;i=Next[i])if(!DONE[A[I].V]) {t=0; sizenow=0;          Size=inf; Findroad (A[I].V,0,0); Ans-=calc (m-2*a[I].W]; Findsize (A[I].V,0); Findroot (A[I].V,0);            Work (A[I].V); }}intMain () {intIx,y, Z; scanf"%d%d",&n,&m); while(n!=0&&m!=0) {e=0; ans=0; memset (Point,0, sizeof (point)); memset (done,false,sizeof (done)); for(i=1; i<=n-1; ++i) {scanf (" %d%d%d",&x,&y, &z); Addx,y, z); } work (1);printf("%d\ n", ans); scanf"%d%d",&n,&m); }}

A study record of point division in a tree

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.