POJ 1655 balancing Act Focus Tree

Source: Internet
Author: User

Title effect: given a tree. After removing a point, the tree will become a chunk of some Chinese unicom. The point is then removed by seeking the maximum number of minimum formed blocks.


Thinking: Tree dp thinking. Determined by deep search for the root node of each subtree size. After that the node is then removed, and there is the remainder. Find the maximum number of these blocks. is to remove this point when the ANS. Then update the total ans.

The problem is actually the center of the tree.


CODE:


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 20010using namespace Std;int cases;int points;int head[max],total;int next[max << 1],aim[max << 1];int P_ans,  ans;inline void Initialize (), inline void Add (int x,int y), int DFS (int x,int last), int main () {for (CIN >> cases;cases; --cases) {scanf ("%d", &points); Initialize (); for (int x,y,i = 1;i < points; ++i) {scanf ("%d%d", &x,&y); Add (x, y), add (y,x);} DFS (1,0);p rintf ("%d%d\n", P_ans,ans);} return 0;} inline void Initialize () {ans = 0x7f7f7f7f,total = 0;memset (head,0,sizeof (Head));} inline void Add (int x,int y) {next[++total] = head[x];aim[total] = y;head[x] = total;} int DFS (int x,int last) {int max_size = 0,size = 1;for (int i = Head[x];i;i = Next[i]) {if (aim[i] = = last) Continue;int Temp = DFS (aim[i],x); max_size = max (max_size,temp); size + = temp;} Max_size = max (max_size,points-size); if (max_size < ans) ans = Max_size,p_ans = X;return size;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

POJ 1655 balancing Act Focus 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.