<|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++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int N;
// adjacency list: adj[u] is the list of neighbors of u
vector<vector<int>> adj;
// subtree_size[u] = size of subtree rooted at u (including u)
vector<int> subtree_size;
// val[u] = largest component size after removing u
vector<int> val;

// DFS to compute subtree sizes
// u = current node, p = parent of u in DFS tree
void dfs(int u, int p) {
    subtree_size[u] = 1;  // count u itself
    for (int v : adj[u]) {
        if (v == p) continue;      // skip the edge back to parent
        dfs(v, u);
        subtree_size[u] += subtree_size[v];
    }
}

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

    cin >> N;
    adj.assign(N+1, {});
    subtree_size.assign(N+1, 0);
    val.assign(N+1, 0);

    // Read edges
    for (int i = 0; i < N-1; i++) {
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    // 1) Compute subtree sizes with root = 1
    dfs(1, 0);

    // 2) Compute val[u] for each node
    int mn = N;  // initialize minimum value to a large number
    for (int u = 1; u <= N; u++) {
        // size of the component above u
        int max_comp = N - subtree_size[u];
        // for each neighbor v, if v is a child in the DFS tree,
        // its subtree_size[v] is a component when u is removed
        for (int v : adj[u]) {
            if (subtree_size[v] < subtree_size[u]) {
                max_comp = max(max_comp, subtree_size[v]);
            }
        }
        val[u] = max_comp;
        mn = min(mn, val[u]);
    }

    // 3) Collect all centroids
    vector<int> centroids;
    for (int u = 1; u <= N; u++) {
        if (val[u] == mn) {
            centroids.push_back(u);
        }
    }

    // 4) Output results
    cout << mn << " " << centroids.size() << "\n";
    for (int u : centroids) {
        cout << u << " ";
    }
    cout << "\n";

    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()
```