Poj 1655 balancing act tree center of gravity

Source: Internet
Author: User

Question: Here is a tree that asks you for the center of gravity. The new definition of the tree is that if you remove this node, the maximum number of subtree nodes of this node is the minimum.

Solution:

Dynamic Planning, tree-like DP

It is found that the vector performance is worse than that of array pointers.

Solution code:

1 // file name: poj1741.cpp 2 // Author: darkdream 3 // created time:, Sunday, October 05, 2014, 4, 5, # include <vector> 6 # include <list> 7 # include <map> 8 # include <set> 9 # include <deque> 10 # include <stack> 11 # include <bitset> 12 # include <algorithm> 13 # include <functional> 14 # include <numeric> 15 # include <utility> 16 # include <sstream> 17 # include <iostream> 18 # include <iomanip> 19 # include <cstdio> 20 # include <cmat H> 21 # include <cstdlib> 22 # include <cstring> 23 # include <ctime> 24 # define ll long long25 # define maxn 2001026 using namespace STD; 27 int ans; 28 int ansv; 29 int MX [maxn]; 30 int sum [maxn]; 31 int visit [maxn]; 32 vector <int> MP [maxn]; 33 void DFS (int K) 34 {35 // printf ("% d \ n", k); 36 int num = MP [K]. size (); 37 sum [k] = 1; 38 for (INT I = 0; I <num; I ++) 39 {40 if (! Visit [MP [k] [I]) 41 {42 visit [MP [k] [I] = 1; 43 DFS (MP [k] [I]); 44 MX [k] = max (sum [MP [k] [I], MX [k]); 45 sum [k] + = sum [MP [k] [I]; 46} 47} 48} 49 int main () {50 int T; 51 scanf ("% d", & T); 52 while (t --) 53 {54 int N; 55 scanf ("% d", & N); 56 int U, v; 57 for (INT I = 1; I <= N; I ++) 58 MP [I]. clear (); 59 for (INT I = 1; I <n; I ++) 60 {61 scanf ("% d", & U, & V ); 62 MP [u]. push_back (V); 63 MP [v]. push_back (U); 64} 65 memset (MX, 0, sizeof (INT) * (n + 1); 66 memset (visit, 0, sizeof (INT) * (n + 1); 67 visit [1] = 1; 68 DFS (1); 69 ansv = 1e9; 70 for (INT I = 1; I <= N; I ++) 71 {72 int tmx = max (sum [1]-sum [I], MX [I]); 73 If (tmx <ansv) 74 {75 ans = I; 76 ansv = tmx; 77} 78} 79 printf ("% d \ n", ANS, ansv); 80} 81 return 0; 82}
View code

 

Poj 1655 balancing act tree center of gravity

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.