Design Tutorial: Inverse the Problem cf 270

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.


给出一个矩阵,表示两个点之间的距离,判断这些点能否构成一颗树。。

一开始想到floyd,但是显然超时。。

后来看了题解才知道原来可以弄最小生成数搞。。好神奇。

根据矩阵构造最小生成数,矩阵上的所有点一定满足最小生成树距离的。所以每个点跑一遍就行了。

/***********************************************\
 |Author: YMC
 |Created Time: 2014/9/29 14:23:49
 |File Name: d.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
int n;
ll ma[2005][2005];
struct Edge{
    int u,v;
    ll dis;
    bool operator < (Edge a) const {
        return dis < a.dis;
    }
}edge[4000005];
int f[2005];
ll ds[2005];
bool vis[2005];
int tot;
vector <int> vv[2005];
void init(){
    srep(i,n) f[i] = i;
}
int find(int a){
    return f[a] = f[a] == a?a:find(f[a]);
}
void kru(){
    tot = 0;
    srep(i,n){
        for(int j=i+1;j<=n;++j){
            Edge tt;
            tt.u = i,tt.v = j,tt.dis = ma[i][j];
            edge[tot ++] = tt;
        }
    }
    sort(edge,edge + tot);
    init();
    int t = 1;
    for(int i=0;i<tot;++i){
        Edge e = edge[i];
        int aa = find(e.u);
        int bb = find(e.v);
        if(aa != bb) {
            f[aa] = bb;
            vv[e.u].push_back(e.v);
            vv[e.v].push_back(e.u);
            t ++;
            if(t == n) return ;
        }
    }
}
void dfs(int u,int fa,ll now){
    if(vis[u]) return ;
    vis[u] = true;
    for(int i=0;i<vv[u].size();++i){
        if(vv[u][i] != fa){
            ds[vv[u][i]] = now + ma[u][vv[u][i]];
            dfs(vv[u][i],u,ds[vv[u][i]]);
        }
    }
}
bool isok(){
    for(int i=1;i<=n;++i){
        memset(ds,0,sizeof(ds));
        memset(vis,0,sizeof(vis));
        dfs(i,-1,0);
        srep(j,n){
            if(ma[i][j] != ds[j]) {
                return false;
            }
        }
    }
    return true;
}
int main() {
	//freopen("input.txt","r",stdin); 
    scanf("%d",&n);
    tot = 0;
    bool fg = true;
    memset(ma,0,sizeof(ma));
    srep(i,n){
        srep(j,n){
            scanf("%I64d",&ma[i][j]);
            if(ma[j][i] != 0 && ma[i][j] != ma[j][i]) fg = false;
            if(i != j && ma[i][j] == 0) fg = false;
        }
        if(ma[i][i] != 0) fg = false;
    }
    if(!fg){
        puts("NO");
        return 0;
    }
    kru();
    if(isok()){
        puts("YES");
    } else {
        puts("NO");
    }
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值