CF1714F.Build a Tree and That Is It

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A tree is a connected undirected graph without cycles. Note that in this problem, we are talking about not rooted trees.

You are given four positive integers n,d12,d23n, d_{12}, d_{23} and d31d_{31}. Construct a tree such that:

  • it contains nn vertices numbered from 11 to nn,
  • the distance (length of the shortest path) from vertex 11 to vertex 22 is d12d_{12},
  • distance from vertex 22 to vertex 33 is d23d_{23},
  • the distance from vertex 33 to vertex 11 is d31d_{31}.

Output any tree that satisfies all the requirements above, or determine that no such tree exists.

树是一个无环的连通无向图。注意,在本题中,我们讨论的是非有根树(即不指定根节点的树)。

给定四个正整数 n,d12,d23n, d_{12}, d_{23} 和 d31d_{31}。请构造一棵满足以下条件的树:

  • 该树包含 nn 个顶点,编号为 11 到 nn;
  • 顶点 11 到顶点 22 的距离(即最短路径的长度)为 d12d_{12};
  • 顶点 22 到顶点 33 的距离为 d23d_{23};
  • 顶点 33 到顶点 11 的距离为 d31d_{31}。

输出任意一棵满足上述所有条件的树;若不存在这样的树,则判定其不存在。

输入格式

The first line of the input contains an integer tt (1≤t≤1041 \le t \le 10^4) —the number of test cases in the test.

This is followed by tt test cases, each written on a separate line.

Each test case consists of four positive integers n,d12,d23n, d_{12}, d_{23} and d31d_{31} (3≤n≤2⋅105;1≤d12,d23,d31≤n−13 \le n \le 2\cdot10^5; 1 \le d_{12}, d_{23}, d_{31} \le n-1).

It is guaranteed that the sum of nn values for all test cases does not exceed 2⋅1052\cdot10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。

接下来是 tt 个测试用例,每个测试用例占单独一行。

每个测试用例包含四个正整数 n,d12,d23n, d_{12}, d_{23} 和 d31d_{31}(3≤n≤2⋅1053 \le n \le 2\cdot10^5;1≤d12,d23,d31≤n−11 \le d_{12}, d_{23}, d_{31} \le n-1)。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, print YES if the suitable tree exists, and NO otherwise.

If the answer is positive, print another n−1n-1 line each containing a description of an edge of the tree — a pair of positive integers xi,yix_i, y_i, which means that the iith edge connects vertices xix_i and yiy_i.

The edges and vertices of the edges can be printed in any order. If there are several suitable trees, output any of them.

对于每个测试用例,如果存在满足条件的树,则输出 YES;否则输出 NO。

如果答案为 YES,则再输出 n−1n-1 行,每行描述树中的一条边——即一对正整数 xi,yix_i, y_i,表示第 ii 条边连接顶点 xix_i 和 yiy_i。

边及其顶点的输出顺序可以任意。如果存在多个满足条件的树,输出任意一个即可。

输入输出样例

  • 输入#1

    9
    5 1 2 1
    5 2 2 2
    5 2 2 3
    5 2 2 4
    5 3 2 3
    4 2 1 1
    4 3 1 1
    4 1 2 3
    7 1 4 1

    输出#1

    YES
    1 2
    4 1
    3 1
    2 5
    YES
    4 3
    2 5
    1 5
    5 3
    NO
    YES
    2 4
    4 1
    2 5
    5 3
    YES
    5 4
    4 1
    2 5
    3 5
    YES
    2 3
    3 4
    1 3
    NO
    YES
    4 3
    1 2
    2 4
    NO

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

首页