题意异常的简单。就是给定一个邻接矩阵,让你判定是否为树。
算法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. linux64下安装swftools

    在文档转换器中,需要在linux上安装swftools,经历了一番曲折过程终于安装成功.swftools安装包从http://www.swftools.org/download.html上面下载. 在 ...

  2. WGS84经纬度坐标与web墨卡托之间的转换【转】

    第一种方法: //经纬度转Web墨卡托 dvec3 CMathEngine::lonLat2WebMercator(dvec3 lonLat) { dvec3 mercator; ; ); ; mer ...

  3. 第二篇:杂项之图像处理pillow

    杂项之图像处理pillow   杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/ ...

  4. 码表 Unicode GBK UTF8 示例

    Unicode的编码形式与对应的字符串相互转换 /**  * Unicode的编码形式与对应的字符串相互转换  * @author 白乾涛  */ public class UnicodeUtils  ...

  5. X86(32位)与X64(64位)有什么区别,如何选择对应的操作系统和应用程序?

    X86就是我们一般用的32位的系统,指针长度为32位(386起):X64就是64位的系统,指针长度为64位. 选择硬件对应的软件,建议通过以下三条考虑:1.64位操作系统相对32位操作系统理论上性能会 ...

  6. TransactionScope简单用法

    记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...

  7. 在fragment中调用SharedPreferences

    [o] Activity中调用SharedPreferences的方式: String prefsName = "mysetting"; SharedPreferences pre ...

  8. HTML5教程:课时一HTML简介

    一.HTML5新特性 1.HTML5多媒体:标签:视频<video>  :音频<audio> 2.HTML5应用:  本地数据存储:访问本地文件: 本地SQL数据:缓存引用: ...

  9. Nhibernate总结(一)查询返回指定字段

    项目查询中,常常需要返回指定的字段,下面是三种Nhibernate的方法1.linq to Nhibernatepublic class NameID{ public int Id { get; se ...

  10. 菜鸟日记之JSP二 内置对象的理解

    ·最近学习JSP了,对编程和网络又有了一些理解.无论是现实中人与人的交流,还是网络世界的接触,都是在相互表达自己的意思让别人知道,并理解对方的信息.然后我们知道的事情不断的变多,会又交杂出新的内容,不 ...