【CODEFORCES】 D. Design Tutorial: Inverse the Problem

本文探讨如何通过给定的节点间距离矩阵,判断其是否能够形成一个加权树结构。重点在于理解逆向问题解决策略,并通过构建最小生成树来验证距离矩阵的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.


题解:这一题题意都好看懂,就是给你各个点的距离,问你这可不可能是一棵树。这样我们每次取最小的边,加入已生成的树中,会发现这就是一个最小生成树问题。所以我们只需要根据这个图构造一棵最小生成树。最后在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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值