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 ...
随机推荐
- windows装了双系统设置默认启动系统
我们装了双系统后,在开机设置时会自动启动其中一个系统,有时提示时间会很短,以至于不能判断自己是否装了双系统,以下,当电脑打开后,我们可以观察是否装了双系统 1.按组合键<Win+R>,打开 ...
- ios 学习常用网站
一 ,别的blog上的内容 iPhone 4 与iPad开发基础教程 一.邮件列表: 1.cocoa-dev http://lists.apple.com/mailman/listinfo/cocoa ...
- 利用jsoup爬虫工具,爬取数据,并利用excel导出
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...
- js解析格式化json日期
代码: function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseIn ...
- gearman mysql udf
gearman安装 apt-get install gearman gearman-server libgearman-dev 配置bindip /etc/defalut/gearman-job-se ...
- How do I check if a type is a subtype OR the type of an object?
To check if a type is a subclass of another type in C#, it's easy: typeof (SubClass).IsSubclassOf(ty ...
- oprofile
一.原理 在关注事件发生一定次数时,进行一次采样,记录下需要的信息(比如指令寄存器或栈寄存器信息). 二.参数 项 说明 eventname 要关注的事件名称,常用的事件名称及功能如下: CP ...
- Linux下查看mysql、apache是否安装,安装,卸载等操作
Linux下查看mysql.apache是否安装,并卸载. 指令 ps -ef|grep mysql 得出结果 root ? :: /bin/sh /usr/ ...
- hdu 1845
一看题意就是二分匹配问题,建边是双向的,两个集合都是n个点 这题的图很特殊,每个点都要与三个点相连,在纸上画了六个点的图就感觉此图最大匹配肯定是六,除以2就是原图的匹配了,就感觉这样的图肯定会达到满匹 ...
- Redis__WindowsServer主从服务部署及调用实例
本文转自:http://www.cnblogs.com/gossip/p/4898653.html 一.先谈谈单个Redis服务的安装 使用的redis是2.8.17版本,从官网下载解 ...