CF959C.Mahmoud and Ehab and the wrong algorithm
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is:
Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm:
- Root the tree at node 1.
- Count the number of nodes at an even depth. Let it be evenCnt.
- Count the number of nodes at an odd depth. Let it be oddCnt.
- The answer is the minimum between evenCnt and oddCnt.
The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0.
Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one.
马哈茂德正在尝试解决树上的顶点覆盖问题。问题描述如下:
给定一棵包含 n 个节点的无向树,求覆盖所有边所需的最少顶点数。形式化地说,我们需要找出一个顶点集合,使得对于树中的每条边 (u,v),该集合中至少包含 u 或 v 中的一个(或两个都包含)。马哈茂德提出了如下算法:
- 将树以节点 1 为根进行有根化;
- 统计深度为偶数的节点个数,记为 evenCnt;
- 统计深度为奇数的节点个数,记为 oddCnt;
- 答案即为 min(evenCnt,oddCnt)。
树中一个节点的深度定义为该节点到根节点的最短路径所含边的数量;根节点的深度为 0。
埃哈卜告诉马哈茂德该算法是错误的,但马哈茂德并不相信,因为他已在许多树上测试过该算法且结果均正确。因此埃哈卜请你构造两棵含 n 个节点的树:第一棵树应使该算法给出错误答案,第二棵树应使该算法给出正确答案。
输入格式
The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.
唯一的一行包含一个整数 n(2 ≤ n ≤ 105),表示所求树的节点数。
输出格式
The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only.
If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict.
If there are multiple answers, you can print any of them.
输出应包含两个独立的部分,每个部分包含一棵树。算法需为第一部分的树找出一个错误的答案,为第二部分的树找出一个正确的答案。若某个部分不存在满足条件的树,则仅对该部分输出 -1(不带引号)。
若某一部分存在答案,则该答案应包含 n−1 行,每行包含两个以空格分隔的整数 u 和 v(1≤u,v≤n),表示节点 u 与节点 v 之间存在一条无向边。若所给图不是一棵树,或不符合该格式,你将收到“答案错误”的判定。
若存在多个合法答案,你可以输出其中任意一个。
输入输出样例
输入#1
2
输出#1
-1 1 2
输入#2
8
输出#2
1 2 1 3 2 4 2 5 3 6 4 7 4 8 1 2 1 3 2 4 2 5 2 6 3 7 6 8
说明/提示
In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section.
In the second sample:
In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this:
In the second tree, the algorithm will find an answer with 3 nodes which is correct: 
在第一个样例中,只有一棵包含 2 个节点的树(节点 1 与节点 2 相连)。该算法在此树上会得出正确答案,因此我们在第一部分输出了 −1;但请注意,我们在第二部分仍输出了这棵树。
在第二个样例中:
-
在第一棵树中,该算法会找到一个包含 4 个节点的解,而实际上存在一个仅含 3 个节点的更优解,如下图所示:

-
在第二棵树中,该算法会找到一个包含 3 个节点的解,该解是正确的:

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