For ACM

# 【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;
}


09-29 795

08-16 372

01-06 659

07-02 37

07-04 86

08-19 1629

08-28 325

07-04 15

09-29 523

09-29 566