Codeforces Round #473

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

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
Copy
-1
1 2
Input
Copy
8
Output
Copy
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:

题目大意:给出了一个无向图中用最少的点覆盖所有边的算法(错的),只要边与该点相连就算覆盖。

题目中的算法是错的,它认为答案是 min{深度为奇数的点的总数,深度为偶数的点的总数},显然忽略了既有深度为奇数的点和深度为偶数的点的情况。只要根据这一点构造答案即可。

(1)n <= 5 时,该算法的结果均正确;

(2)对于错误的答案,为了简便,我们把标号为偶数的点全作为 1 的孩子,标号为奇数的点(除 1 外)全作为 2 的孩子,这样无论 n 取多少,结果永远为 2 < min(n / 2 , (n + 1) / 2);

(3)对于正确的数据,只要树的结构是一条链,就满足题目中的算法。

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    if(n <= 5)
    {
        printf("-1\n");
    }
    else
    {
        for(int i = 2;i <= n; i += 2) printf("%d %d\n",1,i);
        for(int i = 3;i <= n; i += 2) printf("%d %d\n",2,i);
    }
    for(int i = 1;i < n; ++i) printf("%d %d\n",i,i + 1);
    return 0;
}
E. Mahmoud and Ehab and the xor-MST
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight (where is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph?

You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph

You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree

The weight of the minimum spanning tree is the sum of the weights on the edges included in it.

Input

The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.

Output

The only line contains an integer x, the weight of the graph's minimum spanning tree.

Example
Input
Copy
4
Output
Copy
4
Note

In the first sample: The weight of the minimum spanning tree is 1+2+1=4.

题目大意:给定一个完全图,任意两点间边的权重定义为两点标号的XOR,求一个最小生成树。

这种题目貌似见过很多次,但这次终于是找到了正解,这道问的比较简单,还有一种是给定每个点的权值,边的权值还是两个点的XOR,求MST,具体做法在那道题目中详细叙述。

这道题的做法是把所有的点化成二进制表示,建一棵Trie,因为是从 0 开始标号,所以Trie是一个完全二叉树。从最底层开始两两合并。从最后一层的最左边开始,每一对(代表两个连通分量)的合并代价是最小的,因为他们的二进制位只有最后一位不同。这里需要设一个变量记录权重,因为每上一层合并代价翻倍(还是 1 位不同,但是是二进制的高一位),第 n 层是 1 ,第 n-1 层就是 2,然后 2、4、8、16 以此类推。直至剩下一个连通分量,算法结束。

#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
int main()
{
    ll n;
    ll ans = 0;
    ll p = 1;
    scanf("%I64d",&n);
    while(n > 1)
    {
        ans += p * (n >> 1);
        p <<= 1;
        n -= (n >> 1);
    }
    printf("%I64d",ans);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值