Into Cactus

시간 제한1초메모리 제한512 MB

요약
N개 노드로 이루어진 트리가 주어질 때, 어떤 간선도 두 개 이상의 단순 사이클에 속하지 않도록 간선을 최대한 많이 추가하고, 추가한 간선들을 출력한다.
난이도

어려움10점 중 8점

유형
트리, 그래프, DFS, 그리디
정답자
아직 제출이 없습니다

문제

Given a tree, add as many edges as possible so that the resulting graph is a cactus graph.

A cactus graph is a graph where each edge is contained in at most one simple cycle. This graph may not contain self-loops or parallel edges.

입력

The first line contains an integer NN, the size of the tree (1≤N≤200,0001 \le N \le 200\\,000).

Each of the next N−1N - 1 lines contains two integers uu and vv (1≤u,v≤N1 \le u, v \le N, u≠vu \neq v), indicating that there is an edge between nodes uu and vv. It is guaranteed that the resulting graph is a tree.

출력

On the first line, output KK, the maximum number of edges that can be added to the graph. On each of the next KK lines, output two integers aa and bb (1≤a,b≤N1 \le a, b \le N, a≠ba \neq b), indicating that you are going to add an edge between nodes aa and bb. The resulting graph must be a cactus graph.

If there are several solutions with the maximum possible KK, output any one of them.

예제1

  1. 예제 1

    입력
    6
    6 4
    3 1
    3 6
    4 5
    2 3
    
    예상 출력
    2
    2 1
    3 5