CodeForces 404C Restore Graph (构造)
题意:让人构造一个图,满足每个结点边的数目不超过 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 (构造)的更多相关文章
- codeforces C. Restore Graph
题意:构造一个有n个顶点,每个点度不超过k,然后给出每一个点到达一个定点的最短距离d数组,然后构造出这样的一个图: 思路:排序之后,有两个距离为0的或者没有直接输出-1,然后用两个游动下表,后面的与前 ...
- Codeforces 1028E Restore Array 构造
我发现我构造题真的不会写, 想了好久才想出来.. 我们先把n = 2, 所有数字相等, 所有数字等于0的都特判掉. 找到一个b[ i ] > b[ i - 1 ]的位置把它移到最后一个位置, 并 ...
- Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)
题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...
- CodeForces 404C Ivan and Powers of Two
Ivan and Powers of Two Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- [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 916C Jamie and Interesting Graph (构造)
题意:给定两个数,表示一个图的点数和边数,让你构造出一个图满足 1- n 的最短路是素数,并且最小生成树也是素数. 析:首先 1 - n 的最短路,非常好解决,直接 1 连 n 就好了,但是素数尽量 ...
- Graph And Its Complement CodeForces - 990D(思维构造)
题意: 图中有n个点,开始有a个连通块,然后连着的边断开,不连的边连上,变为b个连通块,输出原图的邻接矩阵. 解析: 原图中连通块大于1的图,经过上述操作后,一定变成只有1个连通块的图. 若n != ...
- 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 ...
随机推荐
- django1.7 HTML模板中{%url%}的使用
转载:https://my.oschina.net/jastme/blog/345265
- C++对C语言的拓展(2)—— inline内联函数
C语言中有宏函数的概念.宏函数的特点是内嵌到调用代码中去,避免了函数调用 的开销.但是由于宏函数的处理发生在预处理阶段,缺失了语法检测和有可能带来的语意差错. 1.内联函数基本概念 C++提供了 in ...
- Angular 隨記
Windows下更新Node 和NPM方法 管理員模式打開powershell 執行以下命令: Set-ExecutionPolicy Unrestricted -Scope CurrentUser ...
- 在Mac系统下如何恢复SourceTree全局忽略的文件
在家目录“~”下编辑 “.gitignore_global ” 文件即可:vim .gitignore_global
- HDU1576(扩展欧几里得)
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- rtsp/rtp over http
转载:http://linux-expert.blog.163.com/blog/static/764585292008530912712/ rtsp/rtp over http C->S (g ...
- ucos ii 百度官方介绍
μC/OS II(Micro-Controller Operating System Two)是一个可以基于ROM运行的.可裁剪的.抢占式.实时多任务内核,具有高度可移植性,特别适合于微处 ...
- SpringBoot自动化配置之三:深入SpringBoot:自定义EnableAutoConfiguration
前言 上面几篇文章介绍了SpringFramework的一些原理,这里开始介绍一下SpringBoot,并通过自定义一些功能来介绍SpringBoot的原理.SpringBoot在SpringFram ...
- 生产者与消费者--demo1---bai
import java.util.ArrayList; import java.util.List; import java.util.Random; //自定义类,描述仓库 public class ...
- 2016.1.1 VS中宏的使用技巧点滴
Dim selection As TextSelection = DTE.ActiveDocument.Selection'定义 TextSelection 对象 selection.StartOfL ...