题解:CF472D

· · 题解

题解:CF472D

题目名称:Design Tutorial: Inverse the Problem

题目来源:CodeForces、洛谷

题目链接:https://www.luogu.com.cn/problem/CF472D

题目难度:\color{green}\text{普及+/提高-}

题目标签:最小生成树

Description

现在给定一个 n \times n 的距离矩阵,判断它是否是某棵加权树(所有权值都必须为正整数)的距离矩阵。

Limitations

Analysis

判断条件:

Code

::::success[代码]

// 核心:树是自身距离矩阵的MST。对矩阵求MST,再验证距离是否一致。
#include<bits/stdc++.h>
using namespace std;
/*====================*/
using lnt = long long;
/*====================*/
#define endl '\n'
/*====================*/
const int N = 2e3 + 10;
/*====================*/
const int INF = 1e9;
const lnt INFll = 1e18;
/*====================*/
lnt d[N][N]; int n;
/*====================*/
struct Edge
{
    int u, v; lnt w;
    /*====================*/
    Edge(void) : u(0), v(0), w(0ll) {}
    Edge(int u, int v, lnt w)
    {
        this->u = u, this->v = v, this->w = w;
    }
    /*====================*/
    bool operator< (const Edge& other) const 
    {
        return w < other.w;
    }
};
/*====================*/
int fa[N];
void Init(int n) 
{
    for (int i = 1; i <= n; ++i)
    {
        fa[i] = i;
    }
}
int Find(int x)
{
    return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
bool Merge(int x, int y)
{
    x = Find(x), y = Find(y);
    if (x == y) return false;
    fa[x] = y; return true;
}
/*====================*/
vector<pair<int, lnt>> graph[N];
/*====================*/
lnt Kruskal(int n, const vector<Edge>& G, int k = 1)
{
    vector<Edge> edge = G;
    sort(edge.begin(), edge.end());
    int cnt = n; lnt tot = 0;
    for (auto e : edge)
    {
        if (Merge(e.u, e.v))
        {
            graph[e.u].push_back( { e.v, e.w } );
            graph[e.v].push_back( { e.u, e.w } );
            tot += e.w; if (--cnt == k) return tot;
        }
    }
    return -INFll;
}
/*====================*/
void DFS(int cur, int pre, lnt dis, vector<lnt>& vec)
{
    vec[cur] = dis;
    for (auto e : graph[cur])
    {
        int nxt = e.first;
        lnt val = e.second;
        if (nxt == pre) continue;
        DFS(nxt, cur, dis + val, vec);
    }
}
/*====================*/
void Solve(void)
{
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            cin >> d[i][j];
        }
    }
    /*====================*/
    // Check 1 : 对角线为 0
    for (int i = 1; i <= n; ++i)
    {
        if (d[i][i] != 0)
        {
            cout << "NO" << endl;
            return ;
        }
    }
    // Check 2 : d[i][j] = d[j][i]
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            if (d[i][j] != d[j][i])
            {
                cout << "NO" << endl;
                return ;
            }
        }
    }
    // Check 3 : d[i][j] > 0
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            if (i == j) continue;
            if (!(d[i][j] > 0))
            {
                cout << "NO" << endl;
                return ;
            } 
        }
    }
    // Check 4 : n = 1
    if (n == 1)
    {
        cout << "YES" << endl;
        return ;
    }
    // Check 5 : Build a MST, and build another distance array, 
    // then check : for all [i, j], d[i][j] = another[i][j] is True or False
    vector<Edge> edges;
    for (int i = 1; i <= n; ++i)
    {
        for (int j = i + 1; j <= n; ++j)
        {
            edges.push_back( { i, j, d[i][j] } );
        }
    }
    Init(n); lnt totw = Kruskal(n, edges);
    if (totw == -INFll)
    {
        cout << "NO" << endl;
        return ;
    } 

    for (int i = 1; i <= n; ++i)
    {
        vector<lnt> dis(n + 1);
        DFS(i, -1, 0, dis);
        for (int j = 1; j <= n; ++j)
        {
            if (dis[j] != d[i][j])
            {
                cout << "NO" << endl; 
                return ;
            }
        }
    }
    cout << "YES" << endl;
}
/*====================*/
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif 
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int T = 1; //cin >> T;
    while (T--) Solve();
    return 0;
}

::::