QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 2048 MB Total points: 100

#10338. Annual Ants' Gathering

統計

森林深处有一棵古老的树,树上有 $n$ 个蚂蚁窝,编号从 1 到 $n$,由树的枝干连接。

树枝上的蚂蚁

每年,所有的蚂蚁都需要聚集在一起观看 EUC。为此,所有蚂蚁都要沿着它们所居住的树的 $n - 1$ 条枝干移动,在其中一个蚂蚁窝会合。

然而,今年蚂蚁们无法就聚会地点达成一致,需要你的帮助来聚集它们。如果两个窝 $u$ 和 $v$ 之间有直接相连的枝干,你可以命令当前在窝 $u$ 的所有蚂蚁移动到窝 $v$。但是,如果窝 $v$ 中的蚂蚁数量少于窝 $u$ 中的蚂蚁数量,即如果从窝 $v$ 移动蚂蚁更容易,那么蚂蚁会忽略你的命令。即使窝 $v$ 中当前没有任何蚂蚁,这一规则依然适用。你可以根据需要多次发出此类命令。

请问是否可以将所有蚂蚁聚集在同一个窝中?

输入格式

第一行包含一个整数 $n$ ($1 \le n \le 200\,000$),表示蚂蚁窝的数量。

接下来的 $n - 1$ 行,每行包含两个整数 $u$ 和 $v$ ($1 \le u, v \le n$),表示窝 $u$ 和窝 $v$ 之间有直接相连的枝干。

保证每只蚂蚁都可以通过树的枝干到达任何其他蚂蚁的窝。

输出格式

如果可以将所有蚂蚁聚集在同一个窝中,输出 YES,否则输出 NO。

样例

输入 1

7
5 1
3 2
4 6
3 6
7 1
1 3

输出 1

YES

说明 1

你可以按照以下步骤将所有蚂蚁聚集在窝 3:

  • 命令窝 4 的蚂蚁移动到窝 6。
  • 命令窝 2 的蚂蚁移动到窝 3。
  • 命令窝 6 的两只蚂蚁移动到窝 3(窝 3 此时已有两只蚂蚁)。
  • 命令窝 5 的蚂蚁移动到窝 1。
  • 命令窝 7 的蚂蚁移动到窝 1(窝 1 此时已有两只蚂蚁)。
  • 命令窝 1 的三只蚂蚁移动到窝 3(窝 3 此时已有四只蚂蚁)。

输入 2

5
1 4
4 2
3 2
5 3

输出 2

NO

说明 2

无法将所有蚂蚁聚集在同一个窝中。

输入 3

6
4 5
5 6
6 1
2 6
3 2

输出 3

YES

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.