Complete Mirror

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

요약
트리에서 같은 거리에 있는 모든 정점의 차수가 같아지는 루트 정점을 찾고, 없으면 -1을 출력한다.
난이도

보통10점 중 7점

유형
트리, DFS, 구현
정답자
아직 제출이 없습니다

문제

You have given tree consist of nn vertices. Select a vertex as root vertex that satisfies the condition below.

  • For all vertices v_1v\_{1} and v_2v\_{2}, if distancedistance(rootroot, v_1v\_{1}) =distance= distance(rootroot, v_2)v\_{2}) then degreedegree(v_1v\_{1}) =degree= degree(v_2v\_{2}), where degreedegree means the number of vertices connected to that vertex, and distancedistance means the number of edges between two vertices.

Determine and find if there is such root vertex in the tree. If there are multiple answers, find any of them.

입력

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^{5}) --- the number of vertices.

Each of the next n−1n-1 lines contains two integers v_iv\_{i} and u_iu\_{i} (1≤v_i<u_i≤n1 \le v\_{i} \lt u\_{i} \le n) --- it means there is an edge exist between v_iv\_{i} and u_iu\_{i}. It is guaranteed that the graph forms tree.

출력

If there is such root vertex exists, print any of them. Otherwise, print −1-1.

힌트

This is the picture for the first example. 11, 55, 77 also can be a valid answer.

This is the picture for the second example. You can see that it's impossible to find such root vertex.

예제2

  1. 예제 1

    입력
    7
    1 2
    2 3
    3 4
    4 5
    3 6
    6 7
    
    예상 출력
    3
    
  2. 예제 2

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