G. New Roads
                                                                   time limit per test: 2 seconds
                                                            memory limit per test:256 megabytes
                                                                      input:standard input
                                                                     output:standard output

There are n cities in Berland, each of them has a unique id — an integer from1 ton, the capital is the one with id1. Now there is a serious problem in Berland with roads — there are no roads.

That is why there was a decision to build n - 1 roads so that there will be exactly one simple path between each pair of cities.

In the construction plan t integers a1, a2, ..., at were stated, wheret equals to the distance from the capital to the most distant city, concerning new roads.ai equals the number of cities which should be at the distancei from the capital. The distance between two cities is the number of roads one has to pass on the way from one city to another.

Also, it was decided that among all the cities except the capital there should be exactlyk cities with exactly one road going from each of them. Such cities are dead-ends and can't be economically attractive. In calculation of these cities the capital is not taken into consideration regardless of the number of roads from it.

Your task is to offer a plan of road's construction which satisfies all the described conditions or to inform that it is impossible.

Input

The first line contains three positive numbers n,t andk (2 ≤ n ≤ 2·105,1 ≤ t, k < n) — the distance to the most distant city from the capital and the number of cities which should be dead-ends (the capital in this number is not taken into consideration).

The second line contains a sequence of t integersa1, a2, ..., at (1 ≤ ai < n), thei-th number is the number of cities which should be at the distancei from the capital. It is guaranteed that the sum of all the valuesai equalsn - 1.

Output

If it is impossible to built roads which satisfy all conditions, print -1.

Otherwise, in the first line print one integer n — the number of cities in Berland. In the each of the nextn - 1 line print two integers — the ids of cities that are connected by a road. Each road should be printed exactly once. You can print the roads and the cities connected by a road in any order.

If there are multiple answers, print any of them. Remember that the capital has id1.

Examples
Input
7 3 3
2 3 1
Output
7
1 3
2 1
2 6
2 4
7 4
3 5
Input
14 5 6
4 4 2 2 1
Output
14
3 1
1 4
11 6
1 2
10 13
6 10
10 12
14 12
8 4
5 1
3 7
2 6
5 9
Input
3 1 1
2
Output
-1

在构造树的时候,先把树的主链确定,再确定哪些节点为叶子节点(显然深度最大的那些点一定是叶子结点,且根节点一定不是叶子结点因为n≥2),哪些不是叶子节点。

当叶子节点数目不够时,考虑那些不一定是叶子节点的节点(即深度不是最大值并且不是树的主链的成员的节点),把他作为深度大于他们的结点的父亲即可。这样该结点就变成非叶子结点了。

当非叶子结点个数大于那些可以变成非叶子结点的个数时,无解。

 #include <bits/stdc++.h>

 using namespace std;

 #define REP(i,n)                for(int i(0); i <  (n); ++i)
#define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define PB push_back const int N = + ;
vector <int> v[N];
int fa[N], a[N], n, la, leaf, cnt, l; int main(){ scanf("%d%d%d", &n, &la, &leaf);
rep(i, , la) scanf("%d", a + i);a[] = ;
if ((a[la] > leaf) || (n - la < leaf) || (n < leaf)){ puts("-1"); return ;} int sum = ; rep(i, , la) sum += a[i];
if (sum != n){ puts("-1"); return ;}
cnt = ; rep(i, , la) rep(j, , a[i]) v[i].PB(++cnt); REP(i, a[]) fa[v[][i]] = ;
rep(i, , la) fa[v[i][]] = v[i - ][];
l = n - leaf - la; rep(i, , la){
rep(j, , a[i] - ) if (l && j <= a[i - ] - ) fa[v[i][j]] = v[i - ][j], --l;
else fa[v[i][j]] = v[i - ][];
} if (l) {puts("-1"); return ;} printf("%d\n", n);
rep(i, , n) printf("%d %d\n", fa[i], i); return ; }

Codeforces 746G New Roads (构造)的更多相关文章

  1. [刷题]Codeforces 746G - New Roads

    Description There are n cities in Berland, each of them has a unique id - an integer from 1 to n, th ...

  2. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  3. New Roads CodeForces - 746G (树,构造)

    大意:构造n结点树, 高度$i$的结点有$a_i$个, 且叶子有k个. 先确定主链, 然后贪心放其余节点. #include <iostream> #include <algorit ...

  4. Codeforces 746G(构造)

                                                                                                      G. ...

  5. Codeforces Round #386 (Div. 2)G. New Roads [构造][树]

    题目链接:G. New Roads 题意:给出n个结点,t层深度,每层有a[i]个结点,总共有k个叶子结点,构造一棵树. 分析: 考虑一颗树,如果满足每层深度上有a[i]结点,最多能有多少叶子结点 那 ...

  6. 【codeforces 746G】New Roads

    [题目链接]:http://codeforces.com/problemset/problem/746/G [题意] 给你3个数字n,t,k; 分别表示一棵树有n个点; 这棵树的深度t,以及叶子节点的 ...

  7. Codeforces 362D Fools and Foolproof Roads 构造题

    题目链接:点击打开链接 题意: 给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q 问是否可行,不可行输出NO 可行输出YES,并输出加入的p条边. set走起.. #incl ...

  8. Codeforces 711D Directed Roads - 组合数学

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

  9. Codeforces 1383D - Rearrange(构造)

    Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...

随机推荐

  1. [BZOJ1503]郁闷的出纳员(Splay)

    Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常 ...

  2. 3 - JVM随笔分类(gc.log ,VisualVM插件介绍,VisualVM远程连接方式介绍)

    gc.log 354.2 KB 对于对应用的监控上可以使用Jdk自带的VisualVM来做可视化监控,可以查看当前服务应用进程的堆大小的走向,以及类的加载数量等,除此之外,VisualVM可以支持很多 ...

  3. 【Clone Graph】cpp

    题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...

  4. leetcode 【 Unique Paths II 】 python 实现

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  5. 程序集链接器(AL.exe)

    AL.exe使用程序可以生成一个EXE文件或者DLL PE文件(其中只包含对其他模块中的类型进行描述的一个清单). 不要在普通的命令行窗口中编译,请先打开C:\ProgramData\Microsof ...

  6. IOS开发学习笔记031-代码实现微博界面

    微博界面如下 1.准备资源文件 新建一个plist文件,添加条目,root类型是array,子类型是Dictionary 2.更改父类,实现代理方法 接下来得实现过程如上一篇文章,改变父类为UITab ...

  7. CSU-2049 象棋

    CSU-2049 象棋 Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子.一天,小度在棋盘上摆起了许多車--他想知道,在一共N×M个点的矩形棋盘中摆最多 ...

  8. DownloadManager的使用

    DownloadManager是系统开放给第三方应用使用的类,包含两个静态内部类DownloadManager.Query和DownloadManager.Request.DownloadManage ...

  9. jsp运行机制

    一.JSP机制概述 可以把执行JSP页面的执行分成两个阶段,一个是转译阶段,一个是请求阶段. 转译阶段:JSP页面转换成Servlet类. 请求阶段:Servlet类执行,将响应结果发送至客户端. 1 ...

  10. ValueStack、ActionContext

    笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 1. ValueStac ...