【CODEFORCES】 D. Design Tutorial: Inverse the Problem

原创 2015年07月11日 09:29:53

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;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

【codeforces】 472D Design Tutorial: Inverse the Problem

Let's think it in the following way: for the minimal length edge, it must belong the the tree, ..., ...

Codeforces 472D Design Tutorial: Inverse the Problem【MST+SPFA+思维】

D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test ...

codeforces472D Design Tutorial: Inverse the Problem 最小生成树+dfs

题意:

Codeforces 472D. Design Tutorial: Inverse the Problem

D. Design Tutorial: Inverse the Problem time limit per test2 seconds memory limit per test256 mega...
  • nare123
  • nare123
  • 2016年08月16日 08:39
  • 282

Codeforces Round #270 G. Design Tutorial: Increase the Constraints 分块+FFT

我们如果要求两个串的哈密顿距离我们可以这样把第二个串反向,然后字母为1的地方系数为1,字母为0的系数为-1,所以我们可以算出x-y=a[n-1](x是相同的点数,y是不同的点数)有因为x+y=n所以y...

【CODEFORCES】 B. Design Tutorial: Learn from Life

B. Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 2...
  • S_Black
  • S_Black
  • 2015年07月09日 11:31
  • 381

【CODEFORCES】 C. Design Tutorial: Make It Nondeterministic

C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit p...
  • S_Black
  • S_Black
  • 2015年07月09日 11:34
  • 547

Codeforces 472C - Design Tutorial: Make It Nondeterministic(模拟)

题意 每个人可以选择first name 或者 last name 当自己的账号,现在给出最终的字典序排位,问能不能实现 思路 模拟一下。 lst用于记录上一个人选择的...

【codeforces】 472C Design Tutorial: Make It Nondeterministic

简单贪心。。。。

Codeforces Round #270——C. Design Tutorial: Make It Nondeterministic

C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit per t...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【CODEFORCES】 D. Design Tutorial: Inverse the Problem
举报原因:
原因补充:

(最多只允许输入30个字)