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. array数组加过滤

    var array = new Array(); array.push(0); array.push(1); array.push(2); var arr = array.filter(functio ...

  2. soupui 使用

  3. Unity 联网小测试(WWW)

    研究了很多联网的方式,甚至把TCP/IP,shock,HTTP的关系都搞清楚了,终于弄明白怎么在Unity中用GET或POST的方式通信了,还是有点小激动的,但是不排除有更好的方式,听说Unity还是 ...

  4. 使用PreApplicationStartMethodAttribute

    第一次见到这个东西是在公司的框架里,刚开始还挺郁闷怎么框架的Application_Start里没东西,初始化的那些代码都哪去了,后来通过一些线索找到了PreApplicationStartMetho ...

  5. wpf button的mouse(leftbutton)down/up,click事件不响应解决办法

    按照WPF的帮助说明,某些控件的路由事件被内部处理了,已经被标记为Handled,自行定义的事件处理代码便不再起作用了,有时候会很郁闷!         不过WPF提供了必要的方法.         ...

  6. range和xrange梳理

    一.python2.7 range 用户获取指定范围内的数,range([start,] stop[, step]) >>> range(1,5) #代表从1到5(不包含5) [1, ...

  7. MongoDB安装部署(一)

    前言 MongoDB是一个由C++语言编写的基于分布式文件存储的数据库,是当前NoSQL数据库中比较热门的一种,旨在为Web应用提供可扩展的高性能数据存储解决方案. MongoDB 简介 MongoD ...

  8. angular state和stateParams

    angular ui-route通过路由传递参数 发送ctrl var model = { data: positionid }; $state.go('app.position.publish', ...

  9. WinForm------BarManager中各种属性设置

    1.offset:红色Tool距离左边Tool的偏移量

  10. ubuntu下安装TexLive和Texmaker

    也可以参考ubuntu14.04配置中文latex完美环境(texlive+texmaker+lyx) 设置中文字体的时候参考ubuntu 下安装 texlive 并设置 ctex 中文套装 1.首先 ...