Ivan and Powers of Two

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Valera had an undirected connected graph without self-loops and multiple edges consisting of n vertices. The graph had an interesting property: there were at most k edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to n.

One day Valera counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array d. Thus, element d[i] of the array shows the shortest distance from the vertex Valera chose to vertex number i.

Then something irreparable terrible happened. Valera lost the initial graph. However, he still has the array d. Help him restore the lost graph.

Input

The first line contains two space-separated integers n and k(1 ≤ k < n ≤ 105). Number n shows the number of vertices in the original graph. Number k shows that at most k edges were adjacent to each vertex in the original graph.

The second line contains space-separated integers d[1], d[2], ..., d[n](0 ≤ d[i] < n). Number d[i] shows the shortest distance from the vertex Valera chose to the vertex number i.

Output

If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer m(0 ≤ m ≤ 106) — the number of edges in the found graph.

In each of the next m lines print two space-separated integers ai and bi(1 ≤ ai, bi ≤ nai ≠ bi), denoting the edge that connects vertices with numbers ai and bi. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.

Sample Input

Input
3 2
0 1 1
Output
3
1 2
1 3
3 2
Input
4 2
2 0 1 3
Output
3
1 3
1 4
2 3
Input
3 1
0 0 0
Output
-1

由图上的一点到其他点的距离,还原整个图。易知原图必定是一个树,因为给出的是一个点到其他点的距离,说明这个点到其他的点必定是唯一联通的(即使原图是多路联通,对每个同路取最小值后,也只剩
下一条通路了)。所以就是求一个树的过程。
我们把各个点按照到根节点的距离分类。这些距离可以是1 2 3.。。。n-1;
然后按照距离从小到大连接,每次的距离增加1,所以新增加的点是和上一个的点直接相连的。由于距离根节点距离为1的点开始没有连线,所以起始边数为0,之后的必定和比它到根节点距离少1的
节点连线,所以边数起始值是1.
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#define max1 100005
using namespace std;
int main()
{
int n,k;
vector< pair<int,int> >ans;
vector<int >dis[max1];
scanf("%d%d",&n,&k);
int m=;
for(int i=;i<=n;i++)
{
int di;
scanf("%d",&di);
m=max(di,m);
dis[di].push_back(i);
}
if(dis[].size()!=) {printf("-1\n");return ;}
for(int i=;i<=m;i++)
{
int edge=(i!=),node=;
for(int j=;j<dis[i].size();j++)
{
if(edge==k)
{
node++;
edge=(i!=);
}
if(node==dis[i-].size())
{
printf("-1\n");
return ;
}
ans.push_back(make_pair(dis[i-][node],dis[i][j]));
edge++; }
}
printf("%d\n",n-);
for(int i=;i<n-;i++)
printf("%d %d\n",ans[i].first,ans[i].second);
return ;
}

CodeForces 404C Ivan and Powers of Two的更多相关文章

  1. CF 305C ——Ivan and Powers of Two——————【数学】

    Ivan and Powers of Two time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. CodeForces 1100F Ivan and Burgers

    CodeForces题面 Time limit 3000 ms Memory limit 262144 kB Source Codeforces Round #532 (Div. 2) Tags da ...

  3. codeforces 1100F Ivan and Burgers 线性基 离线

    题目传送门 题意: 给出 n 个数,q次区间查询,每次查询,让你选择任意个下标为 [ l , r ] 区间内的任意数,使这些数异或起来最大,输出最大值. 思路:离线加线性基. 线性基学习博客1 线性基 ...

  4. CodeForces 404C Restore Graph (构造)

    题意:让人构造一个图,满足每个结点边的数目不超过 k,然后给出每个结点到某个结点的最短距离. 析:很容易看出来如果可能的话,树是一定满足条件的,只要从头开始构造这棵树就好,中途超了int...找了好久 ...

  5. 【Codeforces 404C】Restore Graph

    [链接] 我是链接,点我呀:) [题意] 每个节点的度数不超过k 让你重构一个图 使得这个图满足 从某个点开始到其他点的最短路满足输入的要求 [题解] 把点按照dep的值分类 显然只能由dep到dep ...

  6. Codeforces 1239A. Ivan the Fool and the Probability Theory

    传送门 注意到连续两个格子如果有相同颜色那么一路过去的都可以确定 比如一开始染了这两个位置: 然后发现后面整片过去都可以确定: 对于横着的情况也是一样,然后就会发现不可能出现横着两个和竖着两个同时都有 ...

  7. Codeforces 988D Points and Powers of Two ( 思维 || 二的幂特点 )

    题目链接 题意 : 给出坐标轴上的 n 个点的横坐标,要你选出最多的点,使得这些点两两距离是二的幂 ( 特殊情况 : 选出的集合只有一个点也满足条件 ) 分析 : 官方题解已经说的很好了 最关键是是判 ...

  8. Codeforces 1248C Ivan the Fool and the Probability Theory(推公式)

    题意 一个n*m的网格图,每个格子可以染黑色.白色,问你每个格子最多有一个相邻颜色相同的方案数 n,m<=1e5 思路 我们先处理\(1 \times m\)的情况 设\(f[i][j]\)为前 ...

  9. Codeforces Round #184 (Div. 2)

    A. Strange Addition (目前的做法好像做烦了) 统计数的\(mask\),表示个.十.百位上是否是0,共8种数. 枚举8种数组成的所有情况\(2^8\),记录最大数量. B. Con ...

随机推荐

  1. ExtJS学习之路第五步:认识最常见组件Panel

    文档中描述 Panel(面板)是一个容器,它具有特定的功能和结构部件,这使它成为面向应用用户界面的完美基石.面板,继承自Ext.container.Container,能够配置布局以及子组件(Chil ...

  2. 建站时注意敏感词的添加_seo优化禁忌

    之前接手一个站点,网站标题中出现一个“三级医院”的词,虽然这个词在我们看来是没有问题的,医院评级中“三级医院”算是等级很高的,很多医院为了体现等级会在明显的地方着重加注.但是我们要考虑一下搜索引擎的分 ...

  3. HDU 4857 逃生 (优先队列+反向拓扑)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 解题报告:有n个点,有m个条件限制,限制是像这样的,输入a  b,表示a必须排在b的前面,如果不 ...

  4. Linux瑞士军刀:密码管理Keeweb

    导读 如今,我们依赖于越来越多的线上服务.我们每注册一个线上服务,就要设置一个密码:如此,我们就不得不记住数以百计的密码.这样对于每个人来说,都很容易忘记密码.我将在本文中介绍 Keeweb,它是一款 ...

  5. MySQL数据库服务器的架设

    导读 MySQL数据库是Linux操作系统上用得最多的数据库系统,它可以非常方便的与其它服务器集成在一起,如Apache.Vsftpd.Postfix等.下面介绍RHEL 6平台MySQL数据库服务器 ...

  6. [POJ1157]LITTLE SHOP OF FLOWERS

    [POJ1157]LITTLE SHOP OF FLOWERS 试题描述 You want to arrange the window of your flower shop in a most pl ...

  7. RouterOS DNS劫持 -- A记录

    通常我们使用RouterOS的DNS主要是用于实现DNS缓存功能,即由RouterOS实现DNS服务器解析功能,除了这个功能,RouterOS可以实现对内网域名解析劫持,即实现路由网关的A记录查询. ...

  8. move 和 CopyMemory的区别

    Move(ABuffer,P, Sizeof(ABuffer));   //指针传递            Move(ABuffer^,P^, Sizeof(TArrayByte));   //复制内 ...

  9. bwa的使用方法

    bwa的使用需要两中输入文件:    Reference genome data(fasta格式 .fa, .fasta, .fna)    Short reads data (fastaq格式 .f ...

  10. Shell脚本中cd命令使用

    在写shell脚本的时候发现cd切换目录的时候无法切换,代码是下面的. #!/bin/bash #changedir.sh history cd /home/firefox sleep pwd 我仔细 ...