Algorithm: POJ 1655 balancing Act (tree DP)

Source: Internet
Author: User
Tags continue int size

Meaning

An N-node tree that, when removed from a node, becomes a forest.

Each tree in this forest has a number of nodes, where the maximum number of nodes is set to Max.

Ask how much is max when you delete a node?

Ideas

Exactly the same as poj-3107 Godfather! Don't want to spit the groove ...

In fact, did not want to send this article, but found today is the last day of August, but also one of the 80 published. So.. It's a wicked water.

Code

/**===================================================== * is a solution for ACM/ICPC problem * * @source: POJ -1655 Balancing Act * @description: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshuangde
@gmail. com * Copyright (C) 2013/08/31 15:32 All rights reserved. *======================================================*/#include <iostream> #include <cstdio> #
    
Include <algorithm> #include <vector> #include <cstring> using namespace std;
typedef pair<int, int >PII;
typedef long long Int64;
    
const int INF = 0X3F3F3F3F;
const int MAXN = 20010;
int TOT[MAXN];
    
int F[MAXN], minx, id; namespace ADJ {int size, HEAD[MAXN]; struct node{int V, Next;}
E[MAXN*2];
inline void Initadj () {size = 0; memset (head,-1, sizeof (head));} inline void Addedge (int u, int v) {e[size].v = v;
E[size].next = Head[u];
Head[u] = size++;
    
The using namespace Adj;
    
int n; int dfs (int u, int fa) {Tot[u] = 1;/count//http://www.bianceng.cn for (int e = head[u]; e!=-1; e = e[e].next) {int v = e[e].v; if (v = = FA) Continue T
Ot[u] + = DFS (v, u);
//Save answer int& ans = f[u] = N-tot[u]; for (int e = head[u]; e!=-1; e = e[e].next) {int v = e[e].v; if (v = = FA) continue; ans = max (ans, tot[v]);} if (Ans & Lt
Minx) {minx = ans; id = u;} else if (ans = = Minx) {id = min (id, u);} return tot[u];
    
int main () {int ncase; scanf ("%d", &ncase);
    
while (ncase--) {scanf ("%d", &n);
Initadj ();
for (int i = 0; i < n-1 ++i) {int u, v scanf ("%d%d", &u, &v); Addedge (U, v); Addedge (v, u);} minx = INF;
DFS (1,-1);
printf ("%d%d\n", ID, Minx);
return 0; }

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.