【CF】270D Design Tutorial: Inverse the Problem
题意异常的简单。就是给定一个邻接矩阵,让你判定是否为树。
算法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的更多相关文章
- 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 D. Design Tutorial: Inverse the Problem
题意:给定一个矩阵,表示每两个节点之间的权值距离,问是否可以对应生成一棵树, 使得这棵树中的任意两点之间的距离和矩阵中的对应两点的距离相等! 思路:我们将给定的矩阵看成是一个图,a 到 b会有多条路径 ...
- 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 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...
- 【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏
转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Andro ...
- 【数论】FOJ 2238 Daxia & Wzc's problem
题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
随机推荐
- 新建oracle数据库表空间
1.新建表空间,注意是数据表空间,不是临时表空间 create tablespace CARD logging datafile 'C:\app\tablespace\CARD.DBF' //注 ...
- burp
http://www.2cto.com/Article/201406/310929.html
- Jquery Ajax方法传递json到action
ajax向后台传入json需要设置option,如下 contentType:'application/json' data:Json.Stringify(jsObj) 后台处理复杂json对象(不知 ...
- navicat 数据库管理工具快捷键
最近在使用navicat 管理数据库中,因为经常要写一些sql的,但是每次都要鼠标点击运行,感觉很不爽,于是找到navicat(以下) 快捷键(最常用的): ctrl + q 打开查询窗口 ctrl ...
- 前后端分离--构建前端Mock Server--windows部署rap
mock:模拟的,虚假的 mock server:模拟服务,模拟请求,模拟虚假数据 为了前后端更好的分工,接口文档是必须的,前后端都根据接口文档写代码,然后对接接口就行了. 但是,后端跟不上前端节奏, ...
- (转)spring ioc原理(看完后大家可以自己写一个spring)
最近,买了本Spring入门书:spring In Action .大致浏览了下感觉还不错.就是入门了点.Manning的书还是不错的,我虽然不像哪些只看Manning书的人那样专注于Manning, ...
- PS软件之,快速的修改图片你的尺寸
进入 -- 图像 --- 图像尺寸 -- (前面两个去掉后,只剩下最后一个选项的时候就能够任意的修改图像的尺寸)
- Android - 向服务器发送数据(GET).
在此,使用HTTP协议,通过GET请求,向服务器发送请求,这种方式适合于数据量小,数据安全性要求不高的情况下. 一,服务器端,使用Servlet. 在服务器端,定义一个HttpServlet的子类,以 ...
- 修改tomcat默认的端口号
协同管理系统黙认使用Tomcat默认的端口8080,除8080端口外Tomcat还会占用8005,8009和8443端口.如果这4个端口已被占用,可以将协同管理系统修改为使用其它端口. 修改方法如下: ...
- HTML5 程序设计笔记(二)
Canvas API 1.HTML5 Canvas 概述 1.1 历史 Canvas的概念最初是由苹果公司提出的,用于在Mac OS X WebKit中创建控制板部件(dashboard widget ...