Codeforces 959 C. Mahmoud and Ehab and the wrong algorithm

题目链接

C. Mahmoud and Ehab and the wrong algorithm
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The only line contains an integer n (2 ≤ n ≤ 105), the number of nodes in the desired trees.

Output

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.

Examples
input
Copy
2
output
-1
1 2
input
Copy
8
output
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
Note

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:

题目大意:给出一个错误的算法,让你输出能 使这个算法不成立的树 和 使这个算法成立的树。

如果不存在算法不成立的树时输出-1。

这里笔者把 使这个算法不成立的树标记为 树1,使这个算法成立的树为 树2

算法大意可以看题目(应该都看得懂的)。

这里就直接找这个算法的漏洞。

首先这个算法对于单链状的树是一定成立的,这样 树2 就解决了。

然后读者可以动笔试一下,对于点数n<=5的时候也是一定成立的。所以当n<=5时 树1 要输出-1。

然后就是如何找出 树1。

首先这个算法是想根据奇偶分层,只用奇数层的点(或偶数点)对数进行覆盖。

这里先选定是奇数层。对于某个被选中的层,有些点是没有后继的那么显然这些点不用取也可以完成覆盖。但是上面的算法会把这些点也选进。

既然理解的算法的漏洞的话,我们可以构建一颗分叉大且分叉后继没有(或者少)后继的树。

这里笔者的构建方法是先从根(1号)连上2,3,4点,然后2号再连上剩余的点。显然覆盖这颗树只需要2个点。但是上面的算法会算到3个。(不懂的读者可以自己画一下)

结果还是脑洞题(哭)

代码如下

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<set>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<cmath>
#include<stack>
using namespace std;
const int maxn = (int)(1e5) + 100;
const int inf = 0x3f3f3f3f;
const int mod = 2520;
const double eps = 1e-3;
typedef long long LL;
typedef unsigned long long ull;
int main() {
	//freopen("E:\\test.txt", "r", stdin);
	int n;
	while (~scanf("%d", &n)) {
		if (n <= 5) printf("-1\n");
		else {
			printf("1 2\n");
			printf("1 3\n");
			printf("1 4\n");
			for (int i = 5; i <= n; i++)
				printf("2 %d\n", i);
		}
		for (int i = 1; i < n; i++)
			printf("%d %d\n", i, i + 1);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值