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 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
随机推荐
- Linux命令之---touch
命令简介 linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 命令格式 touch [选项]... 文件... 命令参数 -a 或 ...
- VBA连接MySQL数据库以及ODBC的配置(ODBC版本和MySQL版本如果不匹配会出现驱动和应用程序的错误)
db_connected = False '获取数据库连接设置dsn_name = Trim(Worksheets("加载策略").Cells(2, 5).Value) ---- ...
- mysql之处理金钱小数点后的多余0
问题产生原因:我们在做基金项目 产生大量的金钱 在GP首页展示首页信息的时候要求去除多余的0 由于我们在数据库设计的时候查询返回数据 例如18.100000 这种形式 而我们需要将多余的0 ...
- Android学习笔记之-----讯飞语音识别实例化RecognizerDialog参数出现错误的解决方法
本人也是个小菜鸟,在做语音识别时也遇到了这个问题,空指针一直报错,app程序停止运行. 在网上搜了半天在这个帖子里找到了解决方法:http://bbs.xfyun.cn/forum.php?mo .. ...
- Apache shiro学习总结
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- Careercup - Microsoft面试题 - 5917873302142976
2014-05-12 06:56 题目链接 原题: A link list contains following elements struct node{ int data; node* next; ...
- Windows核心编程小结2
这一节看看内存管理相关的信息 首先看看虚拟内存 虚拟地址空间 32位系统 --- 4GB = 232 64 位系统 ---- 16EB = 264 虚拟内存表 当一个应用程序从硬盘加载到RAM时, ...
- Zookeeper ZooDefs.Ids
OPEN_ACL_UNSAFE : 完全开放的ACL,任何连接的客户端都可以操作该属性znode CREATOR_ALL_ACL : 只有创建者才有ACL权限 READ_ACL_UNSAFE:只能读 ...
- HDU3232 Crossing Rivers 数学期望问题
Crossing Rivers ...
- 截图神器-snipaste
基础操作 Snipaste 是一个简单但强大的贴图工具,同时也可以执行截屏.标注等功能. 截屏 开始截图 快捷键(默认为 F1) 鼠标左键 单击托盘图标 何谓一次 成功的截图 保存到剪贴板 ( / ...