問題一覧 > 通常問題

No.1719 Tree and Permutation

レベル : / AI推定難易度 : 1600 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 28
作問者 : nok0 / テスター : zkou Kite_kuma ygussany
お気に入りにしたユーザー ProblemId : 7010 / 自分の提出
問題文最終更新日: 2021-10-16 18:44:34
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 319 (順位表) の他の問題:

Problem Statement

You are given a tree with $N$ vertices.

The vertices are numbered from $1$ to $N$, and the edges are numbered from $1$ to $N-1$. Edge $i$ bidirectionally connects vertices $A_i$ and $B_i$.

Determine whether there exists a permutation $P$ obtained by rearranging $(1,2,\dots,N)$ that satisfies the following condition.

  • For every tuple $(i,j,k)$ of distinct integers between $1$ and $N$, inclusive, at least one of the following $2$ conditions holds.
    • The simple path from vertex $i$ to vertex $j$ does not contain vertex $k$.
    • The simple path from vertex $P_i$ to vertex $P_j$ does not contain vertex $P_k$.

For each input file, solve $T$ test cases.

Constraints

  • All input values are integers.
  • $1 \le T\le 10^5$
  • $3 \le N \le 10^5 $
  • $1 \le A_i,B_i \le N$
  • The given graph is a tree.
  • For $1$ input file, the sum of $N$ is at most $5\times 10^5$.

Input

The input is given from standard input in the following format.
$T$
$\mathrm{case}_1$
$\mathrm{case}_2$
$\vdots$
$\mathrm{case}_T$
Each test case is given in the following format.
$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_{N-1}$ $B_{N-1}$

Output

Print $T$ lines. On line $i\ (1\le i \le T)$, for the $i$-th test case, print Yes if a permutation $P$ satisfying the condition exists, and No otherwise.

Sample

Sample 1
Input
3
4
1 2
2 3
2 4
5
1 2
2 3
3 4
4 5
7
1 3
6 2
2 5
3 7
7 4
5 1
Output
Yes
No
No

In the first test case, for example, $P=(2,1,3,4)$ satisfies the condition.

In the second and third test cases, it can be proved that no permutation $P$ satisfying the condition exists.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。