#include #include // #include // https://github.com/amiast/cpp-utility #ifndef KOTONE_PRUEFER_HPP #define KOTONE_PRUEFER_HPP #include #include #include namespace kotone { // Returns the Pruefer code of the specified undirected tree. // Requires `tree` to be a valid representation of an undirected tree. // Requires `tree.size() >= 2`. // Requires `0 <= tree[i][j] < tree.size()` for all `0 <= i < tree.size()` and `0 <= j < tree[i].size()`. // Reference: https://info.atcoder.jp/entry/algorithm_lectures/prufer_code std::vector encode_pruefer(const std::vector> &tree) { assert(int(tree.size()) >= 2); int n = tree.size(); std::vector deg(n); for (int i = 0; i < n; i++) deg[i] = tree[i].size(); std::priority_queue, std::greater> pq; for (int i = 0; i < n; i++) if (deg[i] == 1) pq.push(i); std::vector result; for (int i = 0; i < n - 2; i++) { int u = pq.top(); pq.pop(); deg[u] = 0; for (int v : tree[u]) { assert(0 <= v && v < n); if (deg[v] == 0) continue; result.push_back(v); if (--deg[v] == 1) pq.push(v); } } return result; } // Returns the adjacency list of the undirected tree corresponding to the specified Pruefer code. // The order of vertices in each list is undefined. // Requires `code` to be a valid representation of a Pruefer code. // Requires `0 <= code[i] < code.size() + 2`. // Reference: https://info.atcoder.jp/entry/algorithm_lectures/prufer_code std::vector> decode_pruefer(const std::vector &code) { int n = code.size() + 2; std::vector count(n); for (int p : code) { assert(0 <= p && p < n); count[p]++; } std::priority_queue, std::greater> pq; for (int i = 0; i < n; i++) if (count[i] == 0) pq.push(i); std::vector> tree(n); for (int i = 0; i < n - 2; i++) { int u = pq.top(); pq.pop(); int v = code[i]; tree[u].push_back(v); tree[v].push_back(u); if (--count[v] == 0) pq.push(v); } int u = pq.top(); pq.pop(); int v = pq.top(); tree[u].push_back(v); tree[v].push_back(u); return tree; } } // namespace kotone #endif // KOTONE_PRUEFER_HPP void alice() { int N; std::cin >> N; std::cout << N - 2 << std::endl; std::vector> tree(N); for (int i = 1; i < N; i++) { int u, v; std::cin >> u >> v; u--, v--; tree[u].push_back(v); tree[v].push_back(u); } for (int p : kotone::encode_pruefer(tree)) std::cout << p << ' '; std::cout << std::endl; } void bob() { int N; std::cin >> N; std::cout << N - 2 << std::endl; std::vector code(N - 2); for (int &p : code) std::cin >> p; auto tree = kotone::decode_pruefer(code); for (int i = 0; i < N; i++) { for (int j : tree[i]) { if (i < j) std::cout << i + 1 << ' ' << j + 1 << std::endl; } } } int main() { std::string S; std::cin >> S; if (S == "Alice") alice(); else bob(); }