"HAOI2009" "P1307" caterpillar

Source: Internet
Author: User

The feeling is much simpler than the other trees, but it's a little bit around (maybe my train of thought is strange.) A) (this is the province of the topic Ah, even as T1 this problem is also too water, ha good weak ... )

Original question:

For a tree, we can pull a chain and the edge connected to the chain, it looks like a caterpillar, the more points, the bigger the caterpillar. For example, the tree on the left (Figure 1) pulls part of it into a caterpillar on the right (Figure 2).

n≤300000

First get out of the tree without root, this time not enumerate the middle point, say my strange practice one. One

Make two arrays, one is UF, which means the immediate maximum, including yourself, and the other is the BF, which represents the largest of the UF in the brother of X and x down

And then we ask for UF and BF

uf[x]=bf[tree[x].child]+tree[x].cnum;//do not have to reduce UF the biggest son of the inside, because still have their own

If x is a leaf, that is child[x]==0,uf[x]=1

Bf[x]=max (Uf[x],bf[tree[x].brother]);

Because the root is not necessarily in the answer, so set a global maximum ans, after the UF and BF, Ans=max (ans,uf[x]+bf[tree[x].brother]+tree[tree[x].father].cnum-(tree[x). Father==1));

There is no need to subtract two sons, because there are father and master, but when Tree[x].father==1 (I set the root to 1) to 1, because the root has no father

The last direct output of ans can

(Updating the answer with the global maximum should be a lot of DP policies)

Code:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 intRead () {intz=0, mark=1;CharCh=GetChar ();8      while(ch<'0'|| Ch>'9'){if(ch=='-') mark=-1; Ch=GetChar ();}9      while(ch>='0'&&ch<='9') {z= (z<<3) + (z<<1) +ch-'0'; Ch=GetChar ();}Ten     returnz*Mark; One } A structddd{intNext,y;} e[610000];intlink[310000],ltop=0; -InlinevoidInsertintXintY) {e[++ltop].next=link[x]; link[x]=ltop;e[ltop].y=y;} - structdcd{intBrother,child,father, Cnum;} tree[310000]; theInlinevoidInsert_tree (intXintY) {tree[y].brother=tree[x].child;tree[x].child=y;tree[y].father=x; tree[x].cnum++;} - intn,m; - intuf[310000],bf[310000];//BF represents the largest of many brothers, UF represents the direct maximum - intans=0; + voidGet_tree (intx) { -      for(intI=link[x];i;i=e[i].next)if(e[i].y!=tree[x].father) { + Insert_tree (X,E[I].Y); A Get_tree (E[I].Y); at     } - } - voidDp_tree (intx) { -     if(!x)return ; - Dp_tree (tree[x].brother); -     if(tree[x].child) { in Dp_tree (tree[x].child); -Uf[x]=bf[tree[x].child]+tree[x].cnum;//Do not reduce UF the biggest son of the inside, because there is also his own to     } +     Else -uf[x]=1; thebf[x]=Max (Uf[x],bf[tree[x].brother]); *Ans=max (ans,uf[x]+bf[tree[x].brother]+tree[tree[x].father].cnum-(tree[x].father==1));//not minus two sons, because there's Daddy and grandpa. $ }Panax Notoginseng intMain () {//freopen ("ddd.in", "R", stdin); -memset (UF,0,sizeof(UF)); thememset (BF,0,sizeof(BF)); +cin>>n>>m;//In fact, M is equal to N-1 bar one. A A     int_left,_right; the      for(intI=1; i<=m;i++) {_left=read (), _right=read (); Insert (_left,_right), insert (_right,_left);} +Get_tree (1); -Dp_tree (1); $cout<<ans<<Endl; $     return 0; -}
View Code

"HAOI2009" "P1307" caterpillar

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.