Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
题意:给出一个距离矩阵,问是不是一颗正确的带权树。
解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑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的更多相关文章
- 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 ...
- codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)
题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...
- 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 ...
- cf472D Design Tutorial: Inverse the Problem
D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test 256 mega ...
- D. Design Tutorial: Inverse the Problem 解析含快速解法(MST、LCA、思維)
Codeforce 472D Design Tutorial: Inverse the Problem 解析含快速解法(MST.LCA.思維) 今天我們來看看CF472D 題目連結 題目 給你一個\( ...
- Design Tutorial: Inverse the Problem
Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...
- codeforces D. Design Tutorial: Inverse the Problem
题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...
- 【CF】270D Design Tutorial: Inverse the Problem
题意异常的简单.就是给定一个邻接矩阵,让你判定是否为树.算法1:O(n^3).思路就是找到树边,原理是LCA.判断树边的数目是否为n-1.39-th个数据T了,自己测试2000跑到4s.算法2:O(n ...
- Codeforces Round #270 A~D
Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...
随机推荐
- 一、MyBatis简介与配置MyBatis+Spring+MySql
//备注:该博客引自:http://limingnihao.iteye.com/blog/106076 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架 ...
- pbfunc外部函数扩展应用-在Powerbuilder中进行Http的GET、POST操作
利用PBFunc扩展函数进行Http的操作时,需要对n_pbfunc_http的以下几个函数进行参数设置: of_set_URL(...)//要进行GET或POST的url,必须 of_set_Con ...
- Ahjesus获取自定义属性Attribute或属性的名称
1:设置自己的自定义属性 public class NameAttribute:Attribute { private string _description; public NameAttribut ...
- Python可变参数
#!/usr/bin/env python # -*- coding: utf-8 -*- import math def calc(*numbers): sum=0 for n in numbers ...
- 写给java程序员的c++与java实现的一些重要细微差别
0.其实常规的逻辑判断结构.工具类.文件读写.控制台读写这些的关系都不大,熟悉之后,这些都是灵活运用的问题. 学习c/c++需要预先知道的一个前提就是,虽然有ANSI C标准,但是每个c/c++编译器 ...
- 我所了解的WEB开发 (1)
开始接触网站开发的时候,概念里就对静态网站和动态网站有了简单的区分,静态网站仅仅是纯粹的HTML网页,动态网站是需要采用asp 连接数据库(比如access).那个时候听说高手都是使用 Notepad ...
- Play Framework介绍:控制器层
业务逻辑代码通常位于模型(model)层.客户端(比如浏览器)无法直接调用其中的代码,所以模型对象提供的功能,必须作为资源以URI方式暴露给外部. 客户端使用HTTP协议来操作这些资源,从而调用了内部 ...
- 安装SQL Server Management Studio Express错误码是29506
解决方法:1:新建一个记事本,输入msiexec /i path\SQLServer2005_SSMSEE.msi 然后另存为.cmd格式.2:右单击刚刚创建的那个.CMD文件,选择“以管理员身份运行 ...
- adb 的相关操作及遇到的问题
一. 电脑连接你的手机 1.打开手机的开发者模式 并 开启调试工具 2.用手机连接上 你的电脑 3.在eclipse 中的DDMS 中查看是否 连接到 如图所示: 这时会发现 多了一个连接 :说明 ...
- 数据库性能调优——sql语句优化(转载及整理) —— 篇2
下面是在网上搜集的一些个人认为比较正确的调优方案,如有错误望指出,定虚心改正 (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中 ...