题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树,
使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等!

思路:我们将给定的矩阵看成是一个图,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的更多相关文章

  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. cf472D Design Tutorial: Inverse the Problem

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

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

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

  4. Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS

    题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...

  5. Design Tutorial: Inverse the Problem

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

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

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

  7. codeforces B. Design Tutorial: Learn from Life

    题意:有一个电梯,每一个人都想乘电梯到达自己想要到达的楼层!从a层到b层的时间是|a-b|, 乘客上下电梯的时间忽略不计!问最少需要多少的时间....     这是一道神题啊,自己的思路不知不觉的就按 ...

  8. codeforces C. Design Tutorial: Make It Nondeterministic

    题意:每一个人 都有frist name 和 last name! 从每一个人的名字中任意选择 first name 或者 last name 作为这个人的编号!通过对编号的排序,得到每一个人 最终顺 ...

  9. 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\ ...

随机推荐

  1. 对TCP说三道四

    夜朦胧,人方静,无聊的人打开了无聊的电脑看到了一张无聊的图,想着想着就睡着了,梦到了人a和人b的一次聊天.        有一天,a有事情想跟b商量就问b"有时间么,想和你聊一下天" ...

  2. ooj1057: M的整数倍DP

    http://121.249.217.157/JudgeOnline/problem.php?id=1057 1057: M的整数倍 时间限制: 1 Sec  内存限制: 64 MB提交: 130   ...

  3. "Cannot find one of more components. Please reinstall the application"--安装VS2013之后不能正常打开的处理办法

    今天,安装完VS2013之后,不能正常启动.总提示一个让人摸不到头脑的错误: "Cannot find one of more components. Please reinstall th ...

  4. 测试markdown发布

  5. IOS开发之不同版本适配问题2(#ifdef __IPHONE_7_0)

    继续说说ios不同版本之间的适配 先说一个东西:在xcode当中有一个东西叫targets,苹果的官方文档是这样说的: A target specifies a product to build an ...

  6. Python中import的使用

    python中的import语句是用来导入模块的,在python模块库中有着大量的模块可供使用,要想使用这些文件需要用import语句把指定模块导入到当前程序中. import语句的作用 import ...

  7. NCPC 2013: Dance Reconstruction

    题目大意 对一个初始矩阵进行置换操作,已知经K次置换后得到的矩阵为,求一组可能的. 样例解释 这里只选取第二组样例进行解释. 4 2 3 4 1 2 2 3 4 1 初始矩阵为,根据Sample Ou ...

  8. javascript一种新的对象创建方式-Object.create()

    1.Object.create() 是什么? Object.create(proto [, propertiesObject ]) 是E5中提出的一种新的对象创建方式,第一个参数是要继承的原型,如果不 ...

  9. Android开发(二十五)——Android上传文件至七牛

    设置头像: Drawable drawable = new BitmapDrawable(dBitmap); //Drawable drawable = Drawable.createFromPath ...

  10. SecureCRT的快捷键

    快捷键,有时比笨拙的方式,要效率高很多,近期经常和Linux打交道,用到SecureCRT,这里就从网上找到部分快捷方式,作为日后查看,以防经常查找. Alt + Enter -- 全屏Alt + B ...