D. Design Tutorial: Inverse the Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is an easy way to obtain a new task from an old one called "Inverse the problem": we give an output of the original task, and ask to generate an input, such that solution to the original problem will produce the output we provided. The hard task of Topcoder Open 2014 Round 2C, InverseRMQ, is a good example.

Now let's create a task this way. We will use the task: you are given a tree, please calculate the distance between any pair of its nodes. Yes, it is very easy, but the inverse version is a bit harder: you are given an n × n distance matrix. Determine if it is the distance matrix of a weighted tree (all weights must be positive integers).

Input

The first line contains an integer n (1 ≤ n ≤ 2000) — the number of nodes in that graph.

Then next n lines each contains n integers di, j (0 ≤ di, j ≤ 109) — the distance between node i and node j.

Output

If there exists such a tree, output "YES", otherwise output "NO".

Sample test(s)
input
3
0 2 7
2 0 9
7 9 0
output
YES
input
3
1 2 7
2 0 9
7 9 0
output
NO
input
3
0 2 2
7 0 9
7 9 0
output
NO
input
3
0 1 1
1 0 1
1 1 0
output
NO
input
2
0 0
0 0
output
NO
 
给出一个矩阵,表示两个点之间的距离,判断这些点能否构成一颗树。。

1th:构造最小生成树。

我们提取所有的边出来按边排序,因为每次我们知道边的权值>0,

之后每次把边加入集合中,不断构造,类似  kruskal算法,构造出边后

再对每个点进行整张图的DFS求距离

复杂度O(N^2lgN):对所有边排序的复杂度。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std; int n;
int nu[][];
int fa[];
int ct = ; struct edge
{
int x,y,w;
edge(int x = ,int y = ,int w = ):x(x),y(y),w(w){}
bool operator <(const edge &rhs) const
{
return w<rhs.w;
}
}eg[*]; vector<int> g[]; int find(int x) {
if(x==fa[x]) return x;
else return fa[x] = find(fa[x]);
} int node;
int flag= ; int kruskal()
{
sort(eg,eg+ct);
for(int i = ;i<n;i++) fa[i] = i;
for(int i = ;i<ct;i++)
{
int tx = find(eg[i].x);
int ty = find(eg[i].y);
if(tx!=ty)
{
fa[tx] = ty;
g[eg[i].x].push_back(eg[i].y);
g[eg[i].y].push_back(eg[i].x);
//cout<<eg[i].x<<' '<<eg[i].y<<' '<<eg[i].w<<'?'<<endl;
}
}
} int dfs(int v,int now,int fa)
{
for(int i = ;i<g[v].size();i++)
{
int t = g[v][i];
if(t == fa) continue;
//cout<<node<<' '<<t<<' '<<nu[v][t]+now<<endl;
int wt = nu[v][t]+now;
if(nu[node][t]!=wt) {flag = ;return -;}
else
dfs(t,wt,v);
}
return ;
} int main()
{
scanf("%d",&n);
int num;
for(int i = ;i<n;i++) g[i].clear();
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
{
scanf("%d",&num);
nu[i][j] = num;
if(i>j)
{
eg[ct].x = i;
eg[ct].y = j;
eg[ct++].w = num;
}
if(nu[i][j]!=&&i==j) flag = ;
if(j<i&&nu[i][j]!=nu[j][i]) flag = ;
if(nu[i][j]==&&i!=j) flag = ;
}
if(flag!=)
{
kruskal();
for(int i = ;i<n;i++)
{
node = i;
if(dfs(i,,-)==-) {flag = ; break;}
}
}
if(flag == ) puts("NO");
else puts("YES");
return ;
}
/*
7
0 1 1 2 2 3 3
1 0 2 1 3 2 4
1 2 0 3 1 4 2
2 1 3 0 4 1 5
2 3 1 4 0 5 1
3 2 4 1 5 0 6
3 4 2 5 1 6 0
*/

第二种做法不得不佩服!1

这个特种点很难在比赛中发现啊

关键点:我们知道一个点到其他所有点都有一条最小距离的边--假设A到其他点最小距离的点是B,A-B一定是直接连接的。假设距离是X。

然后假设一个点C,C到B点要么比C到A点近X,要么C到A点远X。具体可以画图。

通过这个方法可以判断数据是否合法!

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; int num[][];
int n; int main()
{
scanf("%d",&n);
for(int i = ;i<n;i++)
for(int j = ;j<n;j++)
{
scanf("%d",&num[i][j]);
}
int flag = ;
for(int i = ;i<n;i++)
for(int j = i;j<n;j++)
{
if(num[i][j]!=&&i==j) flag = ;
if(num[i][j]!=num[j][i]) flag = ;
if(num[i][j]==&&i!=j) flag = ;
} for(int i = ;i<n;i++)
{
int inf = ;
int pos = -;
for(int j = ;j<n;j++)
{
if(i==j) continue;
if(num[i][j]<inf)
{
inf = num[i][j];
pos = j;
}
} for(int j = ;j<n;j++)
{
if(j==i||j==pos) continue;
if(abs(num[i][j]-num[pos][j])!=inf)
flag = ;
}
}
if(flag == ) puts("NO");
else puts("YES");
return ;
}

Codeforces #270 D. Design Tutorial: Inverse the Problem的更多相关文章

  1. Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS

    题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...

  2. cf472D Design Tutorial: Inverse the Problem

    D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...

  3. D. Design Tutorial: Inverse the Problem 解析含快速解法(MST、LCA、思維)

    Codeforce 472D Design Tutorial: Inverse the Problem 解析含快速解法(MST.LCA.思維) 今天我們來看看CF472D 題目連結 題目 給你一個\( ...

  4. codeforces D. Design Tutorial: Inverse the Problem

    题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...

  5. Design Tutorial: Inverse the Problem

    Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...

  6. 【CF】270D Design Tutorial: Inverse the Problem

    题意异常的简单.就是给定一个邻接矩阵,让你判定是否为树.算法1:O(n^3).思路就是找到树边,原理是LCA.判断树边的数目是否为n-1.39-th个数据T了,自己测试2000跑到4s.算法2:O(n ...

  7. Codeforces Round #270--B. Design Tutorial: Learn from Life

    Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes in ...

  8. codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)

    题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...

  9. Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

随机推荐

  1. C#6.0特性笔记

    Visual Studio 2015的C#6.0,今天无意中看这个视频,怕忘记其中的特性,故此进行记录. public class Point { //Getter专属自动属性 public int ...

  2. 使用Scrapy爬虫框架简单爬取图片并保存本地(妹子图)

    初学Scrapy,实现爬取网络图片并保存本地功能 一.先看最终效果 保存在F:\pics文件夹下 二.安装scrapy 1.python的安装就不说了,我用的python2.7,执行命令pip ins ...

  3. PTA fzu_oop_east

    GitHub链接: 传送门 5-1该日是该年的第几天 定义一个日期类Date,内有数据成员年.月.日,另有成员函数:构造函数用于初始化数据成员,输出,闰年的判断. 编写主函数:创建日期对象,计算并输出 ...

  4. Ubuntu 12.04 root账户开启及密码重设

    以普通用户登录,root账号的开启.关闭和密码设置,命令如下: sudo passwd -u root # 启用root账户 sudo passwd root # 设置root 密码(包括重设) su ...

  5. SQL Server编程(02)自定义函数

    在编程过程中,我们通常把特定的功能语句块封装称函数,方便代码的重用.我们可以在SQL Server中自定义函数,根据函数返回值的区别,我们自定义的函数分两种:标量值函数和表值函数. 自定义函数的优点: ...

  6. js003-基本概念

    js003-基本概念 3.1 语法 3.1.1 区分大小写 ECMAScript中的一切(变量.函数名和操作符)都是区分大小写的,并且不能用关键字作为函数名:如 typeof. 3.1.2 标识符 所 ...

  7. 图片上传利用<iframe></iframe>标签实现无刷新上传图片

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. easyUI Form表单的密码验证是否相同

    一.js文件中的代码: $(function(){ $.extend($.fn.validatebox.defaults.rules, { equals: {//定义一个比较相等与否的函数 valid ...

  9. codeforces 712B. Memory and Trident

    题目链接:http://codeforces.com/problemset/problem/712/B 题目大意: 给出一个字符串(由'U''D''L''R'),分别是向上.向下.向左.向右一个单位, ...

  10. FingerGestures for Unity3D

    FingerGestures http://fingergestures.fatalfrog.com