Algorithm: POJ 3107 godfather (tree DP)

Source: Internet
Author: User
Tags continue int size printf

Meaning

to a N-node tree, the node number is 1~n, ask to delete a node, so that the remaining branches in the largest number of nodes as little as possible.

There may be a variety of scenarios, which are output in numbered order.

Ideas

A simple tree DP. In fact, even the DP can not be counted ... is direct counting statistics

First DFS computes the number of nodes per node tree tot[i].

DFS Update answer again:

F[i] = max (N-tot[i], max{tot[v] | V is son of i});

Two Dfs can be combined in a DFS to complete, complexity O (n)

Code

/**===================================================== * is a solution for ACM/ICPC problem * * @source: poj- 3107 Godfather * @description: Tree DP * @author: Shuangde * @blog: blog.csdn.net/shuangde800 * @email: Zengshuangde@gmai
l.com * Copyright (C) 2013/08/30 16:26 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 = 50010;
int TOT[MAXN];
    
int F[MAXN], minx; 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; tot[u] =
DFS (v, u);
//Calculate 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]); minx = mi
N (minx, ans);
return Tot[u]; int main () {while (~SCANF ("%d", &n)) {Initadj ()-for (int i = 0; i < n-1; ++i) {int u, v; scan
F ("%d%d", &u, &v);
Addedge (U, v);
Addedge (V, u);
} minx = INF;
    
DFS (1,-1);
bool A = true; for (int i = 1; I <= n; ++i) if (f[i] = = Minx) {if (a) = False, printf ("%d", I); else printf ("%d", I);} PU
TS ("");}
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.