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 ...
随机推荐
- Openstack Murano(kilo)二次开发之添加Volume
Openstack Murano(kilo)二次开发之添加Volume 欢迎转载,转载请注明出处:http://www.cnblogs.com/fmnisme/p/openstack_murano_a ...
- my linux tech object
I want to be a linux kernel development engineer. That's my dream.
- django with mysql (part-4)
step01: write the ( views.py ) again .. vim views.py step02: configure your (urls.py) step03: check ...
- atitit.hbnt orm db 新新增更新最佳实践o7
atitit.hbnt orm db 新新增更新最佳实践o7 1. merge跟个save了. 1 2. POJO对象处于游离态.持久态.托管态.使用merge()的情况. 1 3. @Dynamic ...
- paip.提升性能3倍--使用栈跟VirtualAlloc代替堆的使用.
paip.提升性能3倍--使用栈跟VirtualAlloc代替堆的使用. #----为什么要设计堆栈,它有什么独特的用途? 为了性能 .... 堆比栈的性能 也有的说法为了编程容易...这个是错误的 ...
- 爬虫神器xpath的用法(四)
使用xpath多线程爬取百度贴吧内容 #encoing=utf-8 from lxml import etree from multiprocessing.dummy import Pool as T ...
- SVN命令模式批量更新多个项目文件
使用svn作为版本管理是,在一个仓库下边同时建立多个项目,每天上班都需要一个个更新,为了发挥程序员懒的精神,能让电脑做的,绝不手工操作.作为自动化处理,在windows环境,首先想到了bat Tort ...
- emoji表情引发的JNI崩溃
今天突然接到客服那边的反馈说,有玩家反馈进游戏后不久就崩溃了,我先是怀疑网络问题,因为一连接聊天成功后就挂了.之后用logcat抓日志,发现挂在jni那里了 JNI DETECTED ERROR IN ...
- NEWS - InstallShield 2015 正式发布
如果您需要为Windows®应用程序创建安装,InstallShield®便是您的最佳解决方案.在为桌面.服务器.云.Web和虚拟环境构建可靠的Windows Installer (MSI)和Inst ...
- Chkdsk scan needed on volume
After we extended the volume in storage array, in Failover cluster, it shows the volume is of 30.0 T ...