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 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
随机推荐
- [Hdu3652]B-number(数位DP)
Description 题目大意:求小于n是13的倍数且含有'13'的数的个数. (1 <= n <= 1000000000) Solution 数位DP,题目需要包含13,且被13整除, ...
- android/libs/libammsdk.jar" already exists! 解决方法
Error: Uh oh!"/work/2016/fengkongbao/.meteor/local/cordova-build/platforms/android/libs/libamms ...
- Vs2012 打开项目 自动关闭 并停止工作 解决方法
来源:http://q.cnblogs.com/q/52530/ http://www.microsoft.com/zh-cn/download/details.aspx?id=36020 下载并安装 ...
- 1、HTML基础总结 part-1
1.基本标签属性 <html> <!--属性和属性值对大小写不敏感. 不过,万维网联盟在其 HTML 4 推荐标准中推荐小写的属性/属性值. 而新版本的 (X)HTML 要求使用小写 ...
- 【Gas Station】cpp
题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...
- [oldboy-django][2深入django]django一个请求的生命周期 + WSGI + 中间件
1 WSGI # WSGI(是一套协议,很多东西比如wsgiref, uwsgiref遵循这一套协议) - django系统本质 别人的socket(wsgiref或者uwsgiref) + djan ...
- Unity3D - 设计模式 - 工厂模式
工厂模式:以食物生产为例 1. 一个生产食物的工厂(此项 需要建立两个类:食物基类<Food>,工厂类<Factory>) 2. 可以生产不同的食物(此项 建立食物的具体子类, ...
- POJ2723 Get Luffy Out 【2-sat】
题目 Ratish is a young man who always dreams of being a hero. One day his friend Luffy was caught by P ...
- Posix线程编程指南
Posix线程编程指南 Posix线程编程指南... 1 一线程创建与取消... 2 线程创建... 2 1.线程与进程... 2 2. 创建线程... 2 3. 线程创建属性... 2 4. 创建的 ...
- 秀秀的照片(photo)
秀秀的照片(photo) 题目描述 华华在和秀秀视频时有截很多图.华华发现秀秀的每一张照片都很萌很可爱.为什么会这样呢?华华在仔细看过秀秀的所有照片后,发现秀秀的照片都具有一个相同的性质. 设秀秀的分 ...