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. A.Kaw矩阵代数初步学习笔记 1. Introduction

    “矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...

  2. JavaWeb学习总结-05 Servlet 学习和使用(01)

    一 Servlet的原理 1 Servlet 的创建 当Servlet容器启动web应用时,需要立即加载Servlet时: Servlet容器启动web应用时,将按照指定的顺序初始化Servlet,需 ...

  3. Mysql和Memcached的连动

    Memcached 和 mysqld 的联通 一 概述: what's UDFs ? UDFs是User Defined Functions的缩写,表示Mysql用户自定义的函数,应用程序可以利用这些 ...

  4. StringBuilder 和 StringBuffer

    这两者唯一的不同就在于,StringBuffer是线程安全的,而StringBuilder不是.当然线程安全是有成本的,影响性能,而字符串对象及操作,大部分情况下,没有线程安全的问题,适合使用Stri ...

  5. [转发]Dumps of system information with Apple computers

    In this article, I gathered up all the dumps, who found. If you see something new table will be upda ...

  6. 将文件路径以"\"隔开

    将文件路径以"\"隔开,这货搞了我一小时...C++返回一维数组,字符串数组还是要再看看 ]) { ; //string s_array[30]; //局部变量,如果使用retur ...

  7. [转]h5页面测试总结

    转自http://www.blogjava.net/qileilove/archive/2014/07/24/416154.html?utm_source=tuicool H5页面测试总结 其实经过几 ...

  8. sphinx安装记录 转

    [转]sphinx服务器安装及配置详解 安装PHP sphinx扩展 1.架构:ip192.168.0.200 redhat5.4(64位)2.安装   #cd /usr/local/src   #y ...

  9. LyX-220-Installer-3

    所见即所得 单独安装这个写作业可以了,要发论文用CTeX Ctrl + M 打开数学输入,里面可以输入 TeX 代码

  10. C#值类型参数传递的性能开销

    Performance issues Let's dig a little deeper. When data is passed into methods as value type paramet ...