CF2162G.Beautiful Tree
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A tree is a connected graph without cycles.
A tree is called beautiful if the sum of the products of the vertex labels for all its edges is a perfect square.
More formally, let E be the set of edges in the tree. The tree is called beautiful if the value $$S = \sum_{\{u, v\} \in E} (u \cdot v)$$ is a perfect square. That is, there exists an integer x such that S=x2.
You are given an integer n. Your task is to construct a beautiful tree having n vertices or report that such a tree does not exist.
树是无环的连通图。
若一棵树的所有边的两个端点标签乘积之和为完全平方数,则称该树为“优美的”。
更准确地说,设 E 为该树的边集。若值
S={u,v}∈E∑(u⋅v)
为完全平方数,则称该树为优美的。即存在整数 x,使得 S=x2。
给定一个整数 n,你的任务是构造一棵含 n 个顶点的优美树;若不存在这样的树,则报告不存在。
输入格式
The first line of input contains a single integer t (1≤t≤104) — the number of testcases.
Each testcase contains a single integer n (2≤n≤2⋅105).
It is guaranteed that the sum of n over all the testcases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。
每个测试用例包含一个整数 n(2≤n≤2⋅105)。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each testcase, if there is no beautiful tree having n vertices, print −1.
Otherwise, print n−1 lines denoting the edges of a beautiful tree having n vertices. Each line should contain two space-separated integers u,v (1≤u,v≤n) representing an edge.
The vertices can be printed in any order within an edge, and the edges can be printed in any order.
对于每个测试用例,如果不存在包含 n 个顶点的优美树,则输出 −1。
否则,输出 n−1 行,每行表示一棵包含 n 个顶点的优美树的一条边。每行应包含两个以空格分隔的整数 u,v(1≤u,v≤n),表示一条边。
每条边中两个顶点的顺序可以任意,各条边的输出顺序也可以任意。
输入输出样例
输入#1
3 2 3 4
输出#1
-1 1 3 2 3 1 2 3 1 4 1
说明/提示
Test case 1: No beautiful tree exists with 2 vertices. Hence, print −1.
Test case 2:
S=(2⋅3)+(1⋅3)=9=(3)2
Test case 3:
S=(2⋅1)+(3⋅1)+(4⋅1)=9=(3)2
测试用例 1:不存在包含 2 个顶点的优美树。因此,输出 −1。
测试用例 2:
S=(2⋅3)+(1⋅3)=9=(3)2
测试用例 3:
S=(2⋅1)+(3⋅1)+(4⋅1)=9=(3)2
输入解题思路,AI测评打分。不知道怎么写?