CF2247E.Build a Tree
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given two integers n and k.
Construct a tree∗ with n vertices such that i=1∑ndist(i,(imodn)+1)=k†, or determine that no such tree exists.
∗A tree is a connected graph without cycles.
†dist(i,j) is the number of edges on the shortest path from vertex i to vertex j in the tree.
给你两个整数 n 和 k。
请构造一棵包含 n 个顶点的树∗,使得 i=1∑ndist(i,(imodn)+1)=k†;若不存在这样的树,则判定其不存在。
∗树是一类无环的连通图。
†dist(i,j) 表示该树中从顶点 i 到顶点 j 的最短路径所包含的边数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The only line of each test case contains two integers n and k (2≤n≤2⋅105, 0≤k≤n2) — the number of vertices in the tree and the required value of k.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是测试用例的描述。
每个测试用例仅一行,包含两个整数 n 和 k(2≤n≤2⋅105,0≤k≤n2)——分别为树中顶点的数量和所需的 k 值。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, if there is no solution, output −1.
Otherwise, output n−1 lines. Each line should contain two integers u and v (1≤u,v≤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。
否则,输出 n−1 行。每行应包含两个整数 u 和 v(1≤u,v≤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). Therefore, dist(1,2)+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.
In the fourth example, it can be shown that no suitable tree exists.
在第一个例子中,树仅包含一条边 (1,2)。因此,dist(1,2)+dist(2,1)=1+1=2。
在第二个例子中,一棵可能的树如下所示。

对于该树,dist(1,2)+dist(2,3)+dist(3,4)+dist(4,1)=1+2+2+1=6。
在第四个例子中,可以证明不存在满足条件的树。
输入解题思路,AI测评打分。不知道怎么写?