题意异常的简单。就是给定一个邻接矩阵,让你判定是否为树。
算法1:O(n^3)。思路就是找到树边,原理是LCA。判断树边的数目是否为n-1。39-th个数据T了,自己测试2000跑到4s。
算法2:O(n^2)。思考由图如何得到树,显然MST是可行的。因此,题目变为直接找到MST。然后通过树边构建目标矩阵。判定矩阵是否相等。

 /* 472D */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
const int INF = 0x3f3f3f3f;
int M[maxn][maxn];
int T[maxn][maxn];
int dist[maxn];
int fa[maxn];
bool visit[maxn];
int n; void kruskal() {
memset(visit, false, sizeof(visit));
memset(dist, 0x3f, sizeof(dist));
int v, mn;
int i, j, k; dist[] = ;
for (i=; i<n; ++i) {
mn = INF;
for (j=; j<n; ++j) {
if (!visit[j] && dist[j]<mn) {
mn = dist[j];
v = j;
}
} assert(mn < INF); for (j=; j<n; ++j) {
if (visit[j]) {
T[j][v] = T[v][j] = T[j][fa[v]] + mn;
}
}
visit[v] = true; for (j=; j<n; ++j) {
if (M[v][j] < dist[j]) {
dist[j] = M[v][j];
fa[j] = v;
}
}
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif bool flag = true; scanf("%d", &n); rep(i, , n)
rep(j, , n)
scanf("%d", &M[i][j]); rep(i, , n) {
if (M[i][i] != ) {
flag = false;
goto _output;
}
rep(j, i+, n) {
if (M[i][j]!=M[j][i] || M[i][j]==) {
flag = false;
goto _output;
}
}
} kruskal(); rep(i, , n) {
rep(j, i+, n) {
if (M[i][j] != T[i][j]) {
flag = false;
goto _output;
}
}
} _output:
puts(flag ? "YES":"NO"); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】270D 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 D. Design Tutorial: Inverse the Problem

    题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...

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

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

  6. Design Tutorial: Inverse the Problem

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

  7. 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

    转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...

  8. 【数论】FOJ 2238 Daxia & Wzc's problem

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...

  9. 【Leetcode】355. Design Twitter

    题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...

随机推荐

  1. Linux文件查找命令find用法整理(locate/find)

    Linux文件查找查找主要包括:locate和find 1.locate 用法简单,根据数据库查找,非实时,用法: locate FILENAME 手动更新数据库(时间可能较长) updatedb 2 ...

  2. MAC OS X API知识摘抄

    本文为信息为网上各个地方收集整理Carbon和Cocoa,Toolbox,POSIX,JAVA并列成为Mac OS X五个主要的API.与Cocoa相较之下,Carbon是非物件导向(Procedur ...

  3. sulime-text 3 安装以及使用

    sublime-text 3 使用了有一段时间了,虽然不是很熟悉,但是已经爱上它了,但是对这个编辑器还是一知半解,所以现在记录下,希望以后能永久的使用这个编辑器. 一,调用 控制台 使用命令 ctrl ...

  4. mac下安装配置nginx环境

    本文介绍 nginx 在mac上的安装. 我是通过brewhome 来安装的. brew install nginx 一路顺畅. 下面是安装信息. 复制代码 代码如下: hematoMacBook-P ...

  5. winfrom面向对象1

    1:面向对象的技术概论 要学习好面向对象,我们应该从三个问题入手: 1.什么是面向对象? 2.为什么要面向对象? 3.该怎么面向对象? 对象的定义是人们要进行研究的任何事物,从最简单的整数到复杂的飞机 ...

  6. 一看就懂ReactJS

    现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领 ...

  7. 使用微信api接口开发的框架

    <?php/** * 微信公众平台API */class WeixinChat{ private $token; private $appid; private $appsecret; priv ...

  8. 将requirejs进行到底(一)

    随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在互联网开发模式,我们需要团队协作.模块复用.单元测试等等一系列复杂的需求 ...

  9. Mac OS X 好用的软件包管理工具 Homebrew

    github地址:https://github.com/Homebrew/homebrew 安装方法:http://brew.sh/

  10. C#中的Invoke

    在用.NET Framework框架的WinForm构建GUI程序界面时,如果要在控件的事件响应函数中改变控件的状态,例如:某个按钮上的文本原先叫“打开”,单击之后按钮上的文本显示“关闭”,初学者往往 ...