QOJ.ac

QOJ

시간 제한: 1.0 s 메모리 제한: 256 MB 총점: 100 해킹 가능 ✓
통계

给你一个由小灰(Little Grey)记录的直径端点对集合,这是他在拥有一棵有 $N$ 个节点的树时写下的。现在他忘记了这棵树本身。你的任务是重构任意一棵含有 $N$ 个带标号节点的树,使得该树中距离等于树直径的无序节点对集合恰好为给定的集合——或者报告他的记忆是不一致的(即不存在这样的树)。

树的直径是树中最长的简单路径;其长度是任意两个顶点之间的最大距离。如果 $u$ 和 $v$ 之间的距离等于树的直径长度,则称对 $(u, v)$ 为直径端点对。给定的集合包含无序对,并且可以以任何顺序排列。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 2 \cdot 10^5$) —— 测试用例的数量。

每个测试用例的第一行包含三个整数 $N, M$ ($2 \le N \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^5$) —— 节点的数量和记录的无序对的数量。

接下来是 $M$ 行,每行包含两个整数 $u$ 和 $v$ ($1 \le u, v \le N$, $u \ne v$),表示小灰记忆中距离等于树直径长度的一个无序对。

在同一个测试用例中,所有 $M$ 个对都是互不相同的。

保证所有测试用例的 $\sum N \le 2 \cdot 10^5$,$\sum M \le 2 \cdot 10^5$。

输出格式

对于每个测试用例,输出:

如果存在一棵节点为 $1 \dots N$ 的树,其距离等于树直径的无序节点对集合恰好为给定的集合,则输出一行 YES,然后输出 $N - 1$ 行,每行描述该树的一条边 $(a, b)$(每行一条边,节点之间用空格分隔;该树必须是连通且无环的)。

如果存在多个满足条件的树,你可以输出其中任意一个。

否则,输出单行 NO

样例

输入样例 1

3
2 1
1 2
3 2
1 2
2 3
4 3
1 2
2 3
1 3

输出样例 1

YES
1 2
NO
YES
4 1
4 2
4 3

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.