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).

题目大意:给你一个n*n的矩阵,表示一棵树上任意两点间的距离,问是否存在满足要求的树。

输入格式

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.

输出格式

If there exists such a tree, output "YES", otherwise output "NO"。

样例输入

#1

3
0 2 7
2 0 9
7 9 0 

#2

3
1 2 7
2 0 9
7 9 0

#3

3
0 2 2
7 0 9
7 9 0

#4

3
0 1 1
1 0 1
1 1 0

#5

2
0 0
0 0

样例输出

#1

YES

#2

NO

#3

NO

#4

NO

#5

NO

题解

设矩阵为a[i][j] ,如果存在满足要求的树,那么树上任意两点间的距离是唯一的,且任意两个不重合的点距离不为0,一个点到自己的距离必为0。即a[i][j]==a[j][i],a[i][i]==0,a[i][j]!=a[j][i](i!=j),这三个条件有一个不满足就输出“NO”。

排除以上三种情况后,用矩阵建图,求该图的最小生成树,在判断树上任意两点距离是否和矩阵相符。

设树上两点i,j的最近公共祖先为x,dis[i]表示i到根节点距离,那么i,j的距离为dis[i]+dis[j]-dis[x]*2。

#include <algorithm>
#include <cstdio> 
#define LL long long
struct node{
    int u,v;
    LL w;
}g[4000005];
struct note{
    int fa,nex,u;
    LL w;
}tree[4005];
int n,fa[2005],num,a[2005][2005];
int fir[2005],numt,f[2005][15],dep[2005];
LL dis[2005]; 
bool vis[2005];
bool cmp(node x,node y)
{
    return x.w<y.w;
}
void add(int x,int y,int z)
{
    g[++num].u=x;  g[num].v=y;  g[num].w=z;
    return;
}
void addtree(int x,int y,int z)
{
    tree[++numt].u=y;  tree[numt].nex=fir[x];  fir[x]=numt;
    tree[numt].fa=x;  tree[numt].w=z;
    return;
}
int find(int x)
{
    if (fa[x]==x) return x;
    return fa[x]=find(fa[x]); 
}
void Kruskal()
{
    int i,j,k,cnt=0,fu,fv;
    for (i=1;i<=n;i++)
      fa[i]=i;
    std::sort(g+1,g+num+1,cmp);
    for (i=1;i<=num;i++)
    {
        fu=find(g[i].u);  fv=find(g[i].v);
        if (fu!=fv)
        {
            fa[fv]=fu;
            addtree(g[i].u,g[i].v,g[i].w);
            addtree(g[i].v,g[i].u,g[i].w);
            cnt++;
            if (cnt==num-1) break;
        }
    }
    return;
}
void dfs(int u)
{
    int i,x,k;
    vis[u]=1;
    dep[u]=dep[f[u][0]]+1;
    for (i=1;i<=12;i++)
      f[u][i]=f[f[u][i-1]][i-1];
    for (k=fir[u];k;k=tree[k].nex)
      if (!vis[tree[k].u])
      {
          x=tree[k].u;
          dis[x]=dis[u]+tree[k].w;
          f[x][0]=u;
          dfs(x);
      }  
      return;
}
int lca(int x,int y)
{
    int i;
    if (dep[x]>dep[y])
    {
        int t=x;  x=y;  y=t;
    }
    for (i=12;i>=0;i--)
      if (dep[f[y][i]]>=dep[x])
        y=f[y][i];
    if (x==y) return x;
    for (i=12;i>=0;i--)
      if (f[x][i]!=f[y][i])
        x=f[x][i],y=f[y][i];
    return f[x][0];        
}
int main()
{
    int i,j,k;
    scanf("%d",&n);
    for (i=1;i<=n;i++)
      for (j=1;j<=n;j++)
        scanf("%d",&a[i][j]);
    for (i=1;i<=n;i++)
      if (a[i][i]!=0)
      {
          printf("NO");
          return 0;
      }    
    for (i=1;i<=n;i++)
      for (j=1;j<=n;j++)
        if (i!=j)
          if (a[i][j]!=a[j][i] || a[i][j]==0)
          {
              printf("NO");
              return 0;
          }  
    for (i=1;i<=n;i++)
      for (j=1;j<i;j++)
        add(i,j,a[i][j]);
    Kruskal();
    dfs(1);
    int x;
    for (i=1;i<=n;i++)
      for (j=1;j<i;j++)
      {
          x=lca(i,j);
          if ((dis[i]+dis[j]-dis[x]*2)!=a[i][j])
          {
              printf("NO");
              return 0;
        }
      }
    printf("YES");  
    return 0;          
}
原文地址:https://www.cnblogs.com/rabbit1103/p/8511122.html