CF1583B.Omkar and Heavenly Tree

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Lord Omkar would like to have a tree with nn nodes (3≤n≤1053 \le n \le 10^5) and has asked his disciples to construct the tree. However, Lord Omkar has created mm (1≤m<n\mathbf{1 \le m \lt n}) restrictions to ensure that the tree will be as heavenly as possible.

A tree with nn nodes is an connected undirected graph with nn nodes and n−1n-1 edges. Note that for any two nodes, there is exactly one simple path between them, where a simple path is a path between two nodes that does not contain any node more than once.

Here is an example of a tree:

A restriction consists of 33 pairwise distinct integers, aa, bb, and cc (1≤a,b,c≤n1 \le a,b,c \le n). It signifies that node bb cannot lie on the simple path between node aa and node cc.

Can you help Lord Omkar and become his most trusted disciple? You will need to find heavenly trees for multiple sets of restrictions. It can be shown that a heavenly tree will always exist for any set of restrictions under the given constraints.

奥姆卡尔大人希望拥有一棵包含 nn 个节点(3≤n≤1053 \le n \le 10^5)的树,并已要求其门徒构造这棵树。然而,为确保该树尽可能“神圣”,奥姆卡尔大人制定了 mm(1≤m<n\mathbf{1 \le m \lt n})条限制条件。

一棵含 nn 个节点的树是一个具有 nn 个节点和 n−1n-1 条边的连通无向图。注意:对任意两个节点,它们之间恰好存在一条简单路径;所谓简单路径,是指连接两个节点、且不重复经过任一节点的路径。

以下是一棵树的示例:

每条限制条件由三个两两互异的整数 aa、bb 和 cc(1≤a,b,c≤n1 \le a,b,c \le n)组成,表示节点 bb 不能位于节点 aa 与节点 cc 之间的简单路径上。

您能否助奥姆卡尔大人一臂之力,成为他最信赖的门徒?您需要为多组不同的限制条件分别构造出满足要求的“神圣之树”。可以证明:在题目给定的约束条件下,对任意一组限制条件,总存在至少一棵满足所有限制的神圣之树。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \leq t \leq 10^4). Description of the test cases follows.

The first line of each test case contains two integers, nn and mm (3≤n≤1053 \leq n \leq 10^5, 1≤m<n\mathbf{1 \leq m \lt n}), representing the size of the tree and the number of restrictions.

The ii-th of the next mm lines contains three integers aia_i, bib_i, cic_i (1≤ai,bi,ci≤n1 \le a_i, b_i, c_i \le n, aa, bb, cc are distinct), signifying that node bib_i cannot lie on the simple path between nodes aia_i and cic_i.

It is guaranteed that the sum of nn across all test cases will not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \leq t \leq 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(3≤n≤1053 \leq n \leq 10^5,1≤m<n\mathbf{1 \leq m \lt n}),分别表示树的大小和限制条件的数量。

接下来的 mm 行中,第 ii 行包含三个整数 aia_i、bib_i、cic_i(1≤ai,bi,ci≤n1 \le a_i, b_i, c_i \le n,且 aia_i、bib_i、cic_i 互不相同),表示节点 bib_i 不能位于节点 aia_i 与 cic_i 之间的简单路径上。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, output n−1n-1 lines representing the n−1n-1 edges in the tree. On each line, output two integers uu and vv (1≤u,v≤n1 \le u, v \le n, u≠vu \neq v) signifying that there is an edge between nodes uu and vv. Given edges have to form a tree that satisfies Omkar's restrictions.

对于每个测试用例,输出 n−1n-1 行,表示树中的 n−1n-1 条边。每行输出两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n,且 u≠vu \neq v),表示节点 uu 与节点 vv 之间存在一条边。所给出的边必须构成一棵满足 Omkar 限制条件的树。

输入输出样例

  • 输入#1

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

    输出#1

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

说明/提示

The output of the first sample case corresponds to the following tree:

For the first restriction, the simple path between 11 and 33 is 1,31, 3, which doesn't contain 22. The simple path between 33 and 55 is 3,53, 5, which doesn't contain 44. The simple path between 55 and 77 is 5,3,1,2,75, 3, 1, 2, 7, which doesn't contain 66. The simple path between 66 and 44 is 6,7,2,1,3,46, 7, 2, 1, 3, 4, which doesn't contain 55. Thus, this tree meets all of the restrictions.

The output of the second sample case corresponds to the following tree:

第一个样例的输出对应如下树:

对于第一条限制:节点 11 与 33 之间的简单路径为 1,31, 3,不包含节点 22;节点 33 与 55 之间的简单路径为 3,53, 5,不包含节点 44;节点 55 与 77 之间的简单路径为 5,3,1,2,75, 3, 1, 2, 7,不包含节点 66;节点 66 与 44 之间的简单路径为 6,7,2,1,3,46, 7, 2, 1, 3, 4,不包含节点 55。因此,该树满足所有限制条件。

第二个样例的输出对应如下树:

输入解题思路,AI测评打分。不知道怎么写?

首页