Question: Give a distance matrix and ask if it is a correct weighted tree.
Solution: first create a minimum spanning tree based on the distance matrix, because the given distance is the shortest distance between vertices, and then run DFS on each vertex to obtain the expected dis [] [], then compare the consistency between DIS and the original MP.
First, you must judge something. For more information, see the code.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <string>#include <vector>using namespace std;#define N 2007struct Edge{ int u,v,w;}edge[N*N];int a[N][N],fa[N],dis[N][N];vector<pair<int,int> > G[N];int cmp(Edge ka,Edge kb) { return ka.w < kb.w; }int findset(int x){ if(x != fa[x]) fa[x] = findset(fa[x]); return fa[x];}void dfs(int u,int fa,int ori){ for(int i=0;i<G[u].size();i++) { int v = G[u][i].first; if(v == fa) continue; dis[ori][v] = dis[ori][u] + G[u][i].second; dfs(v,u,ori); }}int main(){ int n,i,j; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%I64d",&a[i][j]); int tag = 1; for(i=1;i<=n;i++) { fa[i] = i; for(j=1;j<=n;j++) { if((i == j && a[i][j] != 0)||(i != j && a[i][j] == 0)||(a[i][j] != a[j][i])) { tag = 0; break; } } if(!tag) break; } if(!tag) { puts("NO"); continue; } int tot = 0; for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) edge[tot].u = i, edge[tot].v = j,edge[tot++].w = a[i][j]; sort(edge,edge+tot,cmp); for(i=0;i<tot;i++) { int u = edge[i].u, v = edge[i].v, w = edge[i].w; int fx = findset(u), fy = findset(v); if(fx != fy) { G[u].push_back(make_pair(v,w)); G[v].push_back(make_pair(u,w)); fa[fx] = fy; } } for(i=1;i<=n;i++) { dis[i][i] = 0; dfs(i,0,i); for(j=1;j<=n;j++) { if(a[i][j] != dis[i][j]) { tag = 0; break; } } if(!tag) break; } if(!tag) puts("NO"); else puts("YES"); } return 0;}
View code
Codeforces round #270 d design Tutorial: inverse the problem -- MST + DFS