【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子列的个数; 然后让你输出 ...
随机推荐
- Jquery Math ceil()、floor()、round()比较与用法
Math.ceil():向上取值 如:Math.ceil(2.1) -- 结果为 3 Math.ceil(-2.1) -- 结果为-2 结论:正入 负舍 Math.floor(): 先下取值 入 ...
- 干货:鲜为人用的MySQL高级特性与玩法!
上一篇文章<万字总结:学习MySQL优化原理,这一篇就够了!>文末给大家留有两个开放的问题: 有非常多的程序员在分享时都会抛出这样一个观点:尽可能不要使用存储过程,存储过程非常不容易维护, ...
- SVN文件恢复
SVN删除文件 一.本地删除 SVN删除文件里的本地删除,指的是在clientdelete了一个文件,但还没有commit.使用revert来撤销删除. 二.server删除 1.通过本地删除后提交s ...
- elcipse 编译cocos2d-x android
http://blog.csdn.net/eyu8874521/article/details/22605695 最開始学习cocos2dx.大多数人可能是被复杂的环境配置过程搞死的,尤其是和Andr ...
- USACO 2.2 Runaround Numbers
Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81 ...
- Python 函数(二)
参数 以下是调用函数时可使用的正式参数类型: 必备参数 关键字参数 默认参数 不定长参数 必备参数 必备参数须以正确的顺序传入函数.调用时的数量必须和声明时的一样. 调用printme()函数,你必须 ...
- WPF动态控件生成查找不到问题
2012 08 10 遇到此类问题,已经找到解决方案 记录以备后用 动态往界面添加控件 在页面未显示的情况时,虽然对控件增加了id name等属性但是使用 TextBox txtOtherNati ...
- Nosql的实际应用场景
怎么样把NoSQL引入到我们的系统架构设计中,需要根据我们系统的业务场景来分析,什么样类型的数据适合存储在NoSQL数据库中,什么样类型的数据必须使用关系数据库存储.明确引入的NoSQL数据库带给系统 ...
- FluentAPI配置
基本 EF 配置只要配置实体类和表.字段的对应关系.表间关联关系即可. 如何利用 EF的高级配置,达到更多效果:如果数据错误(比如字段不能为空.字符串超长等),会在 EF 层就会报错,而不会被提交给数 ...
- WPF常用资源
Textbox error template<Style x:Key="ControlBaseStyle" TargetType="{x:Type Control} ...