Codeforces round #270 d design Tutorial: inverse the problem -- MST + DFS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.