F. Magic Matrix

题目连接:

http://www.codeforces.com/contest/632/problem/F

Description

You're given a matrix A of size n × n.

Let's call the matrix with nonnegative elements magic if it is symmetric (so aij = aji), aii = 0 and aij ≤ max(aik, ajk) for all triples i, j, k. Note that i, j, k do not need to be distinct.

Determine if the matrix is magic.

As the input/output can reach very huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.

Input

The first line contains integer n (1 ≤ n ≤ 2500) — the size of the matrix A.

Each of the next n lines contains n integers aij (0 ≤ aij < 109) — the elements of the matrix A.

Note that the given matrix not necessarily is symmetric and can be arbitrary.

Output

Print ''MAGIC" (without quotes) if the given matrix A is magic. Otherwise print ''NOT MAGIC".

Sample Input

3

0 1 2

1 0 2

2 2 0

Sample Output

MAGIC

Hint

题意

给你一个nn的矩阵,然后判断是否是magic的

如何是magic的呢?只要a[i][j]=a[j][i],a[i][i]=0,a[i][j]<=max(a[i][k],a[k][j])对于所有的k

题解:

就正解是把这个矩阵抽象成一个完全图

然后这个完全图,a[i][j]表示i点向j点连一条a[i][j]的边

然后magic是什么意思呢?

就是任何一条路径中的最大边都大于等于a[i][j],那么翻译过来就是跑最小生成树的之后,i到j上的最大边大于等于a[i][j]

于是就这样做呗。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2505;
int a[maxn][maxn];
pair<int,pair<int,int> > d[maxn*maxn];
int n,tot,last;
int fa[maxn];
int fi(int x)
{
return x == fa[x]?x:fa[x]=fi(fa[x]);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
{
fa[i]=i;
for(int j=1;j<=n;j++)
{
if(i==j&&a[i][j])return puts("NOT MAGIC");
if(a[i][j]!=a[j][i])return puts("NOT MAGIC");
if(i>j)d[tot++]=make_pair(a[i][j],make_pair(i,j));
}
}
sort(d,d+tot);
for(int i=0;i<tot;i++)
{
if(i+1<tot&&d[i].first==d[i+1].first)
continue;
for(int j=last;j<=i;j++)
{
int p1 = fi(d[j].second.first);
int p2 = fi(d[j].second.second);
if(p1==p2)return puts("NOT MAGIC");
}
for(int j=last;j<=i;j++)
{
int p1 = fi(d[j].second.first);
int p2 = fi(d[j].second.second);
fa[p2]=p1;
}
last=i+1;
}
puts("MAGIC");
}

Educational Codeforces Round 9 F. Magic Matrix 最小生成树的更多相关文章

  1. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  2. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  3. Educational Codeforces Round 8 D. Magic Numbers

    Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...

  4. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  5. Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)

    https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...

  6. Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)

    F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...

  7. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

  8. Educational Codeforces Round 6 F. Xors on Segments 暴力

    F. Xors on Segments 题目连接: http://www.codeforces.com/contest/620/problem/F Description You are given ...

  9. Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法

    F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...

随机推荐

  1. Perl6 Bailador框架(4):路径匹配

    use v6; use Bailador; =begin pod /:one/:two/:....路径选择 这个路径, 用/分隔 每个/分隔一个, 如果你只设置两个(/admin/login),时, ...

  2. YUV颜色编码解析(转)

    原文转自 https://www.jianshu.com/p/a91502c00fb0

  3. Linux进程调度与源码分析(三)——do_fork()的实现原理

    用户层的fork(),vfork(),clone()API函数在执行时,会触发系统调用完成从用户态陷入到内核态的过程,而上述函数的系统调用,最终实现都是通过内核函数do_fork()完成,本篇着重分析 ...

  4. 源码分析之tinyhttpd-0.1

    1. 简介: tinyhttpd是使用c语言开发的超轻量级http服务器,通过代码流程可以了解http服务器的基本处理流程, 并且涉及了网络套接字,线程,父子进程,管道等等知识点: 项目地址:http ...

  5. 【bzoj3223】文艺平衡树

    #include<bits/stdc++.h> #define N 300005 #define rat 4 using namespace std; struct Node{ int s ...

  6. FineReport——JS二次开发(隐藏下拉框控件的倒三角)

    在对FR控件进行二次开发的过程中,需要自定义样式,比如下拉框控件带有自动检索的功能,但是又希望它的显示样式如同文本框一样,这时就需要隐藏多余的部分. 在对在线文档的查阅中可以发现很多选择器适用于多种控 ...

  7. MySQL索引基础知识点

    什么是索引 索引类似于书本目录,是数据库存储引擎维护的用于快速查找到记录的一种数据结构,它是对查询性能优化的最有效手段. MySQL索引是在存储引擎层而不是服务器层实现的,不同存储引擎的索引工作方式也 ...

  8. json转换工具——fastjson的使用

    1.maven依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...

  9. Child Action

    Control [ChildActionOnly]      //只能用于Child Action public ActionResult Time() { return PartialView(Da ...

  10. 带有定位当前位置的百度地图web api 前端代码

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta name="viewport& ...