【题目链接】: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. JAVA基础总结【面试】

    前言 近间陆续面试了不少的求职的前(JAVA).后(WEB)端开发人员,包括实习生.应届毕业生.一两年工作经验的.也有三四年工作经验的,也算见过了比较多的开发人员,想在这里做个总结,本次主要讲一讲面试 ...

  2. (0)前言【从零开始学Spring Boot】

    在此对整体技术简单说明下. 开发工具和开发环境: Win7 64位操作 Eclipse-jee-mars-2-win32-x86_64 Jdk 1.8 Maven管理项目 如果要学习Spring Bo ...

  3. BA-siemens-apogee总线不稳定解决方法

    状况一:BLN下的火车头在线,但是下面的模块(包括UEC或者PPM)全部掉线 尝试方法: 使用挨个DDC箱断线的方法测试总线是否上线(可以解决由于总线短路引起的总线故障,施工中总线压冷压端子的话就不容 ...

  4. JS 正则表达式的位置匹配ZZ

    http://regexpal.com/ 上面这个网站可以用于在线检测JS的正则表达式语法 除了熟知的几个固定字符表示位置: ^ : Match the beginning of the string ...

  5. 多线程编程TSL相关的技术文档

    线程本地存储 (TLS) https://msdn.microsoft.com/zh-cn/library/6yh4a9k1(v=vs.80).aspx Using Thread Local Stor ...

  6. 敏捷开发 —— TDD(测试驱动开发)

    测试驱动开发 TDD(Test-Driven Development)是敏捷开发的一项核心实践,同时也是一种设计技术和方法. 既然是测试驱动,便是测试,测试用例先行: 首先编写好测试用例,期待值,实际 ...

  7. 如何使用github来展示自己的网页

    项目文档或者单纯的html页面怎么用github来展示呢? 第一步:新建库 第二步: 上传自己的页面(index.html需在根目录下) 先把git库克隆下来 进入lineShop文件夹,拷贝自己的页 ...

  8. [jzoj 5664] [GDOI2018Day1模拟4.6] 凫趋雀跃 解题报告(容斥原理)

    interlinkage: https://jzoj.net/senior/#contest/show/2703/3 description: solution: 考虑容斥原理,枚举不合法的走的步数 ...

  9. 1.matlab基础准备及入门

    1.1 Command Window(命令行窗口)运用入门 1 计算器的用法 2 数值变量与表达式 3. 计算结果的图形表示 代码及注释 function [ output_args ] = Unti ...

  10. 1.Win32基本程序概念

    还没学会走之前,不要跑! Message based , event driven 每个程序都应该有一个如下的循环:MSG msg;while(GetMessage(&msg,NULL,NUL ...