Logo
(追記) (追記ここまで)

21250번 - Binary Search Tree 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB722100.000%

문제

How to check if a tree is a binary search tree?

Someone in a Telegram chat

Binary search tree is a rooted tree, in which:

  • each vertex can have at most one left child and at most one right child,
  • for each non-leaf vertex $x,ドル all vertices in its left subtree are less than $x$. and all vertices in its right subtree are greater than $x$.

You are given a tree with $n$ vertices. Can this tree, being rooted at some vertex, be a binary search tree, and if it can, what vertices can be a root?

입력

The first line contains an integer $n$ (1ドル \le n \le 500000$) --- the number of vertices in the tree.

Each of the next $n - 1$ lines contains two integers $u_i$ and $v_i$ (1ドル \le u_i, v_i \le n$) --- the edges of the tree.

출력

If this tree can't be a binary search tree, output "-1".

Otherwise, output all vertices that can be a root, in increasing order.

제한

예제 입력 1

3
1 2
2 3

예제 출력 1

1 2 3

예제 입력 2

3
1 3
3 2

예제 출력 2

1

예제 입력 3

4
1 3
3 2
2 4

예제 출력 3

-1

예제 입력 4

4
1 2
1 3
1 4

예제 출력 4

-1

힌트

출처

Contest > Open Cup > 2020/2021 Season > Stage 8: Grand Prix of Samara M번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /