Codeforces Round #270 D 最小生成树(并查集)+DFS



链接:戳这里


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.


题意:

给出n*n的矩阵,aij表示一棵树上的节点i与j之间的距离为aij,问通过这些节点直接的距离这是否能组成一棵树


思路:

首先aij=0 && i=j || aij=aji 是肯定的,且边的权值>0

先将边排序然后搞出最小生成树

DFS每个节点作为根节点,判断根到各个节点之间的距离是否符合aij


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n;
ll a[2020][2020];
int fa[2020];
int Find(int x){
    if(x!=fa[x])
        fa[x]=Find(fa[x]);
    return fa[x];
}
struct edge{
    int u,v;
    ll w;
    edge(int u=0,int v=0,ll w=0):u(u),v(v),w(w){}
    bool operator < (const edge &a)const{
        return w<a.w;
    }
}e[4000100];
struct node{
    int v,next;
    ll w;
}E[8000100];
int head[4000100],cnt=0;
void Add(int u,int v,int w){
    E[cnt].v=v;
    E[cnt].w=w;
    E[cnt].next=head[u];
    head[u]=cnt++;
}
int tot=0,flag;
ll val;
void DFS(int st,int u,int fa){
    for(int i=head[u];i!=-1;i=E[i].next){
        int v=E[i].v;
        if(v==fa) continue;
        val+=E[i].w;
        if(a[st][v]!=val || a[st][v]==0) {
            ///cout<<st<<" "<<v<<endl;
            flag=1;
            return ;
        }
        DFS(st,v,u);
        val-=E[i].w;
    }
}
int main(){
    mst(head,-1);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            scanf("%I64d",&a[i][j]);
            if(i==j && a[i][j]){
                cout<<"NO"<<endl;
                return 0;
            }
        }
        fa[i]=i;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j]!=a[j][i]){
                cout<<"NO"<<endl;
                return 0;
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            e[tot++]=edge(i,j,a[i][j]);
        }
    }
    sort(e,e+tot);
    for(int i=0;i<tot;i++){
        int X=Find(e[i].u);
        int Y=Find(e[i].v);
        if(fa[X]!=fa[Y]){
            fa[X]=Y;
            Add(e[i].u,e[i].v,e[i].w);
            Add(e[i].v,e[i].u,e[i].w);
        }
    }
    flag=0;
    for(int i=1;i<=n;i++){
        val=0;
        DFS(i,i,0);
        if(flag) {
            cout<<"NO"<<endl;
            return 0;
        }
    }
    cout<<"YES"<<endl;
    return 0;
}
/*
3
0 2 7
2 0 10
7 10 0

5
0 2 1 4 5
2 0 3 6 7
1 3 0 3 4
4 6 3 0 8
5 7 4 8 0
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值