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\ ...
随机推荐
- git stash恢复
今天下午在使用Git命令进行代码管理时,因为自己一时疏忽直接把自己一天的劳动成果给弄丢了,这还了得,吓死宝宝了.真的,相信有代码丢失的朋友肯定能体会我当时的心情,不能体会我心情的那就祝你们也丢次代码, ...
- 转:Directshow开发的一些例子
DirectShow Filter 开发典型例子分析 --字幕叠加 (FilterTitleOverlay)1 本文分析一下<DirectShow开发指南>中的一个典型的Transform ...
- RTL8710 ROM 符号表 函数,常量,变量
函数 Name Address Ordinal ---- ------- ------- __vectors_table Reset_Handler NMI_Handler HardFault_Han ...
- Apache Solr查询语法(转)
查询参数 常用: q - 查询字符串,必须的. fl - 指定返回那些字段内容,用逗号或空格分隔多个. start - 返回第一条记录在完整找到结果中的偏移位置,0开始,一般分页用. rows - 指 ...
- Oracle的FRA(Flash Recovery Area)的好处
如果FRA的空间耗尽,只会影响到这个Oracle实例自身.所以不会耗尽所有磁盘空间从而影响到其它的数据库实例或其它应用.
- webRTC-实时流媒体的福音
WebRTC是一项在浏览器内部进行实时视频和音频通信的技术,是谷歌2010年以6820万美元收购Global IP Solutions公司而获得的一项技术.[1] WebRTC实现了基于网页的视频会议 ...
- win8 中使用第三方无线网卡出现无线连接受限解决办法
无线路由 无线网络模式基本设置 模式改为 11bg mixed , 然后在 win8 的设备管理器中找到无线路由 不知道 win8 有些地方兼容性做的不是很到位,我的 xp不做任何配置可以正常使用.
- Java web实时进度条整个系统共用(如java上传进度条、导入excel进度条等)
先上图: 这上文件上传的: 这是数据实时处理的: 1:先说说什么是进度条:进度条即计算机在处理任务时,实时的,以图片形式显示处理任务的速度,完成度,剩余未完成任务量的大小,和可能需要处理时间,显示方式 ...
- centos7 Linux 安装mysql
安装过程如: http://blog.csdn.net/tengdazhang770960436/article/details/18361979 中间过程出现无法安装mysql-server 解决方 ...
- console 让 js 调试更简单
浏览器的控制台(console)是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 显示信息 console.log('hello world'); console.debug('debug' ...