CF2247E.Build a Tree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given two integers nn and kk.

Construct a tree∗^{\text{∗}} with nn vertices such that ∑i=1ndist⁡(i,(i mod n)+1)=k\sum\limits_{i = 1}^{n} \operatorname{dist}(i, (i \bmod n) + 1) = k†^{\text{†}}, or determine that no such tree exists.

∗^{\text{∗}}A tree is a connected graph without cycles.

†^{\text{†}}dist⁡(i,j)\operatorname{dist}(i, j) is the number of edges on the shortest path from vertex ii to vertex jj in the tree.

给你两个整数 nn 和 kk。

请构造一棵包含 nn 个顶点的树∗^{\text{∗}},使得 ∑i=1ndist⁡(i,(i mod n)+1)=k\sum\limits_{i = 1}^{n} \operatorname{dist}(i, (i \bmod n) + 1) = k†^{\text{†}};若不存在这样的树,则判定其不存在。

∗^{\text{∗}}树是一类无环的连通图。

†^{\text{†}}dist⁡(i,j)\operatorname{dist}(i, j) 表示该树中从顶点 ii 到顶点 jj 的最短路径所包含的边数。

输入格式

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

The only line of each test case contains two integers nn and kk (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5, 0≤k≤n20 \le k \le n^2) — the number of vertices in the tree and the required value of kk.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例仅一行,包含两个整数 nn 和 kk(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5,0≤k≤n20 \le k \le n^2)——分别为树中顶点的数量和所需的 kk 值。

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

输出格式

For each test case, if there is no solution, output −1-1.

Otherwise, output n−1n - 1 lines. Each line should contain two integers uu and vv (1≤u,v≤n1 \le u, v \le n), denoting an edge of the tree. The edges may be output in any order.

If there are multiple suitable trees, output any of them.

对于每个测试用例,若无解,则输出 −1-1。

否则,输出 n−1n - 1 行。每行应包含两个整数 uu 和 vv(1≤u,v≤n1 \le u, v \le n),表示树中的一条边。边的输出顺序可以任意。

若存在多个满足条件的树,输出其中任意一个即可。

输入输出样例

  • 输入#1

    5
    2 2
    4 6
    5 10
    5 14
    100 8347

    输出#1

    1 2
    1 4
    1 3
    1 2
    3 2
    3 4
    4 1
    5 3
    -1
    -1

说明/提示

In the first example, the tree consists of the single edge (1,2)(1, 2). Therefore, dist⁡(1,2)+dist⁡(2,1)=1+1=2\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 1 + 1 = 2.

In the second example, one possible tree is shown below.

For this tree, dist⁡(1,2)+dist⁡(2,3)+dist⁡(3,4)+dist⁡(4,1)=1+2+2+1=6\operatorname{dist}(1, 2) + \operatorname{dist}(2, 3) + \operatorname{dist}(3, 4) + \operatorname{dist}(4, 1) = 1 + 2 + 2 + 1 = 6.

In the fourth example, it can be shown that no suitable tree exists.

在第一个例子中,树仅包含一条边 (1,2)(1, 2)。因此,dist⁡(1,2)+dist⁡(2,1)=1+1=2\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 1 + 1 = 2。

在第二个例子中,一棵可能的树如下所示。

对于该树,dist⁡(1,2)+dist⁡(2,3)+dist⁡(3,4)+dist⁡(4,1)=1+2+2+1=6\operatorname{dist}(1, 2) + \operatorname{dist}(2, 3) + \operatorname{dist}(3, 4) + \operatorname{dist}(4, 1) = 1 + 2 + 2 + 1 = 6。

在第四个例子中,可以证明不存在满足条件的树。

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

首页