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#调试方法

    单步执行 有三种, 一种是每次执行一行(F10): 一种是每次执行一行,但遇到函数调用就会跳到被调用的函数里(F11): 一种是直接执行当前函数里剩下的指令,返回上一级函数(Shift+F11). 还 ...

  2. 为什么 SharedPreferences 可以直接 调用,前面却没有对象

    获取SharedPreferences的两种方式: 1 调用Context对象的getSharedPreferences()方法 2 调用Activity对象的getPreferences()方法 两 ...

  3. Groupby - collection processing

    Groupby - collection processing Iterator and Iterable have most of the most useful methods when deal ...

  4. 高可用与负载均衡(8)之聊聊 LVS重要参数和优化以及监控

    preface 在明白LVS-DR模式的部署之后,我们看看LVS的几个重要参数: 如有问题,请联系我18500777133@sina.cn [root@localhost ~]# ls /proc/s ...

  5. VRRP协议详解

    今天做了lvs的负载均衡的实验,竟然成功了,出乎我意料......哈哈哈哈 http://blog.csdn.net/fanlu319/article/details/7013258

  6. StringBuilder 和 StringBuffer

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

  7. 初学者 的 js 关于checkbox全选的问题

    <script type="text/javascript" language="javascript" > function ff() { var ...

  8. 原创最简单的ORM例子

    这个仅是为了培训做的一个小例子 public class DB     { public static string GetClassName(Type type) { if (type == nul ...

  9. 关于datatable linq的转换

    关于datatable datarow DataTable paraval = GetParaVal(DateCondition, strUrl, Page, RowPage, iYearMonthN ...

  10. C#中使用DateTimePicker控件显示修改日期时间

    1.只显示日期   默认就是   2.只显示时间   修改属性 Format 设为Time ShowUpDown设为true   3.同时显示日期时间   Format设为Custom CustomF ...