Contingency Plan 2

시간 제한2초메모리 제한1024 MB

요약
트리가 주어질 때, 위상 정렬 순서가 정확히 하나가 되도록 방향 간선을 최소 개수만큼 추가하고 그 간선들을 출력한다.
난이도

어려움10점 중 8점

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

문제

You are working as a manager in The ICPC Company. In the company building, there are NN computers (numbered from 11 to NN). There are N−1N - 1 cables, numbered from 11 to N−1N - 1, that connect all the computers into a single network. Cable ii connects computer U_iU\_i and V_iV\_i. Each cable can be set into emergency mode, where cable ii only transfers data from computer U_iU\_i to computer V_iV\_i, but not the other way around. During a disaster, it is mandatory for all cables to be in emergency mode.

Through your research, you discover a new way to determine the vulnerability of a network. You want to add zero or more new cables to the current network such that it is not vulnerable during a disaster. Your network is not vulnerable if and only if there is exactly one permutation of 11 to NN such that uu appears before vv in the permutation for all cables that connect computer uu and vv. In other words, it should have exactly one topological order.

The following illustration shows examples of not vulnerable networks and vulnerable networks.

For the not vulnerable networks, the only permutation that satisfies the requirement for the networks on the left and on the right are \[1,2,3]\[1, 2, 3] and \[3,1,2]\[3, 1, 2], respectively. Meanwhile, for the vulnerable networks, there are 22 permutations that satisfy the requirement for the network on the left: \[1,2,3]\[1, 2, 3] and \[3,1,2]\[3, 1, 2]; while there is no permutation that satisfies the requirement for the network on the right.

You are interested in the minimum number of new cables that should be added to the current network such that it is not vulnerable during a disaster. Furthermore, you want to know, which pairs of computers should be connected by using the minimum number of cables. If there are several ways to connect, you can connect in any way of your choice. Under the given constraints, it can be proven that there exists a way to make the network not vulnerable.

입력

The first line consists of an integer NN (2≤N≤100,0002 ≤ N ≤ 100\\, 000).

Each of the next N−1N - 1 lines consists of two integers U_iU\_i V_iV\_i (1≤U_i,V_i≤N1 ≤ U\_i , V\_i ≤ N). The input edges form a tree.

출력

The first line consists of an integer, representing the minimum number of new cables that should be added to the current network such that it is no longer vulnerable during a disaster. Denote this number as KK and the new cables are numbered from 11 to KK.

If KK is not 00, then output KK lines. Each of the next KK lines consists of two integers A_iA\_i B_iB\_i, representing the computers that are connected by the new cable ii. During a disaster, new cable ii only transfers data from computer A_iA\_i to computer B_iB\_i, but not the other way around. If there exist several solutions, you can output any of them.

예제3

  1. 예제 1

    입력
    3
    1 2
    3 2
    
    예상 출력
    1
    3 1
    
  2. 예제 2

    입력
    3
    1 2
    2 3
    
    예상 출력
    0
    
  3. 예제 3

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