# 【CODEFORCES】 D. Design Tutorial: Inverse the Problem

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
Note

In the first example, the required tree exists. It has one edge between nodes 1 and 2 with weight 2, another edge between nodes 1 and 3 with weight 7.

In the second example, it is impossible because d1, 1 should be 0, but it is 1.

In the third example, it is impossible because d1, 2 should equal d2, 1.

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int a[2005][2005],n,father[2005],q[2005],flag[2005];
int dist[2005][2005],cc=0,h[2005],d[2005][2005];

struct edge
{
int from,to,dist;
};

struct edge e[4000000];

int getfather(int k)
{
if (k==father[k])
return k;
return father[k]=getfather(father[k]);
}

void qsort(int low,int high)
{
int i=low,j=high,t=e[(low+high)/2].dist;
while (i<j)
{
while (e[i].dist<t) i++;
while (e[j].dist>t) j--;
if (i<=j)
{
int k;
k=e[i].from; e[i].from=e[j].from; e[j].from=k;
k=e[i].to; e[i].to=e[j].to; e[j].to=k;
k=e[i].dist; e[i].dist=e[j].dist; e[j].dist=k;
i++; j--;
}
}
if (low<j) qsort(low,j);
if (i<high) qsort(i,high);
}

void bfs(int k)
{
memset(q,0,sizeof(q));
memset(flag,0,sizeof(flag));
int i,j;
i=1; j=1;
q[j++]=k; flag[k]=1;
while (j-i>=1)
{
for (int s=1;s<=h[q[i]];s++)
if (!flag[d[q[i]][s]])
{
q[j++]=d[q[i]][s]; flag[d[q[i]][s]]=1;
if (!dist[k][d[q[i]][s]])
{
dist[k][d[q[i]][s]]=dist[k][q[i]]+a[q[i]][d[q[i]][s]];
dist[d[q[i]][s]][k]=dist[k][d[q[i]][s]];
if (dist[k][d[q[i]][s]]!=a[k][d[q[i]][s]])
{
cout <<"NO"<<endl;
cc=1;
return;
}
}
}
i++;
}
}

int main()
{
int k=0;
scanf("%d",&n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
k++;
scanf("%d",&a[i][j]);
e[k].from=i;
e[k].to=j;
e[k].dist=a[i][j];
}
for (int i=1;i<=n;i++)
for (int j=1;j<=i;j++)
{
if (i==j && a[i][j]!=0)
{
cout <<"NO"<<endl;
return 0;
}
if (i!=j && a[i][j]==0)
{
cout <<"NO"<<endl;
return 0;
}
if (a[i][j]!=a[j][i])
{
cout <<"NO"<<endl;
return 0;
}
}
for (int i=1;i<=n;i++)
father[i]=i;
qsort(1,k);
for (int i=1;i<=k;i++)
{
if (getfather(e[i].from)!=getfather(e[i].to))
{
int f1=getfather(e[i].from),f2=getfather(e[i].to);
father[f1]=father[f2];
d[e[i].from][++h[e[i].from]]=e[i].to;
d[e[i].to][++h[e[i].to]]=e[i].from;
}
}
//bfs
for (int i=1;i<=n;i++)
{
bfs(i);
if (cc) return 0;
}
cout <<"YES"<<endl;
return 0;
}

• 本文已收录于以下专栏：

## Codeforces-766D-The Door Problem(2-SAT-并查集解)

• jinglinxiao
• 2017年02月25日 22:21
• 352

## Codeforces Round #270 D. Design Tutorial: Inverse the Problem

• u012841845
• 2014年09月29日 23:38
• 513

## CodeForces - 472D Design Tutorial: Inverse the Problem——最小生成树

#include #include #include #include using namespace std; typedef long long ll; const int maxn = ...
• hao_zong_yin
• 2017年11月15日 00:04
• 73

## Codeforces 472D. Design Tutorial: Inverse the Problem

D. Design Tutorial: Inverse the Problem time limit per test2 seconds memory limit per test256 mega...
• nare123
• 2016年08月16日 08:39
• 345

## 【codeforces】 472D Design Tutorial: Inverse the Problem

Let's think it in the following way: for the minimal length edge, it must belong the the tree, ..., ...
• blankcqk
• 2014年09月29日 18:35
• 554

## CodeForces 472D Design Tutorial: Inverse the Problem (最小生成树)

• u011265346
• 2015年01月06日 21:44
• 624

## cf472D Design Tutorial: Inverse the Problem

D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per te...
• zhb1997
• 2014年09月30日 22:53
• 424

## Codeforces 472G Design Tutorial: Increase the Constraints(分块+FFT)

#include #include #include #include using namespace std; #define N 550020 #define LL long long...
• u013654696
• 2016年03月15日 20:20
• 284

## Codeforces 761D-Dasha and Very Difficult Problem

Dasha and Very Difficult Problem time limit per test 2 seconds memory limit per test 256 megabyt...
• a664607530
• 2017年02月02日 00:21
• 411

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

Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 25...
• u013446688
• 2014年10月29日 07:30
• 858

举报原因： 您举报文章：【CODEFORCES】 D. Design Tutorial: Inverse the Problem 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)