@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 ...
随机推荐
- ObservableCollection类
https://blog.csdn.net/GongchuangSu/article/details/48832721 https://blog.csdn.net/hyman_c/article/de ...
- java如何使用 tesseract 4.0.0-1.4.4
提示: 建议直接使用tess4j,tess4j是对tesseract的封装,使用更简单 首先引入依赖 <!-- https://mvnrepository.com/artifact/org.by ...
- tomcat9 gzip
我认为apr模式比较屌所以 <Connector port=" protocol="org.apache.coyote.http11.Http11AprProtocol&qu ...
- spring boot自动配置之jdbc(转)
1.DataSource配置 1.1 默认配置application.xml spring.datasource.url=jdbc:mysql://localhost/test spring.data ...
- jS生成二叉树,二叉树的遍历,查找以及插入
js递归,二叉树的操作 //递归算法n次幂 function foo(n) { if (n == 1) { return 1; } else { return n * foo(n - 1); } } ...
- java-多线程的入门_进阶总结
多线程 概述图 1.概述 进程:正在执行中的程序,其实时应用程序在内存中运行的那片空间. 线程:进程中的一个执行单元,负责进程中的程序的运行,一个进程中至少要有一个线程. (进程可以理解为是一个QQ程 ...
- js顺序加载与并行加载
前端优化过程中常提到js的加载方式,下面说下几种常用的加载方式: 1:head标签内插入<script>标签 <script type="text/javaScript&q ...
- Leetcode78. Subsets子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2 ...
- CentOS 7下使用RPM安装mysql的方法。
1.卸载系统自带的 mariadb-lib [root@centos-linux ~]# rpm -qa|grep mariadbmariadb-libs-5.5.44-2.el7.centos.x8 ...
- Centos系统Python环境搭建和项目部署
目录 一.Python 1. 源安装 Python3 2. SCL安装 Python3 3. 虚拟环境venv 4. 安装Flask 5. 安装gunicorn 二.安装Nginx 1. 安装Ngin ...