Codeforces Round #749 (Div. 1 + Div. 2, based on Technocup 2022 Elimination Round 1)B. Omkar and Hea

博客内容涉及到一个图论问题,即如何为Lord Omkar构建一棵包含n个节点的树,同时满足m个限制条件。每个限制条件规定了三个不同的节点,使得在任何情况下,限制中的节点都不在其他两个节点之间的简单路径上。题目给出多个测试用例,并要求找到满足所有限制条件的树结构,输出树的边来构建这样的树。示例给出了满足条件的树形解决方案。
摘要由CSDN通过智能技术生成

这个题目一看数据1e5就不可能跟建图树挂钩,因此一定是一道思维题目
然而明知如此我还是看了大半晚上,不会做…太菜!
不会做就最好读读题目:我们观察:若限制 a b c,那么b不能在a c的中间路径上,一共m个限制,有m个点不能在a c的中间路径上,又m<n,即至少有一个点可以承担”交通中枢“的作用,形成一个发散状的图,使得满足限制。
另外这个样例图示给的太恶心了,还是需要自己观察(吐血状)
B. Omkar and Heavenly Tree
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Lord Omkar would like to have a tree with n nodes (3≤n≤105) and has asked his disciples to construct the tree. However, Lord Omkar has created m (1≤m<n) restrictions to ensure that the tree will be as heavenly as possible.

A tree with n nodes is an connected undirected graph with n nodes and n−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 3 pairwise distinct integers, a, b, and c (1≤a,b,c≤n). It signifies that node b cannot lie on the simple path between node a and node c.

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.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains two integers, n and m (3≤n≤105, 1≤m<n), representing the size of the tree and the number of restrictions.

The i-th of the next m lines contains three integers ai, bi, ci (1≤ai,bi,ci≤n, a, b, c are distinct), signifying that node bi cannot lie on the simple path between nodes ai and ci.

It is guaranteed that the sum of n across all test cases will not exceed 105.

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

Example
inputCopy
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
outputCopy
1 2
1 3
3 5
3 4
2 7
7 6
5 1
1 3
3 2
2 4
Note
The output of the first sample case corresponds to the following tree:

For the first restriction, the simple path between 1 and 3 is 1,3, which doesn’t contain 2. The simple path between 3 and 5 is 3,5, which doesn’t contain 4. The simple path between 5 and 7 is 5,3,1,2,7, which doesn’t contain 6. The simple path between 6 and 4 is 6,7,2,1,3,4, which doesn’t contain 5. Thus, this tree meets all of the restrictions.
The output of the second sample case corresponds to the following tree:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值