CF404C.Restore Graph

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

瓦莱拉曾拥有一个无向连通图,该图不含自环和重边,共有 nn 个顶点。该图具有一个有趣的性质:每个顶点的度数至多为 kk。为方便起见,我们假设图的顶点编号为 11 到 nn 的整数。

某天,瓦莱拉计算了从图中某个顶点到其余所有顶点的最短距离,并将结果记录在数组 dd 中。因此,数组中的元素 d[i]d[i] 表示瓦莱拉所选定的顶点到编号为 ii 的顶点的最短距离。

随后,一场无法挽回的灾难发生了:瓦莱拉丢失了原始图。但他仍保留着数组 dd。请帮助他恢复这个丢失的图。

输入格式

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.

第一行包含两个以空格分隔的整数 nn 和 kk(1≤k<n≤1051 \leq k < n \leq 10^5)。整数 nn 表示原图中的顶点数量;整数 kk 表示原图中每个顶点的度数至多为 kk。

第二行包含 nn 个以空格分隔的整数 d[1], d[2], …, d[n]d[1],\,d[2],\,\dots,\,d[n](0≤d[i]<n0 \leq d[i] < n)。整数 d[i]d[i] 表示 Valera 所选顶点到编号为 ii 的顶点的最短距离。

输出格式

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 a__i and b__i (1 ≤ a__i, b__i ≤ n; a__i ≠ b__i), denoting the edge that connects vertices with numbers a__i and b__i. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.

如果瓦列拉在笔记中犯了错误,所要求的图不存在,则在第一行输出数字 -1。否则,在第一行输出整数 mm(0 ≤ m ≤ 1060 ≤ m ≤ 10^6)——即所找到图中的边数。

接下来的 mm 行中,每行输出两个用空格分隔的整数 aia_i 和 bib_i(1 ≤ ai, bi ≤ n1 ≤ a_i, b_i ≤ n;ai ≠ bia_i ≠ b_i),表示连接编号为 aia_i 和 bib_i 的顶点的一条边。该图不应包含自环和重边。如果存在多种可能的答案,输出任意一种即可。

输入输出样例

  • 输入#1

    3 2
    0 1 1

    输出#1

    3
    1 2
    1 3
    3 2
  • 输入#2

    4 2
    2 0 1 3

    输出#2

    3
    1 3
    1 4
    2 3
  • 输入#3

    3 1
    0 0 0

    输出#3

    -1

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

首页