【codeforces 746G】New Roads
【题目链接】: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的更多相关文章
- 【34.40%】【codeforces 711D】Directed Roads
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【50.00%】【codeforces 602C】The Two Routes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 546E】Soldier and Traveling
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 752F】Santa Clauses and a Soccer Championship
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【27.66%】【codeforces 592D】Super M
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
随机推荐
- properties 乱码问题
File --> Others Settings --> Default Settings
- nodejs-EventEmitter
addListener(event, listener)为指定事件添加一个监听器到监听器数组的尾部. on(event, listener)为指定事件注册一个监听器,接受一个字符串 event 和一个 ...
- BA-闭式冷却塔系统
- [using_microsoft_infopath_2010]Chapter10 与SharePoint Designer工作流集成
本章概要: 1.创建工作流集成表单 2.允许工作流手动触发 3.创建自定义动作按钮 4.使用人物设计器 5.维护工作流人物表单
- HDU 2371
知道了怎么置换之后,就可以用矩阵来置换了,但这道题一直关于置换的地方读不明白. #include <iostream> #include <cstdio> #include & ...
- Android框架简要介绍
1. Android架构直观图 下图展示了Android系统的主要组成部分: 总体上而言,Android系统结构由5个部分组成.从上到下,别人是Applications (Android应用 ...
- Linux 下安装apache2.4
Linux 下安装apache2.4 下载,解压,配置安装! 好生麻烦! 安装一个apache,需要很多依赖!比如apr.apr-util.pcre等等. 这些依赖有可能还需要别的更多的依赖! 真心的 ...
- composer的一些操作
版本更新 命令行下:composer self-update 设置中国镜像 composer config -g repo.packagist composer https://packagist.p ...
- JS实现掷骰子
JS实现掷骰子 实现方法: 方法一:通过background-position.background-image.backg-repeat三个属性以及jquery animate()方法改变背景骰子图 ...
- 洛谷P4015 运输问题(费用流)
题目描述 WW 公司有 mm 个仓库和 nn 个零售商店.第 ii 个仓库有 a_iai 个单位的货物:第 jj 个零售商店需要 b_jbj 个单位的货物. 货物供需平衡,即\sum\limits ...