@topcoder - 2017TCOAlgorithmRound2A - D1L2@ DistanceZeroAndOne
@description@
一个 n 个点的无向简单的连通图,编号从 0 到 n-1。
现给出每个点到点 0 的距离 dist0[]、每个点到点 1 的距离 dist1[],还原整张图,或判断无解。
Constraints
n 在 2 到 50 之间。
dist0 与 dist1 中的元素都在 0 到 n-1 之间。
Examples
0)
{0,2,1}
{2,0,1}
Returns: {
"NNY",
"NNY",
"YYN" }
整张图为 0 - 2 - 1。
{0,2,1}
{1,0,2}
Returns: { }
dist0[1] ≠ dist1[0]。
@solution@
根据三角形不等式,假如 u 与 v 之间有边,则 |dist0[u] - dist0[v]| ≤ 1 且 |dist1[u] - dist1[v]| ≤ 1。
如果 u, v 之间可以连边(即满足三角形不等式),则连 (u, v)。
显然边连的越多,点之间的距离越精确。
所以要是有解,则上面的连边方案一定可以得到一个合法解。
我们连完边过后再跑两边 bfs 检验一下这个图是否满足 dist0 与 dist1 的限制。
@accepted code@
#include<queue>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
class DistanceZeroAndOne{
#define MAXN 50
private:
int a[MAXN][MAXN], n;
int abs(int x) {return x >= 0 ? x : -x;}
int d[MAXN];
public:
void bfs(int x) {
for(int i=0;i<n;i++)
d[i] = n;
d[x] = 0; queue<int>que; que.push(x);
while( !que.empty() ) {
int f = que.front(); que.pop();
for(int i=0;i<n;i++)
if( a[f][i] && d[f] + 1 < d[i] ) {
d[i] = d[f] + 1, que.push(i);
}
}
}
vector<string>ans;
vector<string>construct(vector<int>d0, vector<int>d1) {
ans.clear(), n = d0.size();
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if( i != j && abs(d0[i] - d0[j]) <= 1 && abs(d1[i] - d1[j]) <= 1 )
a[i][j] = 1;
bool flag = true;
bfs(0);
for(int i=0;i<n;i++)
if( d[i] != d0[i] )
flag = false;
if( !flag ) return ans;
bfs(1);
for(int i=0;i<n;i++)
if( d[i] != d1[i] )
flag = false;
if( !flag ) return ans;
for(int i=0;i<n;i++) {
string s = "";
for(int j=0;j<n;j++)
if( a[i][j] ) s = s + 'Y';
else s = s + 'N';
ans.push_back(s);
}
return ans;
}
};
@details@
好像。。。也没什么细节。。。
@topcoder - 2017TCOAlgorithmRound2A - D1L2@ DistanceZeroAndOne的更多相关文章
- @topcoder - TCO19 Regional Wildcard Wildcard Round - D1L2@ Diophantine
目录 @description@ @solution@ @accepted code@ @details@ @description@ 令 p[] 为质数序列:p[0] = 2, p[1] = 3, ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
随机推荐
- TZ_13_负载均衡-Robbin
1.但是实际环境中,我们往往会开启很多个user-service的集群.此时我们获取的服务列表中就会有多个,到底该访问哪一个呢? 一般这种情况下我们就需要编写负载均衡算法,在多个实例列表中进行选择. ...
- PHP--时间格式处理
Ymd格式转Y-m-d或转成时间戳将Ymd格式如19930811转成1993-08-11格式 date('Y-m-d',strtotime('19930811') 将Ymd格式如19930811转成时 ...
- Win7下设置WiFi热点
Win7下设置WiFi热点 今天研究了下Win7设置WIFI热点,Connectify神马的都是浮云~亲測可用,现拿出来分享下~ 1.点击"開始",再点击"执行" ...
- oracle之FUNCTION拙见
一.介绍 函数(Function)为一命名的存储程序,可带参数(有无均可),有返回值 函数和过程的结构类似,但必须有一个RETURN子句,用于返回函数值. 函数说明要指定函数名.返回值的类型,以及参数 ...
- 洛谷P1470 最长前缀
P1470 最长前缀 Longest Prefix 题目描述 在生物学中,一些生物的结构是用包含其要素的大写字母序列来表示的.生物学家对于把长的序列分解成较短的序列(即元素)很感兴趣. 如果一个集合 ...
- Leetcode49. Group Anagrams字母异位词分组
给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "tan&quo ...
- http响应头信息
HTTP 响应头信息 HTTP请求头提供了关于请求,响应或者其他的发送实体的信息. 在本章节中我们将具体来介绍HTTP响应头信息. 应答头 说明 Allow 服务器支持哪些请求方法(如GET.POST ...
- CentOS安装fortune+cowsay
1.先找下看有没 2.安装 yum -y install fortune-mod 3.执行fortune 应该可以输出了,接着去弄中文词库,阮一峰的: git clone git@github.com ...
- 批处理启动应用程序(win)
@echo off net session >nul 2>&1 " ( echo Oops: This tools must run with administrator ...
- truncate 、delete、drop的区别
TRUNCATE TABLE 在功能上与不带 Where 子句的 Delete 语句相同:二者均删除表中的全部行.但 TRUNCATE TABLE 比 Delete 速度快,且使用的系统和事务日志资源 ...