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 ...
随机推荐
- (转)Linux sort命令
Linux 的 ‘sort’命令的14个有用的范例(一) 2015-5-2 10:29 评论: 3 收藏: 10 编译自:http://www.tecmint.com/sort-command- ...
- POJ2985 The k-th Largest Group (并查集+treap)
Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is ...
- 大整数乘法(Comba 乘法 (Comba Multiplication)原理)
Comba 乘法以(在密码学方面)不太出名的 Paul G. Comba 得名.上面的笔算乘法,虽然比较简单, 但是有个很大的问题:在 O(n^2) 的复杂度上进行计算和向上传递进位,看看前面的那个竖 ...
- ACC 001 C - Shorten Diameter 图论
题目: Problem Statement Given an undirected tree, let the distance between vertices \(u\) and \(v\) be ...
- java 简单实现socket
server端 package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...
- 图片的base64编码通过javascript生成图片--当前URL地址的二维码应用
前面的话 在电脑端发现一篇好的博文,想在手机上访问.这时,就必须打开手机浏览器输入长长的URL地址才行,非常不方便.如果在博客标题的后面跟一张小的图片,点击该图片后,出现一张二维码的大图,然后再通过手 ...
- BZOJ3403:[USACO2009OPEN]Cow Line
浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...
- TCP之半关闭与CLOSE_WAIT
终止一个连接要经过4次握手.这由TCP的半关闭(half-close)造成的.既然一个TCP连接是全双工(即数据在两个方向上能同时传递,可理解为两个方向相反的独立通道),因此每个方向必须单独地进行关闭 ...
- get方法传递中文数据的时候如何进行转码
首先,如果是在js端的代码,用window.href进行请求时,需要进行转码 前台jsp中: var param = document.getElementById('param').value;pa ...
- 【SymmetricDS】实现新的数据库方言
2018-04-20 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/8890785.html 本文翻译自SymmetricDS官方文档 I ...