最小生成树(kruskal)Codeforces 472D

D. Design Tutorial: Inverse the Problem

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.


题意:已知两点之间的最小距离,问能不能构造出一棵树

思路:求最小生成树,然后dfs求两点之间的最短距离,是否符合已知情况

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=2010;
int N,num;
int a[maxn][maxn],pra[maxn];
long long dis[maxn];
vector<pair<int,int> > g[maxn];
struct node
{
    int u,v,f;
    bool operator<(const node &a)const
    {
        return f<a.f;
    }
}edge[maxn*maxn];
void add_edge(int u,int v,int f)
{
    edge[num].u=u;
    edge[num].v=v;
    edge[num++].f=f;
}
void dfs(int u,int fa)
{
    int len=g[u].size();
    for(int i=0;i<len;i++)
    {
        int v=g[u][i].first;
        int w=g[u][i].second;
        if(v==fa)continue;
        dis[v]=dis[u]+w;
        dfs(v,u);
    }
}
int find(int x)
{
    if(x==pra[x])return x;
    return pra[x]=find(pra[x]);
}
void solve()
{
    num=0;
    for(int i=0;i<=N;i++)pra[i]=i;
    for(int i=1;i<=N;i++)
        for(int j=i+1;j<=N;j++)
            add_edge(i,j,a[i][j]);
    sort(edge,edge+num);
    for(int i=0;i<num;i++)
    {
        int u=edge[i].u,v=edge[i].v;
        int x=find(u),y=find(v);
        if(x!=y)
        {
            pra[x]=y;
            g[u].push_back(make_pair(v,edge[i].f));
            g[v].push_back(make_pair(u,edge[i].f));
        }
    }
    for(int i=1;i<=N;i++)
    {
        dis[i]=0;
        dfs(i,-1);
        for(int j=i+1;j<=N;j++)
            if(dis[j]!=a[i][j]){printf("NO\n");return;}
    }
    printf("YES\n");
}
int main()
{
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)scanf("%d",&a[i][j]);
    bool flag=true;
    for(int i=1;i<=N;i++)
        if(a[i][i]){flag=false;break;}
    for(int i=1;i<=N;i++)
    {
        for(int j=i+1;j<=N;j++)
            if(a[i][j]!=a[j][i]||a[i][j]==0)
            {
                flag=false;
                break;
            }
        if(!flag)break;
    }
    if(!flag){printf("NO\n");return 0;}
    solve();
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值