题意:给出一个距离矩阵,问是不是一颗正确的带权树。

解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否一致即可。

首先还要判断一些东西。具体看代码吧。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
#define N 2007 struct 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=;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=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%I64d",&a[i][j]);
int tag = ;
for(i=;i<=n;i++)
{
fa[i] = i;
for(j=;j<=n;j++)
{
if((i == j && a[i][j] != )||(i != j && a[i][j] == )||(a[i][j] != a[j][i]))
{
tag = ;
break;
}
}
if(!tag) break;
}
if(!tag) { puts("NO"); continue; }
int tot = ;
for(i=;i<=n;i++)
for(j=i+;j<=n;j++)
edge[tot].u = i, edge[tot].v = j,edge[tot++].w = a[i][j];
sort(edge,edge+tot,cmp);
for(i=;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=;i<=n;i++)
{
dis[i][i] = ;
dfs(i,,i);
for(j=;j<=n;j++)
{
if(a[i][j] != dis[i][j])
{
tag = ;
break;
}
}
if(!tag) break;
}
if(!tag)
puts("NO");
else
puts("YES");
}
return ;
}

Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS的更多相关文章

  1. Codeforces #270 D. Design Tutorial: Inverse the Problem

    http://codeforces.com/contest/472/problem/D D. Design Tutorial: Inverse the Problem time limit per t ...

  2. codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)

    题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...

  3. Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  4. cf472D Design Tutorial: Inverse the Problem

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

  5. D. Design Tutorial: Inverse the Problem 解析含快速解法(MST、LCA、思維)

    Codeforce 472D Design Tutorial: Inverse the Problem 解析含快速解法(MST.LCA.思維) 今天我們來看看CF472D 題目連結 題目 給你一個\( ...

  6. Design Tutorial: Inverse the Problem

    Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...

  7. codeforces D. Design Tutorial: Inverse the Problem

    题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...

  8. 【CF】270D Design Tutorial: Inverse the Problem

    题意异常的简单.就是给定一个邻接矩阵,让你判定是否为树.算法1:O(n^3).思路就是找到树边,原理是LCA.判断树边的数目是否为n-1.39-th个数据T了,自己测试2000跑到4s.算法2:O(n ...

  9. Codeforces Round #270 A~D

    Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...

随机推荐

  1. [moka同学摘录]Yii2.0开发初学者必看

    想要了解更多YII,PHP方面内容,请关注本博客. 基础总结 1.修改默认控制器/方法 yii默认是site控制器,可以在web.php中设置$config中的'defaultRoute'='xxxx ...

  2. html5 大幅度地增加和改良input元素的种类

    增加和改良input元素 url类型.email类型.date类型.time类型.datetime类型.datetime-local类型.month类型.week类型.number类型.range类型 ...

  3. SAP中给当前指定的活动用户发系统信息的函数

    函数名:TH_POPUP 输入集团.当前在线用户.Message即可

  4. English Training Material - 05

    Could I leave a message? Language Checklist Telephoning (1) Introducing yourself Good morning, Arist ...

  5. 【原】xcode5&IOS7及以下版本免证书真机调试记录

    搞了有一段IOS开发了,之前一直在企业做,近阶段主要在公司做C++服务端开发,打算在空闲实现搞搞个人开发,为自己赚钱,IDP还没申请下来,所以先用此方法在越狱设备上先做一下app的免证书真机调试,先记 ...

  6. Scrum不是万能药,要在时机成熟时推行

    敏捷很火热,大家都在谈敏捷:但不是所有团队都适合敏捷! 需要等待时机,时机成熟了,才推! 什么时候算时机成熟呢? 我们的经验是需要两点: 一.团队有三名或以上的研发工程师 : 二. 团队内有一名合适的 ...

  7. nginx安装过程,报错处理:make[1]: *** [objs/addon/src/bson.o] Error 1

    nginx安装过程中,经常会有各种错误: 具体安装步骤这里不做说明,网上一搜大把: 主要分析安装过程中遇到的问题 在make编译的时候,若报如下错误: cc1: warnings being trea ...

  8. 如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题

    应用程序事件日志中: "C:\windows\system32\test.exe"的激活上下文生成失败.找不到从属程序集 Microsoft.VC80.MFC,processorA ...

  9. iptables & selinux

    iptables -F getenforce setenforce 0 可以临时关闭,但重启之后还是会变成原来的状态. vi /etc/sysconfig/selinux 把里边的一行改为 SELIN ...

  10. ISO9126软件质量模型

    ISO9126软件质量模型,是评价软件质量的国际标准.6个特性27个子特性组成. ISO/IEC9126软件质量模型是一种评价软件质量的通用模型,包括3个层次: 1.质量特性 2.质量子特性 3.度量 ...