题目大意

一个含有 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

题目链接

C. Restore Graph

Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)的更多相关文章

  1. 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 ...

  2. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  3. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  4. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  5. 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 ...

  6. Codeforces Round #237 (Div. 2)

    链接 A. Valera and X time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inp ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. HBase在单Column和多Column情况下批量Put的性能对比分析

    作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2013/11/28/hba ...

  2. 【系统移植】kernel分析

    内核启动流程 第二阶段 starte_kernel: | rest_init:  |  kernel_init   |   do_basic_setup(); // 加载驱动    |    do_i ...

  3. paip.消除 Java 的冗长try/catch/finally

    paip.消除 Java 的冗长try/catch/finally 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blo ...

  4. javaweb学习总结(七)——HttpServletResponse对象(一)

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  5. Linux内核启动分析过程-《Linux内核分析》week3作业

    环境搭建 环境的搭建参考课件,主要就是编译内核源码和生成镜像 start_kernel 从start_kernel开始,才真正进入了Linux内核的启动过程.我们可以把start_kernel看做平时 ...

  6. Docker + Consul 多数据中心模拟

    Docker + Consul 多数据中心模拟 1. dc1搭建 docker run -d --name node1 -h node1 progrium/consul -server -bootst ...

  7. 解决JS传参中文乱码

    function PopupFK(cNum,type){ var url = "contract!Paying.action"; url = url + "?contra ...

  8. 解决Visual Studio 2010/2012的RC4011 warnings

    如果在vc10/11工程的rc文件中有以下任意一行代码: #include <winuser.h> #include <richedit.h> 那么vc将会给出一对警告: C: ...

  9. Pinterest 架构:两年内月 PV 从零到百亿【翻译】

    原文地址 这篇文章,采用 Markdown 方式,写的还是比较实在的,要是有架构图就好了. Pinterest 是图片版的 Twitter,用户把自己感兴趣的东西用图钉(Pins)钉在钉板(PinBo ...

  10. C++实现单例模式

    昨天面试的时候,面试官让我用C++或Java实现一个单例模式. 因为设计模式是在12年的时候学习过这门课,而且当时觉得这门课很有意思,所以就把课本读了几遍,所以印象比较深刻,但是因为实际编程中很少注意 ...