题意:让人构造一个图,满足每个结点边的数目不超过 k,然后给出每个结点到某个结点的最短距离。

析:很容易看出来如果可能的话,树是一定满足条件的,只要从头开始构造这棵树就好,中途超了int。。。找了好久。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100000 + 10;
const int mod = 100000000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} vector<int> G[maxn];
int main(){
scanf("%d %d", &n, &m);
int rt = -1, cnt = 0;
int mmax = 0;
for(int i = 1; i <= n; ++i){
int x;
scanf("%d", &x);
G[x].push_back(i);
if(x == 0) rt = i;
mmax = max(mmax, x);
} if(G[1].size() > G[0].size() * m || G[0].size() != 1){
printf("-1\n");
return 0;
}
for(int i = 2; i <= mmax; ++i)
if(G[i].size() > (LL)G[i-1].size() * (m-1)){
printf("-1\n");
return 0;
} printf("%d\n", n-1);
for(int i = 0; i < mmax; ++i){
bool ok = true;
int p = 0;
for(int j = 0; j < G[i].size() && ok; ++j){
int u = G[i][j];
for(int k = (i != 0); k < m && ok; ++k){
printf("%d %d\n", u, G[i+1][p++]);
if(p == G[i+1].size()) ok = false;
}
}
}
return 0;
}

  

CodeForces 404C Restore Graph (构造)的更多相关文章

  1. codeforces C. Restore Graph

    题意:构造一个有n个顶点,每个点度不超过k,然后给出每一个点到达一个定点的最短距离d数组,然后构造出这样的一个图: 思路:排序之后,有两个距离为0的或者没有直接输出-1,然后用两个游动下表,后面的与前 ...

  2. Codeforces 1028E Restore Array 构造

    我发现我构造题真的不会写, 想了好久才想出来.. 我们先把n = 2, 所有数字相等, 所有数字等于0的都特判掉. 找到一个b[ i ] > b[ i - 1 ]的位置把它移到最后一个位置, 并 ...

  3. Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)

    题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...

  4. CodeForces 404C Ivan and Powers of Two

    Ivan and Powers of Two Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  5. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

  6. 【Codeforces 404C】Restore Graph

    [链接] 我是链接,点我呀:) [题意] 每个节点的度数不超过k 让你重构一个图 使得这个图满足 从某个点开始到其他点的最短路满足输入的要求 [题解] 把点按照dep的值分类 显然只能由dep到dep ...

  7. CodeForces 916C Jamie and Interesting Graph (构造)

    题意:给定两个数,表示一个图的点数和边数,让你构造出一个图满足 1-  n 的最短路是素数,并且最小生成树也是素数. 析:首先 1 - n 的最短路,非常好解决,直接 1 连 n 就好了,但是素数尽量 ...

  8. Graph And Its Complement CodeForces - 990D(思维构造)

    题意: 图中有n个点,开始有a个连通块,然后连着的边断开,不连的边连上,变为b个连通块,输出原图的邻接矩阵. 解析: 原图中连通块大于1的图,经过上述操作后,一定变成只有1个连通块的图. 若n != ...

  9. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

随机推荐

  1. CodeForces - 138C: Mushroom Gnomes - 2 (线段树&概率&排序)

    One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her th ...

  2. JavaScript6 新语法 let 有什么优势

    最近看国外的前端代码时,发现ES6的新特性已经相当普及,尤其是 let,应用非常普遍 虽然 let 的用法与 var 相同,但不管是语法语义上,还是性能上,都提升了很多,下面就从这两方面对比一下 语法 ...

  3. linux下ioctl遇到的坑

    在驱动编程里面经常会用到ioctl的系统调用,发现cmd = 2的时候,用户ioctl直接返回-1. 原因在于在linux-x.xx/fs/ioctl.c定义的do_vfs_ioctl函数 int d ...

  4. POJ3468:A Simple Problem with Integers

    浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:http://poj.org/problem?id=3468 分块裸题.对于每个块记录 ...

  5. 如何修改MAC自带的PHP的版本?

    1. 切换到root目录,新建“.profile”文件 cd ~ vim .profile 2.在.profile文件中添加PATH环境变量 比如这样的路径 export PATH=/Applicat ...

  6. (转)SC命令---安装、开启、配置、关闭windows服务 bat批处理

    本文转载自:http://blog.csdn.net/moruna/article/details/9190733 废话不多说,看命令行更直接! 一.直接使用cmd来进行服务的一些操作 1.安装服务 ...

  7. C语言库在不同系统下的后缀

    C语言的静态库与动态库对比分析,各有长短 库:  指由标准常用函数编译而成的文件,旨在提高常用函数的可重用性,减轻开发人员负担.常用的sdtio.h,math.h等                 库 ...

  8. java代码继承基础

    总结:继承是java编程的核心,我搞不明白,子类对象调用父类的成员方法时.父类的带参构造方法有什么用,还赋值了 package com.bc; //普通类 public class yt { publ ...

  9. Regexp:正则表达式应用——实例应用

    ylbtech-Regexp:正则表达式应用——实例应用 1. 实例应用返回顶部 1. 1.验证用户名和密码:("^[a-zA-Z]\w{5,15}$")正确格式:"[A ...

  10. Python操作txt文本文件

    题目: 1.TXT文本文件中的内容为: url:http://119.23.241.154:8080/futureloan/mvc/api/member/login,mobilephone:13760 ...