【题目链接】:http://codeforces.com/problemset/problem/746/G

【题意】



给你3个数字n,t,k;

分别表示一棵树有n个点;

这棵树的深度t,以及叶子节点的个数k;

给你树的每层节点个数;

让你画出这么一棵树;

输出它的n-1条边;

【题解】



先构造出一棵树的主链;

即t层,每层的第一个节点都连在一起;

然后,把第二层的节点都和头结点都连在一起;

这时有t个点,它们已经不可能是叶子节点了,还有tot=n-k-t,需要把这n-k-t都变成不是叶子节点(非叶子节点),那样最后就能剩k个叶子节点了;

具体来说,把第i层的点和上一层的点连在一起,能让上一层的点变成非叶子节点;即tot–;如果tot变成0了,则让他和上一层的第一个节点连在一起;这样就不会产生新的非叶子节点了;

最后判断tot会不会为0;

为0则输出方案就好;

不为0就输出无解;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100; int n,t,k,a[N],tot,fa[N];
vector <int> v[N]; int main()
{
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n >> t >> k;
rep1(i,1,t) cin >> a[i];
a[0] = 1;
if (a[t]>k)
return cout <<-1<<endl,0;
rep1(i,0,t)
rep1(j,1,a[i])
v[i].pb(++tot);
rep1(i,0,a[1]-1)
fa[v[1][i]] = 1;
rep1(i,2,t)
fa[v[i][0]] = v[i-1][0];
int l = n-t-k;
rep1(i,2,t)
{
rep1(j,1,a[i]-1)
if (l>0 && j<=a[i-1]-1)
fa[v[i][j]] = v[i-1][j],l--;
else
fa[v[i][j]] = v[i-1][0];
}
if (l!=0)
return cout <<-1<<endl,0;
cout << n <<endl;
rep1(i,2,n)
cout <<i<<' '<<fa[i]<<endl;
return 0;
}

【codeforces 746G】New Roads的更多相关文章

  1. 【34.40%】【codeforces 711D】Directed Roads

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【50.00%】【codeforces 602C】The Two Routes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【codeforces 546E】Soldier and Traveling

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【codeforces 752F】Santa Clauses and a Soccer Championship

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【27.66%】【codeforces 592D】Super M

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  8. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  9. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

随机推荐

  1. 【codeforces 805B】3-palindrome

    [题目链接]:http://codeforces.com/contest/805/problem/B [题意] 让你生成一个只包含a,b,c的字符串; 要求c出现的次数最少,且任意一个 长度为3的子串 ...

  2. WCF4.0 知识点

    一些基础概念 SOAP:Simple Object Access Protocol,简单对象访问协议,基于XML的可扩展消息信封格式,需同事绑定一个网络传输协议. UDDI:用来发布和搜索web服务的 ...

  3. HDU 3579 线性同余方程组

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  4. string转utf8后解决TTS识别中文的问题

    今天遇到string字符编码的问题,由于遇到了用TTS将文本转语音的一个API,里面的中文必须是utf8的,我传了一个uncode编码的中文进去,就一直不能正常读出来.后来才发现是编码的问题.这里在网 ...

  5. STL之效率比較

    1.vector 变长一维数组,连续存放的内存块,有保留内存.堆中分配内存: 支持[]操作,高效率的随机訪问: 在最后添加元素时,一般不须要分配内存空间,速度快:在中间或開始操作元素时要进行内存拷贝效 ...

  6. Regexp-Utils:银行卡号Luhm校验

    ylbtech-Regexp-Utils:银行卡号Luhm校验 1.返回顶部 1.方法 //Description: 银行卡号Luhm校验 //Luhm校验规则:16位银行卡号(19位通用): // ...

  7. Asp.Net Core部署到Linux服务器

    从2016年7月, .NET Core1.0 正式发布开始,由于时间问题,我没怎么关注过.NET Core,最近刚抽出点时间研究了下,先讲下如何把ASP.NET Core部署到Linux上吧.这里我用 ...

  8. C++头文件一览

    C++头文件一览 C.传统 C++ #include <assert.h> 设定插入点#include <ctype.h> 字符处理#include <errno.h&g ...

  9. Web前端必须规避的8个误区

    现在,有越来越多所谓的“教程”来帮助我们提高网站的易用性.下面收集了一些在Web开发中容易出错和被忽略的小问题,并且提供了参考的解决方案,以便于帮助Web开发者更好的完善网站. 通过避免下面这些小错误 ...

  10. Ubuntu包管理工具整理

    概述 常用的包管理包含三类工具:dpkg.apt和aptitude.1 dpkg 主要是对本地的软件包进行管理,本地软件包包括已经在本地安装的软件包和已经下载但还没有安装的 deb 文件,不解决依赖关 ...