<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

134. Centroid

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


You are 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 will 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 will have only N-1 vertices and may be composed of more than one connected components. Each of these components is (obviously) a tree. The value associated to vertex k is the largest number of vertices contained by some connected component in the remaining graph, after the removal of vertex k. All the vertices for which the associated value is minimum are considered centroids.


Input

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


Output

You should print two 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 are centroids, sorted in ascending order.


Sample Input

7
1 2
2 3
2 4
1 5
5 6
6 7
Sample Output

3 1
1
Author	: Mugurel Ionut Andreica
Resource	: SSU::Online Contester Fall Contest #2
Date	: Fall 2002

<|response|>
1. Abridged Problem Statement  
Given a tree with N nodes (numbered 1…N) and N–1 edges, define for each vertex k the value  
   val[k] = size of the largest connected component obtained by removing k (and its incident edges).  
Find the minimum possible val[k], the number of vertices achieving it, and list those vertices in ascending order.

2. Key Observations  
- Removing a node k from a tree splits it into deg(k) separate subtrees—one per neighbor of k.  
- If we root the tree arbitrarily (say at node 1), then for each node u:  
  • Each child v in the rooted tree gives a component of size subtree_size[v] when u is removed.  
  • There is also the “upward” component (the rest of the tree above u) of size N – subtree_size[u].  
- Therefore for each u:  
    val[u] = max( N – subtree_size[u],  max_over_children( subtree_size[child] ) )  
- The centroids are those nodes u for which val[u] is minimized.

3. Full Solution Approach  
1. Read N and build an adjacency list for the tree.  
2. Run a DFS from node 1 (or any arbitrary root) to compute subtree_size[u] = number of nodes in the subtree rooted at u.  
3. For each node u from 1 to N:  
   a. Compute up_size = N – subtree_size[u]  (size of the component above u).  
   b. Let max_child = maximum of subtree_size[v] over all children v of u in the DFS tree.  
   c. Set val[u] = max(up_size, max_child).  
4. Scan val[1..N] to find mn = minimum val[u].  
5. Collect all vertices u with val[u] == mn, sort them.  
6. Output:  
   – First line: mn and the count of centroids  
   – Second line: the list of centroids in ascending order  

Time complexity: O(N) for one DFS and one pass over all nodes and edges.  
Space complexity: O(N) for adjacency lists and auxiliary arrays.

4. C++ Solution
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int n;
vector<vector<int>> adj;
vector<int> tr_sz;

void read() {
    cin >> n;
    adj.assign(n + 1, {});
    for(int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
}

void dfs(int u, int pr) {
    tr_sz[u] = 1;
    for(int v: adj[u]) {
        if(v != pr) {
            dfs(v, u);
            tr_sz[u] += tr_sz[v];
        }
    }
}

void solve() {
    /*
     * Centroid by subtree sizes. Root the tree at vertex 1 and compute tr_sz
     * via DFS. Removing vertex u leaves components: the "up" part of size
     * n - tr_sz[u], and one component per child v of size tr_sz[v]. The value
     * val[u] is the largest such component, i.e. max(n - tr_sz[u], max child
     * subtree). A neighbour v is a child iff tr_sz[v] < tr_sz[u] (its size is
     * smaller under this rooting). The centroids are all vertices attaining the
     * minimum val, listed in ascending order.
     */

    tr_sz.assign(n + 1, 0);
    dfs(1, 1);

    vector<int> val(n + 1, 0);
    int mn = n;
    for(int u = 1; u <= n; u++) {
        val[u] = n - tr_sz[u];
        for(int v: adj[u]) {
            if(tr_sz[v] < tr_sz[u]) {
                val[u] = max(val[u], tr_sz[v]);
            }
        }

        mn = min(mn, val[u]);
    }

    vector<int> li;
    for(int i = 1; i <= n; i++) {
        if(val[i] == mn) {
            li.push_back(i);
        }
    }

    cout << mn << " " << li.size() << '\n';
    for(int x: li) {
        cout << x << " ";
    }
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys
sys.setrecursionlimit(1000000)

def main():
    data = sys.stdin.read().split()
    it = iter(data)
    N = int(next(it))

    # Build adjacency list
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a = int(next(it)); b = int(next(it))
        adj[a].append(b)
        adj[b].append(a)

    # subtree_size[u]: size of subtree rooted at u
    subtree_size = [0]*(N+1)

    # DFS to fill subtree_size
    def dfs(u, p):
        subtree_size[u] = 1
        for v in adj[u]:
            if v == p:
                continue
            dfs(v, u)
            subtree_size[u] += subtree_size[v]

    dfs(1, 0)

    # Compute val[u] = largest component size after removing u
    val = [0]*(N+1)
    mn = N
    for u in range(1, N+1):
        # component above u
        max_comp = N - subtree_size[u]
        # each child v in DFS gives a component of size subtree_size[v]
        for v in adj[u]:
            if subtree_size[v] < subtree_size[u]:
                max_comp = max(max_comp, subtree_size[v])
        val[u] = max_comp
        if max_comp < mn:
            mn = max_comp

    # Collect centroids
    centroids = [u for u in range(1, N+1) if val[u] == mn]

    # Output: minimum value, count, and the list of centroids
    print(mn, len(centroids))
    print(*centroids)

if __name__ == "__main__":
    main()
```