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).
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.
If there exists such a tree, output "YES", otherwise output "NO".
3 0 2 7 2 0 9 7 9 0
YES
3 1 2 7 2 0 9 7 9 0
NO
3 0 2 2 7 0 9 7 9 0
NO
3 0 1 1 1 0 1 1 1 0
NO
2 0 0 0 0
NO
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.
题解:这一题题意都好看懂,就是给你各个点的距离,问你这可不可能是一棵树。这样我们每次取最小的边,加入已生成的树中,会发现这就是一个最小生成树问题。所以我们只需要根据这个图构造一棵最小生成树。最后在BFS一遍去跟题目给的矩阵比较就行了。如果相同就是YES,否则就输出NO。当然有些特殊情况特判。
#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;
}