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

解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑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. PHP学习笔记:利用时间和mt_rand函数获取随机名字

    这个知识会在文件上传等场合用到,还没学面向对象,现在用函数形式呈献给各位,代码都做了备注,有不懂得可以在线提问. <?php /** * Created by PhpStorm. * User: ...

  2. ahjesus根据身份证号码获取相关信息(生日,省市县,性别)

    使用说明: //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! var idCard = new IdCard();var msg = ...

  3. django使用笔记

    django的具体使用可以看官方手册http://djangobook.py3k.cn,这里主要记录使用django中遇到的问题. 1.中文编码问题. 因为我们用到的东西基本上都有中文,在settin ...

  4. 初次接触mootools

    以下是今天所学代码,网上有这篇博客可供参考,另外还是推荐官方文档 ,以下是今天所敲代码: //用mootools创建类的方式: //方式1:用标准方式传入一个对象字面量 /* var Person = ...

  5. Smartforms常见问题

    分类: 1.使用SFSY-FORMPAGES显示总页数的时候,如果页数大于9,将会在前10页显示成星号* 解决: 有时候这样做完之后,星号*是没有了,但是字体会有颠倒或者重叠的现象. 如果出了这个问题 ...

  6. DevExpress.XtraGrid.Views 设置指定行的背景颜色 .

    如需要将指定行的背景设置颜色,可参考以下示例 1.事件:CustomDrawCell 2.示例: private void gridView1_CustomDrawCell(object sender ...

  7. NPOI Excel类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using NPOI.HSSF.Us ...

  8. 转:C# 使用NLog记录日志

    原文:http://www.cnblogs.com/felixnet/p/5498759.html NLog是一个记录日志组件,和log4net一样被广泛使用,它可以将日志保存到文本文件.CSV.控制 ...

  9. android 保存 用户名和密码 设置等应用信息优化

    1.传统的保存用户名,密码方式 SharedPreferences Editor editor = shareReference.edit(); editor.putString(KEY_NAME,& ...

  10. IOS 杂笔-5(NSTimer极浅析)

    1.timer都会对它的target进行retain,我们需要小心对待这个target的生命周期问题,尤其是重复性的timer. 2. timer不是一种实时的机制,会存在延迟,而且延迟的程度跟当前线 ...