codeforces D. Design Tutorial: Inverse the Problem
题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树,
使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等!
思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径, 如果存在一棵树,那么
这个树中a->b的距离一定是这个图中所有a->b中路径长度最短的一条!所以我们根据边权,
建立一棵MST树!再将MST树中的任意两点之间的距离求出来,看是否和矩阵中的对应的节点
对距离相同!
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 2005
#define M 2000005
using namespace std; int mp[N][N];
int mpp[N][N];
int f[N];
int vis[N];
int n; struct node{
int v, dist;
node(){}
node(int v, int dist){
this->v = v;
this->dist = dist;
}
}; vector<node>tmp[N]; struct edge{
int x, y, d;
edge(int x, int y, int d){
this->x = x;
this->y = y;
this->d = d;
}
edge(){}
}; int cnt;
edge e[M]; bool cmp(edge a, edge b){
return a.d < b.d;
} int getFather(int x){
return x == f[x] ? x : f[x] = getFather(f[x]);
} bool _union(int x, int y){
int fx = getFather(x), fy = getFather(y);
if( fx != fy){
f[fx] = fy;
return true;
}
return false;
} void dfs(int u, int cur, int dist){
vis[u] = ;
int len = tmp[u].size();
for(int i = ; i<len; ++i){
int v = tmp[u][i].v;
if( !vis[v] ){
mpp[cur][v] = mpp[v][cur] = dist+tmp[u][i].dist;
dfs(v, cur, dist+tmp[u][i].dist);
}
}
} int main(){
scanf("%d", &n);
bool flag = true;
bool flag1 = false;
for(int i = ; i <= n; ++i){
f[i] = i;
for(int j = ; j <= n; ++j){
scanf("%d", &mp[i][j]);
if(j > i) e[cnt++] = edge(i, j, mp[i][j]);//将边存起来
if(i==j && mp[i][j] != ) flag = false;//是否自身到自身有权值
if( i!=j && !mp[i][j]) flag1 = true;//是否都是全零
}
}
if(!flag1 && flag){
sort(e, e+cnt, cmp);
for(int i=; i<cnt; ++i)
if( _union(e[i].x, e[i].y) )
tmp[e[i].x].push_back(node(e[i].y, e[i].d)), tmp[e[i].y].push_back(node(e[i].x, e[i].d)); for(int i=; flag && i<n; ++i){//求最小生成树中任意两个节点的距离
memset(vis, , sizeof(vis));
dfs(i, i, );
for(int j=i+; flag && j<=n; ++j)
if(!(mp[i][j] == mpp[i][j] && mp[i][j] == mp[j][i]))//如果最小生成树中的任意两点距离和给定的对应的两点之间的距离不相等
flag = false;
} if( flag ) printf("YES\n");
else printf("NO\n");
}
else printf("NO\n");
return ;
}
codeforces D. Design Tutorial: Inverse the Problem的更多相关文章
- 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 ...
- 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 題目連結 題目 給你一個\( ...
- Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...
- Design Tutorial: Inverse the Problem
Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...
- 【CF】270D Design Tutorial: Inverse the Problem
题意异常的简单.就是给定一个邻接矩阵,让你判定是否为树.算法1:O(n^3).思路就是找到树边,原理是LCA.判断树边的数目是否为n-1.39-th个数据T了,自己测试2000跑到4s.算法2:O(n ...
- codeforces B. Design Tutorial: Learn from Life
题意:有一个电梯,每一个人都想乘电梯到达自己想要到达的楼层!从a层到b层的时间是|a-b|, 乘客上下电梯的时间忽略不计!问最少需要多少的时间.... 这是一道神题啊,自己的思路不知不觉的就按 ...
- codeforces C. Design Tutorial: Make It Nondeterministic
题意:每一个人 都有frist name 和 last name! 从每一个人的名字中任意选择 first name 或者 last name 作为这个人的编号!通过对编号的排序,得到每一个人 最终顺 ...
- Codeforces.472F.Design Tutorial: Change the Goal(构造 线性基 高斯消元)
题目链接 \(Description\) 给定两个长为\(n\)的数组\(x_i,y_i\).每次你可以选定\(i,j\),令\(x_i=x_i\ \mathbb{xor}\ x_j\)(\(i,j\ ...
随机推荐
- Ruby on Rails框架开发学习
学习地址:http://www.ixueyun.com/lessons/detail-lessonId-685.html 一.课程概述 软件开发在经历了面向过程编程的阶段,现在正大行其道的是敏捷开发, ...
- iOS开发拓展篇——如何把项目托管到GitHub
iOS开发拓展篇——如何把项目托管到GitHub 说明:本文主要介绍如何把一个OC项目托管到Github,重操作轻理论. 第一步:先注册一个Github的账号,这是必须的 注册地址:Github官网注 ...
- win7给C盘扩容
win7给C盘扩容 需要给C盘在不重装系统的情况下进行扩容.在网上搜索了不少资料,包括使用系统自带的分区工具,PQ等软件,都没有成功.不小心看到了分区助手,使用之,迅速的解决了问题,而且解决的非常完美 ...
- Linux 磁带机备份完全攻略
一.确定数据备份策略 首先必须确定在备份过程中操作哪些文件.在商业环境中,这是非常困难的一个决定,而且会产生严重的影响.如果备份了太多数据,会导致备份系统的成本过于庞大,会削减其他方面的开支.如果没有 ...
- [C#] AY.WPF-图形编程-高中生为起点-研究报告1
=========================www.ayjs.net独家拥有,未经许可,不许转载,违者追究法律责任 简单的引入:点的平移与转换 System.Window.Point类的 Off ...
- “合规性”是考核IT运维的重要指标
ITSM的绩效考核向来是一个令人头疼的问题,有时就像一团乱麻,既无章可循,又无从下手.其实,只要掌握正确的思想方法,就能拨云见日.“斩乱麻”需“快刀”,“合规性考核”就是斩ITSM绩效考核这团乱麻的快 ...
- MT写的对URL操作的两个方法
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 可编辑的DIV -编辑器
找了好多,没几个好用的,都或多或少有问题 目前这个最好用.. 不过有一个奇葩的问题,就是要放在"<a></a>"标签里面, js或者jQuery获取 $ ...
- Linux设备驱动剖析之Input(四)
static void input_pass_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) ...
- PHP vs Python
最近在搞微信公众号方面的开发,发现很多开发微信公众号都使用PHP来开发,由于我之前开发Web端喜欢使用Python,所以从Quora网站找出一篇Which-is-better-PHP-or-Pytho ...