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\ ...
随机推荐
- android定位GPS定位 代码实现
package com.lx.util; import android.content.Context; import android.content.SharedPreferences; imp ...
- Tomcat之web项目部署
Tomcat一般用于部署JavaWeb项目. 遇到的问题 Linux操作系统中,在tomcat中部署项目时,一般只需要把项目war包:demo.war放到webapps下,然后启动tomcat即可.这 ...
- js弹出放大图
<script type="text/javascript"> function openpic(url){ OpenWindow = window.open(&quo ...
- 环回接口(loopback interface)的新认识
背景 前些日子在IDC实验docker的时候,为了避免与公司网络冲突,将bridge设置为127.x网段的IP,原以为这样就OK,后来发现在访问container内部的服务的时候无法访问.开始以为ip ...
- Microsoft.VisualBasic.DateAndTime.Timer 与 DateTime.Now.TimeOfDay.TotalSeconds 相当
如题,示例如下: Console.WriteLine(DateTime.Now.TimeOfDay.TotalSeconds); Console.WriteLine(Microsoft.VisualB ...
- Android 使用java.net.socket 的接收问题
// 初始化socketsocket = new Socket(InetAddress.getByName(sip), sport);InputStream sin = socket.getInput ...
- icmp,tcp,traceroute,ping,iptables
有东莞的监控主机到北京BGP出问题了: 报警短信疯狂发送: 找东莞IDC和北京BGP服务商协查: 有个奇怪的问题:北京到东莞trcaceroute都有路由信息 东莞143段到北京全无路由信息:但,东莞 ...
- asp.net web 后台判断提示框,点击'是'执行代码A(),点击'否'执行代码B()
html code <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server&q ...
- 每日英语:The Secret About Online Ad Traffic: One-Third Is Bogus
Billions of dollars are flowing into online advertising. But marketers also are confronting an uncom ...
- fast db 学习
见 http://code.google.com/p/mmdbsolution/source/browse/trunk/+mmdbsolution+--username+SiliangDu1987%4 ...