【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子列的个数; 然后让你输出 ...
随机推荐
- 一个简单的 PC端与移动端的适配(通过UA)
只需在header引用个js文件, 原理就是判断UA里面的标识. 加下面代码添加到js文件,在头文件引用即可 var Pc_url = 'http://www.baidu.com'; //PC端网址 ...
- Exchange EMC打开出错 解决
Exchange控制台打开出错如何解决 1.卸载win server功能中的winrm iis 2.重启 3.安装winrm iis 4.查看default web site 有没有绑定80端口,没有 ...
- 在IntelliJ IDEA中创建Web项目
在IntelliJ IDEA中创建Web项目 在IntelliJ IDEA中创建Web项目1,创建Maven WebProject选择File>New>Project 出现New Proj ...
- ASP.Net MVC – What are the uses of Display, DisplayName, DisplayFormat and ScaffoldColumn attributes
http://www.codeproject.com/Articles/775220/ASP-Net-MVC-What-are-the-uses-of-Display-DisplayNa?utm_so ...
- 2015.04.24,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 34
1.no fatigue indefatigable([indi'fætigәb(ә)l] adj. 不知疲倦的)来自faigue,in-是反义词缀:后缀-able表示able to be,因此ind ...
- angularjs1-6,自定义服务
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- hdoj--2122--Ice_cream’s world III(克鲁斯卡尔)
Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- [SCOI 2009] 生日快乐
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1024 [算法] 直接DFS,即可 [代码] #include<bits/std ...
- python 3.x 学习笔记17(协程以及I/O模式)
1.协程(微线程)协程是一种用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈.因此: 协程能保留上一 ...
- Primitive data types recap