cf Inverse the Problem (最小生成树+DFS)
题意:
N个点。N行N列d[i][j]。
d[i][j]:结点i到结点j的距离。
问这N个点是否可能是一棵树。是输出YES,否则输出NO。
思路:
假设这个完全图是由一棵树得来的,则我们对这个完全图求最小生成树,得到原树。(画个图就明白)
故我们对完全图求最小生成树,然后用DFS从这棵树上的每个点出发,判断距离是否和矩阵d相同。
注意:
用vector存与每个点相连树枝的另一端,否则超时。用了vector也耗了1400多秒,限时2s。
代码:
#include <cstdio>
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <stack>
using namespace std;
int const uu[4] = {1,-1,0,0};
int const vv[4] = {0,0,1,-1};
typedef long long ll;
int const maxn = 50005;
int const inf = 0x3f3f3f3f;
ll const INF = 0x7fffffffffffffffll;
double eps = 1e-10;
double pi = acos(-1.0);
#define rep(i,s,n) for(int i=(s);i<=(n);++i)
#define rep2(i,s,n) for(int i=(s);i>=(n);--i)
#define mem(v,n) memset(v,(n),sizeof(v))
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
struct node{
int x,y,len;
};
int n;
int d[2005][2005], a[2005][2005];
int fa[2005];
node edge[4000005];
vector<int> graph[2005]; bool cmp(node a,node b){
return a.len<b.len;
} int findFa(int x){
if(fa[x]!=x) fa[x]=findFa(fa[x]);
return fa[x];
} bool dfs(int start,int x,int fa,int weight){
if(d[start][x]!=weight){
return false;
}
int L=graph[x].size();
rep(i,0,L-1){
int v=graph[x][i];
if(v==fa) continue;
bool t=dfs(start,v,x,weight+a[x][v]);
if(!t) return false;
}
return true;
} int main(){
scanf("%d",&n);
rep(i,1,n) rep(j,1,n) scanf("%d",&d[i][j]); rep(i,1,n) if(d[i][i]!=0){
printf("NO\n");
return 0;
}
rep(i,1,n-1) rep(j,i+1,n){
if(d[i][j]==0 || (d[i][j]!=d[j][i])){
printf("NO\n");
return 0;
}
} int eNum=0;
mem(a,0); rep(i,1,n-1) rep(j,i+1,n){
edge[++eNum].x=i, edge[eNum].y=j;
edge[eNum].len=d[i][j];
}
sort(edge+1,edge+1+eNum,cmp);
rep(i,1,n) fa[i]=i;
rep(i,1,n) graph[i].clear(); rep(i,1,eNum){
int xx=edge[i].x, yy=edge[i].y;
int fx=findFa(xx), fy=findFa(yy);
if(fx!=fy){
fa[fx]=fy;
a[xx][yy]=a[yy][xx]=edge[i].len;
graph[xx].push_back(yy);
graph[yy].push_back(xx);
}
} int t=findFa(1);
rep(i,2,n) if(findFa(i)!=t){
printf("NO\n");
return 0;
} rep(i,1,n){
bool k=dfs(i,i,-1,0); //从顶点i出发
if(!k){
printf("NO\n");
return 0;
}
}
printf("YES\n");
}
cf Inverse the Problem (最小生成树+DFS)的更多相关文章
- Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...
- 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 題目連結 題目 給你一個\( ...
- CF 291E. Tree-String Problem [dfs kmp trie图优化]
CF291E 题意:一棵树,每条边上有一些字符,求目标串出现了多少次 直接求目标串的fail然后一边dfs一边跑kmp 然后就被特殊数据卡到\(O(n^2)\)了... 因为这样kmp复杂度分析的基础 ...
- 【CF】270D Design Tutorial: Inverse the Problem
题意异常的简单.就是给定一个邻接矩阵,让你判定是否为树.算法1:O(n^3).思路就是找到树边,原理是LCA.判断树边的数目是否为n-1.39-th个数据T了,自己测试2000跑到4s.算法2:O(n ...
- Design Tutorial: Inverse the Problem
Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...
- HDU 2489 Minimal Ratio Tree 最小生成树+DFS
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- CodeForces160D 最小生成树 + dfs
https://cn.vjudge.net/problem/26727/origin 题目大意: 给一个带权的无向图,保证没有自环和重边. 由于最小生成树不唯一,因此你需要确定每一条边是以下三种情况哪 ...
随机推荐
- 使用Visual Studio Code 开发 ESP8266
使用Visual Studio Code 开发 ESP8266 ESP8266+ArduinoIDE+VSCode开发ESP8266. 首先说明一下ESP8266并不是某一WiFi模块的名字(我以前是 ...
- Docker系列(27)- 容器互联--link
思考 思考一个场景,我们编写了一个微服务,database url=IP:,项目不重启,数据库ip换掉了,我们希望可以处理这个问题,可以使用名字来进行访问容器吗 实践 [root@localhost ...
- 🧚♂️全套Java教程_Java基础入门教程,零基础小白自学Java必备教程👨💻004 # 第四单元 流程控制语句上 #
一.本单元知识点概述 二.本单元目标 (Ⅰ)重点知识目标 1.if语句的格式及执行流程2.switch语句的格式及执行流程 (Ⅱ)能力目标 1.掌握if语句的格式及执行流程2.掌握switch语句的格 ...
- 案例分享 | dubbo 2.7.12 bug导致线上故障
本文已收录 https://github.com/lkxiaolou/lkxiaolou 欢迎star.搜索关注微信公众号"捉虫大师",后端技术分享,架构设计.性能优化.源码阅读. ...
- P4755-Beautiful Pair【笛卡尔树,线段树】
正题 题目链接:https://www.luogu.com.cn/problem/P4755 题目大意 \(n\)个数字的一个序列,求有多少个点对\(i,j\)满足\(a_i\times a_j\le ...
- sessionId在小程序中的妙用
前言:小程序发送短信验证码需要在后台储存生成的code,一次会话应放入session中,请求头部发送sessionId验证为同一session 1.页面一加载就从后台获取sessionId,储存在本地 ...
- 05-IdentityServer4
前面我们认识了jwt的token颁发模式,其中的应用场景和部分缺陷已经很是了解了.有些场合并不适合jwt,特别是针对第三方进行使用时,比如我们使用qq或者微信登陆博客园或其他第三方应用时. Ids4的 ...
- MySQL5.7主从复制-异步复制搭建
两台服务器,系统是Redhat6.5,MySQL版本是5.7.18.1.在主库上,创建复制使用的用户,并授予replication slave权限.这里创建用户repl,可以从IP为10.10.10 ...
- 试题 算法训练 最大最小公倍数 java题解
资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少. 输入格式 输入一个正整数N. 输出格式 输出一个整数 ...
- python 工具箱
strip() 方法可以从字符串去除不想要的空白符. print() BIF的file参数控制将数据发送/保存到哪里. finally组总会执行,而不论try/except语句中出现什么异常. 会向e ...