Codeforces 472D. Design Tutorial: Inverse the Problem

D. Design Tutorial: Inverse the Problem
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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”.

Examples
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<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
#define F(x,a,b) for (int x=a;x<=b;x++)
#define me(x) memset(x,0,sizeof(x))
#define mme(x) memset(x,-1,sizeof(x))
#define Ea(x) _f(f[EE[x].a])
#define Eb(x) _f(f[EE[x].b])
#define reset F(i,1,n) f[i]=i;
#define ll long long
ll fr,head[2005],vis[2005],ma[2005][2005],f[2005],a[2005][2005];
struct p{ll a,b,v;}EE[2000005];
struct q{ll node,next,v;}E[200005];
bool cmp(p aa,p bb){return aa.v<bb.v;}
int _f(int x) {return f[x]=f[x]==x?x:_f(f[x]);}
void insertt(int x,int y,ll val){E[fr]={y,head[x],val};head[x]=fr++;}
void dfs(int st,int stand,int pr)
{
   vis[st]=1;
   for (int i=head[st];i!=-1;i=E[i].next)
   {
      if (!vis[E[i].node])
    {
       ma[stand][E[i].node]=pr+E[i].v;
       dfs(E[i].node,stand,ma[stand][E[i].node]);
    }
   }
}
int main()
{
    int n;int cnt=0; fr=0;int coutt=0;int flag=1;
    scanf("%d",&n);
    F(i,1,n)F(j,1,n){scanf("%d",&a[i][j]);if (a[i][j]&&i<j){flag=0;EE[++cnt].a=i;EE[cnt].b=j;EE[cnt].v=a[i][j];}}
    if (n==1&&!a[1][1]) {printf("YES\n");return 0;}
    if (flag) {printf("NO\n");return 0;}
    F(i,1,n)F(j,1,n){if (a[i][j]!=a[j][i]||(a[i][j]&&i==j)||(i!=j&&a[i][j]==0)){printf("NO\n");return 0;}}
    sort(EE+1,EE+cnt+1,cmp); reset mme(head);me(vis);
    F(i,1,cnt)
    {
        if (Ea(i)!=Eb(i))
        {
            f[Ea(i)]=Eb(i);
            coutt++;
            insertt(EE[i].a,EE[i].b,EE[i].v);insertt(EE[i].b,EE[i].a,EE[i].v);
        }
        if (coutt==n-1) break;
    }
    F(i,1,n){me(vis);dfs(i,i,0);}
    F(i,1,n)F(j,1,n){if (a[i][j]!=ma[i][j]){printf("NO\n");return 0;}}
    printf("YES\n");


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值