POJ 1741 tree divide and conquer (point divide and cure template problem)

Source: Internet
Author: User

POJ 1741

Test Instructions : Ask for the number of points in a tree to <=k.

Summary : Point division Treatment, do not understand. I probably knocked it over.

#include <iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>#include<string>#include<cmath>#include<queue>#include<stack>#include<map>#include<bitset>#include<vector>#include<Set>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineF (I,A,B) for (int i=a;i<b;i++)#defineFF (I,A,B) for (int i=a;i<=b;i++)#defineMes (A, b) memset (A,b,sizeof (a))#defineINF 0x3f3f3f3ftypedefLong Longll;Const intN = 1e5+ -;intN, K, ans, root, num, maxn;intVis[n], Size[n], dis[n], maxv[n];//size[] Indicates the number of sub-tree nodes, maxv[] represents the maximum number of subtrees, dis[] represents the node to the center of gravity distanceintHead[n], E;structEdge {intV,next,w;} Edge[n];voidInit () {ans=e=0; Mes (head,-1); Mes (Vis,0);}voidAddedge (intUintVintW) {EDGE[E].V=v; EDGE[E].W=W; Edge[e].next=Head[u]; Head[u]=e++;}voidDfssize (intUintFa//calculate the number of nodes in a U-tree{Size[u]=1; maxv[u]=0;  for(intI=head[u]; i!=-1; I=Edge[i].next) {        intv=edge[i].v; if(V!=fa && vis[v]==0) {dfssize (V, u); Size[u]+=Size[v]; if(Size[v]>maxv[u]) maxv[u]=size[v];//find the maximum number of subtrees        }    }}voidDfsroot (intRintUintFa//Calculate the center of gravity of the U-Tree after removing the R tree, reduce the complexity, it seems to use the tree-shaped dp;r as the center of gravity of the tree{    if(Size[r]-size[u] >maxv[u]) maxv[u]=size[r]-size[u];//if the U-tree node is less than the number of U-trees removed by the R-Tree, the U-tree is reversed to remove the node from the U-Tree as a subtree    if(MAXV[U]&LT;MAXN) maxn=maxv[u], root=u;//MAXV is the maximum number of subtrees, Root is the center of gravity     for(intI=head[u]; i!=-1; I=Edge[i].next) {        intv=edge[i].v; if(V!=fa && vis[v]==0) Dfsroot (r,v,u); }}voidDfsdis (intUintDintFa//calculate the distance from each node in the U tree to the center of gravity{dis[num++]=D;  for(intI=head[u]; i!=-1; I=Edge[i].next) {        intv=edge[i].v; if(V!=fa && vis[v]==0) Dfsdis (V, d+edge[i].w, u); }}intCalcintUintD//calculate the number of point-to-dis () <=k in the U-Tree{    intret=0; Num=0;//num represents the number of points in the U-TreeDfsdis (U,d,0); Sort (dis, dis+num); intI=0, j=num-1;  while(I&LT;J) {//Classic, opposite search         while(dis[i]+dis[j]>k && i<j) j--; RET+ = Ji; I++; }    returnret;}voidDfsintu) {MAXN=N; Dfssize (U,0); Dfsroot (U,u,0);//find the center of gravity root in the U treeans+= Calc (Root,0); Vis[root]=1;  for(intI=head[root]; i!=-1; I=edge[i].next) {//from the center of gravity, remove the center of gravity, and then search the sub-tree        intv=edge[i].v; if(vis[v]==0) {ans-= Calc (v, EDGE[I].W);//minus the V subtree, because the DFS (v) below also addsDfs (v); }    }}intMain () { while(~SCANF ("%d%d", &n,&k) && (n&&k)) {Init (); intu,v,l; FF (i,1, N-1) {scanf ("%d%d%d", &u,&v,&l);            Addedge (u,v,l);        Addedge (v,u,l); } DFS (1); printf ("%d\n", ans); }    return 0;}
View Code

POJ 1741 tree divide and conquer (point divide and cure template problem)

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.