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. Object的公用方法们

    如图所示,Object一共有10种方法: 下面详细描述: 1.public Object() 方法,默认构造函数方法,当新建一个Object对象的时候,调用这个方法向堆区申请一片内存: 2.priva ...

  2. 005 JAVA多线程和并发基础面试问答(转载)

    原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-answers/ 多线程和并发问题是Ja ...

  3. 【数位dp入门】【HDU2089】62

    为了我的点歪的技能树…… 所以开始补一些sb的东西…… #include<bits/stdc++.h> typedef long long ll; using namespace std; ...

  4. Smarty模板快速入门

    文件下载 1.下载地址:http://www.smarty.net/ 2.我下载的版本是3.1.27 ,将下载的文件smarty-3.1.27.zip解压出来,然后将libs文件夹的所有文件复制到你的 ...

  5. UNIX shell 学习笔记 一 : 几个shell的规则语法对比

    1. 查看系统有哪些可用的shell cat /etc/shell 2. 每种shell都有一个特殊内置变量来存上一条命令的退出状态,例: C/TC shell $status % cp fx fy ...

  6. MYSQL5.5源码安装 linux下

    /* 首先安装必要的库 */ yum -y install gcc* ###### 安装 MYSQL ###### 首先安装camke 一.支持YUM,则 yum install -y cmake 二 ...

  7. c语言实现CRC校验和

    最近在摄像头采集的数据清晰度上需要加强,则在每一帧传输的数据包后边加了CRC校验和.CRC校验和有16位的,也有32位的.至于CRC校验和算法原理,我是在百度上学习的,其实网上有很多这种资料.简单的说 ...

  8. PHP的数据类型

      原始类型共8种: 1,  4种标量类型:boolean(布尔型).integer(整形).float/double(浮点型).string(字符串型): 2,  2种复合型:array(数组).o ...

  9. mui页面跳转

    $('.mui-title').on('click',function(){ mui.openWindow({ //跳转到指导信息页面 url:"/index.php?m=mobile&am ...

  10. JQuery一个对象绑定多个事件

    jQuery("#id").click(func1).mouseover(func2)//方法连写,func为方法的名字 jQuery("#id").click ...