SGU 134.Centroid (tree dp)

Source: Internet
Author: User

An entry tree DP, seeking the center of gravity of a tree ... How bored I am to write this question ... Silly x inscribed also no egg use later or less write good.

----------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 16009;int SZ[MAXN], N;int ANS[MAXN], n, ansv;struct Edge {int to;edge* Next;} E[MAXN << 1], *pt = E, *HEAD[MAXN];void Addedge (int u, int v) {pt->to = v; pt->next = head[u]; Head[u] = pt++;} void Init () {scanf ("%d", &n);for (int i = 1; i < N; i++) {int u, v; scanf ("%d%d", &u, &v); u--; v-- ;Addedge (U, v);Addedge (V, u);}n = 0;ansv = MAXN;}void Dfs (int x, int fa =-1) {sz[x] = 1;int mx = 0;For (edge* e = head[x]; e; e = e->next) if (e->to! = FA) {DFS (e->to, x);mx = max (mx, sz[e->to]);Sz[x] + = sz[e->to];}mx = max (mx, n-sz[x]);if (MX < ANSV)ansv = mx, ans[0] = x, n = 1;else if (mx = = ansv)ans[n++] = x;}int main () {Init ();DFS (0);printf ("%d%d\n", ANSV, N);sort (ans, ans + N);for (int i = 0; i < n; i++)printf ("%d", ++ans[i]);return 0;}

----------------------------------------------------------------

134. centroid

Time limit per test:0.25 sec.
Memory limit per test:4096 KB

You is given an undirected connected graph, with N vertices and N-1 edges (a tree). You must find the centroid (s) of the tree.
In order to define the centroid, some integer value would be assosciated to every vertex. Let ' s consider the vertex K. If We remove the vertex K from the tree (along with its adjacent edges), the remaining graph'll has only N -1 vertices and May is composed of more than one connected components. Each of these are (obviously) a tree. The value associated to vertex K are the largest number of vertices contained by some connected component in the R Emaining graph, after the removal of vertex K. All of the vertices for which the associated value was minimum is considered centroids.

Input

The first line of the input contains the integer number N (1<=n<=16). The next N-1 lines would contain the integers, a and b, separated by blanks, meaning that there Exists an edge between vertex a and vertex b.

Output

You should print the lines. The first line should contain the minimum value associated to the centroid (s) and the number of centroids. The second line should contain the list of vertices which is centroids, sorted in ascending order.

Sample Input

71 22 32 41 55 66 7

Sample Output

3 11
Author : Mugurel Ionut Andreica
Resource : Ssu::online contester Fall Contest #2
Date : Fall 2002
Pasting

SGU 134.Centroid (tree dp)

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.