codeforces D. Design Tutorial: Inverse the Problem
题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树,
使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等!
思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径, 如果存在一棵树,那么
这个树中a->b的距离一定是这个图中所有a->b中路径长度最短的一条!所以我们根据边权,
建立一棵MST树!再将MST树中的任意两点之间的距离求出来,看是否和矩阵中的对应的节点
对距离相同!
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<vector> 5 #include<algorithm> 6 #define N 2005 7 #define M 2000005 8 using namespace std; 9 10 int mp[N][N]; 11 int mpp[N][N]; 12 int f[N]; 13 int vis[N]; 14 int n; 15 16 struct node{ 17 int v, dist; 18 node(){} 19 node(int v, int dist){ 20 this->v = v; 21 this->dist = dist; 22 } 23 }; 24 25 vector<node>tmp[N]; 26 27 struct edge{ 28 int x, y, d; 29 edge(int x, int y, int d){ 30 this->x = x; 31 this->y = y; 32 this->d = d; 33 } 34 edge(){} 35 }; 36 37 int cnt; 38 edge e[M]; 39 40 bool cmp(edge a, edge b){ 41 return a.d < b.d; 42 } 43 44 int getFather(int x){ 45 return x == f[x] ? x : f[x] = getFather(f[x]); 46 } 47 48 bool _union(int x, int y){ 49 int fx = getFather(x), fy = getFather(y); 50 if( fx != fy){ 51 f[fx] = fy; 52 return true; 53 } 54 return false; 55 } 56 57 void dfs(int u, int cur, int dist){ 58 vis[u] = 1; 59 int len = tmp[u].size(); 60 for(int i = 0; i<len; ++i){ 61 int v = tmp[u][i].v; 62 if( !vis[v] ){ 63 mpp[cur][v] = mpp[v][cur] = dist+tmp[u][i].dist; 64 dfs(v, cur, dist+tmp[u][i].dist); 65 } 66 } 67 } 68 69 int main(){ 70 scanf("%d", &n); 71 bool flag = true; 72 bool flag1 = false; 73 for(int i = 1; i <= n; ++i){ 74 f[i] = i; 75 for(int j = 1; j <= n; ++j){ 76 scanf("%d", &mp[i][j]); 77 if(j > i) e[cnt++] = edge(i, j, mp[i][j]);//将边存起来 78 if(i==j && mp[i][j] != 0) flag = false;//是否自身到自身有权值 79 if( i!=j && !mp[i][j]) flag1 = true;//是否都是全零 80 } 81 } 82 if(!flag1 && flag){ 83 sort(e, e+cnt, cmp); 84 for(int i=0; i<cnt; ++i) 85 if( _union(e[i].x, e[i].y) ) 86 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)); 87 88 for(int i=1; flag && i<n; ++i){//求最小生成树中任意两个节点的距离 89 memset(vis, 0, sizeof(vis)); 90 dfs(i, i, 0); 91 for(int j=i+1; flag && j<=n; ++j) 92 if(!(mp[i][j] == mpp[i][j] && mp[i][j] == mp[j][i]))//如果最小生成树中的任意两点距离和给定的对应的两点之间的距离不相等 93 flag = false; 94 } 95 96 if( flag ) printf("YES\n"); 97 else printf("NO\n"); 98 } 99 else printf("NO\n"); 100 return 0; 101 }
View Code
转载于:https://www.cnblogs.com/hujunzheng/p/4001152.html
codeforces D. Design Tutorial: Inverse the Problem相关推荐
- 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 798C:Mike and gcd problem
Codeforces 798C:Mike and gcd problem 题目链接:http://codeforces.com/contest/798/problem/C 题目大意:给出一个大小为$n ...
- Codeforces - Ehab and a component choosing problem
题目链接:Codeforces - Ehab and a component choosing problem 显然 Σa / k,相当于取平均值,我们得到最大值肯定是k=1的时候. 求最大值树形dp ...
- 【CodeForces - 472A】Design Tutorial: Learn from Math (tricks,思维,数论,打表)
题干: One way to create a task is to learn from math. You can generate some random math statement or m ...
- 每日一佳——Computational Rationalization: The Inverse Equilibrium Problem(Kevin Waugh et al. ,ICML ,2011)
PDF 这篇是2011年ICML的最佳论文. 题目意思:计算合理化:反均衡问题 摘要: Modeling the purposeful behavior of imperfect agents fro ...
- Codeforces 742B B. Arpa’s obvious problem and Mehrdad’s terrible solution
B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...
- codeforce A. Design Tutorial: Learn from Math
题意:将一个数拆成两个合数的和, 输出这两个数!(这道题做的真是TMD水啊)开始的时候不知道composite numbers是啥意思,看了3遍才看懂.... 看懂之后又想用素数筛选法来做,后来决定单 ...
- 【Codeforces - 798C】 Mike and gcd problem(思维,贪心)
题干: Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, .. ...
最新文章
- jquery怎么获取radio的值
- 使用boostrap组件结合PageHelper完成javaweb网页的分页功能
- python编写Mysql自动备份脚本
- CentOS Linux下VNC Server远程桌面配置详解
- 区块链BaaS云服务(21)腾讯CCGP跨链平台“系统架构”
- 权限操作-springSecurity快速入门
- 无法定位程序输入点 except_软件测试中的功能测试点(三)
- (18) Node.js npm包管理工具
- 计算机控制技术摘要,计算机控制技术摘要.ppt
- PureMVC--一款多平台MVC框架
- Java2十大经典中文图书
- android apk 可以直接放在systemapp下吗,内置语音apk到/system/app下的问题
- Pointer is missing a nullability type specifier (__nonnull or __nullable)
- 手把手教你使用Python做数据分析
- yolo算法python代码_深度学习目标检测系列:一文弄懂YOLO算法|附Python源码
- 工厂模式(包含3种工厂)
- 国产系统deepin。为什么要国产化?国产化意味着什么?(含Deepin系统部分问题解决)
- 面试:Android应用的崩溃率
- JS-表格行的动态删除和添加(insertRow deleteRow)
- 新春送祝福,直接发红包。现金红包等你来拿~
热门文章
- 以太坊白皮书_以太坊发展历程
- 调整selinux状态为disabled
- Jmeter 生成HTML性能测试报告
- gitlab访问慢,出现502,特别卡,耗内存cpu解决办法
- MyBatis-Plus_Condition作用
- java 发送16进制数据'_java 16进制数据递增
- C/C++ atol函数- C语言零基础入门教程
- php sql查询占位符,使用命名占位符时PHP / SQL插入错误
- mysql 建表时建立索引_mysql 分享建表和索引的几点规范
- 远程桌面服务器office版本,在启用远程桌面服务的计算机上部署 Office 2010