Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)
题目大意
一个含有 n 个顶点的无向图,顶点编号为 1~n。给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离。这个图有个限制:每个点的度不能超过 k
现在,请构造一个这样的无向图,要求不能有自环,重边,且满足距离数组和度数限制,输出图中的边;如果无解,输出 -1
数据规模:1 ≤ k < n ≤ 105,0 ≤ d[i] < n
做法分析
第一眼做法:SPFA 或者 BFS,想了想,还是乱搞
根据 d 数组直接构造这个图,因为最短路具有最优子结构,所以,d[i] 为 0 的点只有一个,从 0 到 maxLen 的所有距离,都一定在 d 数组中出现过,然后就考虑度数限制。
显然,距离为 len + 1 的点是由距离为 len 的点到达的,于是,将所有的点按照距离分类。要尽量满足度数限制,每个点的度数就要尽量少,所以,距离相同的点之间不能有边。于是,构造出来的解中,所有的边 (u, v) 一定满足这样的性质:d[u] == d[v] + 1(假设 d[u] > d[v]),这让我想起了 ISAP 求最大流的 gap 优化
然后,怎么保证每个点都尽量满足度数限制呢?平均分配
将距离为 len + 1 的点平均分配到距离为 len 的点里面去,这样一定是最优的,如果这样都不满足度数限制,肯定无解了
参考代码
#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int N = ; int k, n, d[N];
vector <int> len[N];
vector < pair <int, int> > edge; int main() {
scanf("%d%d", &n, &k);
for (int i = ; i <= n; i ++) len[i].clear();
for (int i = ; i <= n; i ++) {
scanf("%d", &d[i]);
len[d[i]].push_back(i);
}
if (len[].size() != ) {
printf("-1\n");
return ;
}
int maxLen = n;
for (; len[maxLen].size() == ; maxLen --);
for (int i = ; i <= maxLen; i ++) {
if (len[i].size() == ) {
printf("-1\n");
return ;
}
}
edge.clear();
for (int i = ; i <= maxLen; i ++) {
int cnt1 = (int)len[i - ].size();
int cnt2 = (int)len[i].size();
int cnt = cnt2 / cnt1 + (cnt2 % cnt1 != );
if (cnt + (i - != ) > k) {
printf("-1\n");
return ;
}
for (int id1 = , id2 = ; id1 < cnt1 && id2 < cnt2; id1 ++) {
for (; id2 < (id1 + ) * cnt && id2 < cnt2; id2 ++) {
edge.push_back(make_pair(len[i - ][id1], len[i][id2]));
}
}
}
int cnt = (int)edge.size();
printf("%d\n", cnt);
for (int i = ; i < cnt; i ++) {
printf("%d %d\n", edge[i].first, edge[i].second);
}
return ;
}
C. Restore Graph
题目链接
Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)的更多相关文章
- Codeforces Round #381 (Div. 2) A B C 水 构造
A. Alyona and copybooks time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)
Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题
Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...
- Codeforces Round #237 (Div. 2)
链接 A. Valera and X time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inp ...
- Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #334 (Div. 2) A. Uncowed Forces 水题
A. Uncowed Forces Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/604/pro ...
- Codeforces Round #112 (Div. 2) D. Beard Graph
地址:http://codeforces.com/problemset/problem/165/D 题目: D. Beard Graph time limit per test 4 seconds m ...
随机推荐
- [MFC] MFC音乐播放器 傻瓜级教程 网络 搜索歌曲 下载
>目录< >——————————————————————< 1.建立工程 1.建立一个MFC工程,命名为Tao_Music 2.选择为基本对话框 3.包含Windows So ...
- AngularJS 源码分析3
本文接着上一篇讲 上一篇地址 回顾 上次说到了rootScope里的$watch方法中的解析监控表达式,即而引出了对parse的分析,今天我们接着这里继续挖代码. $watch续 先上一块$watch ...
- javascript 中关于对象转换数字值的一些特点
下面是摘至<Javascript 高级程序设计第三版>里的一段话 是关于对象转换数字值的一些规则 "在应用于对象时,先调用对象的valueOf()方法以取得一个可供操作的值.然后 ...
- 建立一个名叫Cat的类
//属性 成员变量 String name; int age; String color; //方法 函数 成员函数 void name() { System.out.println("名字 ...
- Enterprise Solution 2.2 开发帮助文档集合
首先是一个PPT文档,从宏观层面展示Enterprise Soltion的几个功能特色. Enterprise Solution解决方案安装与配置 将源代码解决方案和演示程序在电脑中进行配置,作为了解 ...
- Bitbucket Repository size limits
Repository size limits By Justen Stepka, Product Manager on May 30, 2014 In order to improve and mai ...
- AngularJS中实现日志服务
本篇体验使用AngularJS自定义一个记录日志的服务. 在AngularJS中,服务的一些写法是这样的: var app = angular.module('app',[]); app.provid ...
- java微信开发API第一步 服务器接入
I如何接入服务器,下面就为大家进行介绍 一.说明 * 本示例根据微信开发文档:http://mp.weixin.qq.com/wiki/home/index.html最新版(4/3/2016 5:34 ...
- mybatis中传入String类型参数异常
在使用mybatis时,写了一条sql语句,只有一个String类型的参数, 示例代码 <select id="getApplyNum" parameterType=&quo ...
- mysql中You can’t specify target table for update in FROM clause错误解决方法
mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...