codeforces C. Restore Graph
题意:构造一个有n个顶点,每个点度不超过k,然后给出每一个点到达一个定点的最短距离d数组,然后构造出这样的一个图;
思路:排序之后,有两个距离为0的或者没有直接输出-1,然后用两个游动下表,后面的与前面的度都小于k且它们的距离相差1,就建1条边。然后dfs输出就可以。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#define maxn 100010
using namespace std; int n,k;
int d[maxn];
int du[maxn];
struct node
{
int x,id;
bool operator <(const node &a)const
{
return x<a.x;
}
} p[maxn];
vector<int>g[maxn]; void dfs(int x)
{
for(int i=; i<(int)g[x].size(); i++)
{
printf("%d %d\n",x,g[x][i]);
dfs(g[x][i]);
}
} int main()
{
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&d[i]);
p[i].x=d[i];
p[i].id=i;
}
sort(p+,p+n+);
if(p[].x==&&p[].x==||p[].x!=)
{
printf("-1\n");
return ;
}
int l=,r=;
int cnt=;
bool flag=false;
while(r<=n)
{
if(p[r].x==p[l].x)
{
printf("-1\n");
return ;
}
if(p[r].x-p[l].x==&&du[p[l].id]<k&&du[p[r].id]<k)
{
g[p[l].id].push_back(p[r].id);
cnt++;
du[p[l].id]++;
du[p[r].id]++;
r++;
}
else if(p[r].x-p[l].x>||du[p[l].id]>=k)
{
l++;
if(l==r)
{
flag=true;
break;
}
}
else if(du[p[r].id]>=k)
{
r++;
}
else
{
r++;
}
}
if(flag)
{
printf("-1\n");
return ;
}
printf("%d\n",cnt);
dfs(p[].id);
return ;
}
codeforces C. Restore Graph的更多相关文章
- CodeForces 404C Restore Graph (构造)
题意:让人构造一个图,满足每个结点边的数目不超过 k,然后给出每个结点到某个结点的最短距离. 析:很容易看出来如果可能的话,树是一定满足条件的,只要从头开始构造这棵树就好,中途超了int...找了好久 ...
- Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)
题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- 【Codeforces 404C】Restore Graph
[链接] 我是链接,点我呀:) [题意] 每个节点的度数不超过k 让你重构一个图 使得这个图满足 从某个点开始到其他点的最短路满足输入的要求 [题解] 把点按照dep的值分类 显然只能由dep到dep ...
- Codeforces 1093D Beautiful Graph(二分图染色+计数)
题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...
- Codeforces 986C AND Graph dfs
原文链接https://www.cnblogs.com/zhouzhendong/p/9161514.html 题目传送门 - Codeforces 986C 题意 给定 $n,m (0\leq n\ ...
- Codeforces 670F - Restore a Number - [字符串]
题目链接:https://codeforces.com/contest/670/problem/F 题意: 有一个非负整数 $n$,在它的右侧添上它的位数后,被发送出去:例如 $6510$,加上位数 ...
- Codeforces 466E Information Graph
Information Graph 把询问离线之后就能随便搞了, 去check一下是不是祖先, 可以用倍增也能用dfs序. #include<bits/stdc++.h> #define ...
- CodeForces - 986C AND Graph
不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的...但是我们可以一个1一个1的消去,最后变成补集的一个子集. 但是必须当且仅当 至少有一个 a 等于 x 的 ...
随机推荐
- Qt 发送 https 请求
1.环境 ubuntu 12.04 Qt库版本 4.8.1(安装包是Nokia时期的sdk,现在已经不好找了) 2.网上一查都说 Qt 默认不支持Openssl,心想那https也肯定用不了啊,然后屁 ...
- [每日一题] 11gOCP 1z0-052 :2013-09-1 RMAN-- repair failure........................................A20
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10859315 正确答案:D 一.模拟上题的错误: 1.删除4号文件 [oracle@myd ...
- [PWA] Enable Push Notification in your web app
1. Clone the project: git clone https://github.com/GoogleChrome/push-notifications.git 2. install th ...
- [RxJS] map vs flatMap
What's the difference between map and flatmap? First, let's show what map is. To show that, I need a ...
- mysql 时区 , 夏令时,冬令时
mysql默认时区: mysql> show variables like '%time_zone%'; +------------------+--------+ | Variable_nam ...
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现--转载
使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...
- iOS--RunLoop原理介绍
什么是RunLoop RunLoop从字面上看是运行循环的意思,这一点也不错,它确实就是一个循环的概念,或者准确的说是线程中的循环. 本文一开始就提到有些程序是一个圈,这个圈本质上就是这里的所谓的Ru ...
- android开发之Notification学习笔记
今天总结了一下Notification的使用,与大家分享一下. MainActivity.java: 本文参考:http://www.jb51.net/article/36567.htm,http:/ ...
- HTML特效代码大全
1)贴图:<img src="图片地址">2)加入连接:<a href="所要连接的相关地址">写上你想写的字</a>1)贴 ...
- PHP 的try catch 报错捕获机制
首先上代码: try { echo 'Never executed'; echo "<br>"; if(1<0){ echo 'end'; }else{ thro ...