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 ...
随机推荐
- Sale.js——快速创建促销样式
小菜编写的又一款jQuery小插件,有兴趣的朋友可以试试~ 简介: 对于一个用于产品展示.销售的网站而言,很可能需要一种促销的特效. 一般而言,我们会在商品图片前加一个促销栏,写上一些促销标语.原价. ...
- tomcat通过conf-Catalina-localhost目录发布项目详解
Tomcat发布项目的方式大致有三种,但小菜认为通过在tomcat的conf/Catalina/localhost目录下添加配置文件,来发布项目,是最佳选择. 因为这样对tomcat的入侵性最小,只需 ...
- Msbuild利用cpu多核加速
msbuild /t:Rebuild /p:Configuration=Release /m /m 自动检测cpu数量启动对应数量进程
- Git 常用的命令总结(欢迎收藏备用)
总结日常开发生产中常用的Git版本控制命令 ------------------------------main-------------------------------- <极客镇楼> ...
- 用distinct在MySQL中查询多条不重复记录值[转]
在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...
- iOS开发-xCode代码格式化xAlign
xCode默认是可以进行代码格式化的,能满足基础开发需求,如果想要个性一些代码对齐方式宏对齐,等号对齐,属性对齐,xAlign就提供了以上三种功能,参考文中效果~ 基础效果 等号对齐: 属性对齐: 宏 ...
- Struts2学习笔记-基本结构
一个普通的的web应用文件结构如下: 1. 最上层是应用名,区分大小写,在浏览器输入应用名的时候,必须与应用名的大小一样,例如:localhost:8080/HelloWorld 2. 在应用下,放有 ...
- Django博客功能实现
开发环境:Python3.5.2和Django1.10.2 username: rootemail: 2016968116@qq.compassword: 123456liuqiuchen 现在我们进 ...
- 在主方法中定义一个大小为10*10的二维字符型数组,数组名为y,正反对角线上存的是‘*’,其余 位置存的是‘#’;输出这个数组中的所有元素。
//在主方法中定义一个大小为10*10的二维字符型数组,数组名为y,正反对角线上存的是‘*’,其余 位置存的是‘#’:输出这个数组中的所有元素. char [][]y=new char [10][10 ...
- python中argparse模块的使用
有两个文件一个是 文件1:sync_shop_source_bimer.sh 文件2:sync_shop_source_bimer.py 在sync_shop_source_bimer.sh 中调用s ...