Codeforces 746G New Roads (构造)
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.
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.
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.
7 3 3
2 3 1
7
1 3
2 1
2 6
2 4
7 4
3 5
14 5 6
4 4 2 2 1
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
3 1 1
2
-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 (构造)的更多相关文章
- [刷题]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 ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- New Roads CodeForces - 746G (树,构造)
大意:构造n结点树, 高度$i$的结点有$a_i$个, 且叶子有k个. 先确定主链, 然后贪心放其余节点. #include <iostream> #include <algorit ...
- Codeforces 746G(构造)
G. ...
- Codeforces Round #386 (Div. 2)G. New Roads [构造][树]
题目链接:G. New Roads 题意:给出n个结点,t层深度,每层有a[i]个结点,总共有k个叶子结点,构造一棵树. 分析: 考虑一颗树,如果满足每层深度上有a[i]结点,最多能有多少叶子结点 那 ...
- 【codeforces 746G】New Roads
[题目链接]:http://codeforces.com/problemset/problem/746/G [题意] 给你3个数字n,t,k; 分别表示一棵树有n个点; 这棵树的深度t,以及叶子节点的 ...
- Codeforces 362D Fools and Foolproof Roads 构造题
题目链接:点击打开链接 题意: 给定n个点 m条边的无向图 须要在图里添加p条边 使得图最后连通分量数为q 问是否可行,不可行输出NO 可行输出YES,并输出加入的p条边. set走起.. #incl ...
- Codeforces 711D Directed Roads - 组合数学
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...
- Codeforces 1383D - Rearrange(构造)
Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
随机推荐
- python如何合并两个字典
我有两个Python字典,如何合并它们呢?update()方法正是你所需要的. >>> x = {'a':1, 'b': 2} >>> y = {'b':10, ' ...
- datagrid的基本属性&查询和清空功能的实现
1.datagrid基本属性 <script charset=UTF-8"> $(function(){ $("#datagrid").datagrid({ ...
- idea 安装findBugs 可以做代码扫描,也可以导出扫描结果生成扫描报告
idea 安装findBugs 可以做代码扫描,也可以导出扫描结果生成扫描报告 https://my.oschina.net/viakiba/blog/1838296 https://www.cnbl ...
- 实时视频h5
http://www.cnblogs.com/dotfun/p/4286878.html
- 高亮T4模板
http://t4-editor.tangible-engineering.com/Download_T4Editor_Plus_ModelingTools.html
- 【Two Sum】cpp
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- BMP图片的加载方式:资源 VS 文件
在程序中加载位图有很多方法,各有各的好处.这里简单说一下在资源里和文件里加载的区别. 第一.在资源里加载位图 这种方法就是在工程里的“资源视图”-->“添加资源”-->"Bitm ...
- centos开机启动项设置命令:chkconfig
在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后系统默认不会自动启动的.就算手动执行/etc/init.d/mysqld start ...
- Python-S9——Day84-ORM项目实战之权限、form以及modelform
01 权限菜单显示 02 Django路径的自动添加问题 03 原生form实现增删改查 04 modelform实现增删改查 01 权限菜单显示 1.1 优先查找项目中的templates,如果没有 ...
- Android数据储存之SharedPreferences
Android中SharedPreferences通常与Editor连用 接口SharedPreferences常用方法: boolean contains(String str):判断SharedP ...