【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 ...
随机推荐
- Sqlserver中实现oralce 数据库的rownumber
引用自:http://cai555.javaeye.com/blog/466033 方法1: with temp as ( select row_number() over(order by city ...
- UTF-8 BOM对PHP的影响
今天在用notepad++写代码时 载入一个frameset框架模版后 在页面上一直不显示该页面,查看源码后都正常.然后索性把里面东西全删掉 随便写了几个测试文字可以正常显示. 折腾了好长时间,最后偶 ...
- Java实现微信菜单json字符串拼接
Java实现微信菜单json字符串拼接 微信菜单拼接json字符串方法 >>>>>>>>>>>>>>>> ...
- springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)
springMvc中406错误解决, springMvc使用json出现406 (Not Acceptable) >>>>>>>>>>> ...
- webapi 接口规则
[HttpPost] [AuthorizeFilter] public HttpResponseMessage DeleteStudentInfo([FromBody] object value) { ...
- [页面辅助] 最新的 PageValidate 类 (转载)
代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Text. ...
- SQL Prompt Snippet Manager 妙用
SQL Prompt有一个很好用的工具叫Snippet Manager,SQL脚本片段管理器. 使用它可以快速的键入一段脚本,如输入ii+Tab,即可变成INSERT INTO 同理,我们可以定义一些 ...
- 如何获得Windows 8中已记住的WIFI的明文密码
网上很流行的一种查看WIFI密码明文的方法,如下: 今天遇到了一种状况,就是如果不连WIFI的情况我能抓到这个密码吗?(实在不想开口问同事密码多少,只能苦逼的自己想办法了o(︶︿︶)o ) 答案当然是 ...
- CI 笔记 datagrid的调用,不支持多页面多次调用js
在导航列表中,调用datagrid时,如果用js加载datagrid时,不知为何,报“404错误”找不到网页, 用datagrid时,用网页的形式调用,则没有问题. ----------------- ...
- iOS消息推送机制
iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...