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 矩阵构造

    传送门 文章目录 题意: 思路: 题意: 给你一个奇数nnn,让你构造一个n∗nn*nn∗n的矩阵,矩阵的每个位置依次填上[1,n∗n]之内的数[1,n*n]之内的数[1,n∗n]之内的数,满足每行. ...

  2. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  3. Educational Codeforces Round 14 - F (codeforces 691F)

    题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...

  4. Educational Codeforces Round 60 D. Magic Gems

    易得递推式为f[i]=f[i-1]+f[i-M] 最终答案即为f[N]. 由于N很大,用矩阵快速幂求解. code: #include<bits/stdc++.h> using names ...

  5. Educational Codeforces Round 39 F Largest Beautiful Number

    传送门 暴力DP dp[i][j][k] 从j 到 k 发f(x) 的大小, #include <bits/stdc++.h> using namespace std; typedef l ...

  6. 双联通分量求简单环(Educational Codeforces Round 42: F. Simple Cycles Edges)

    题意: n个点m条边的无向图,问有哪些边在一个简单环上,按顺序输出这些边的编号 思路: 对于无向图求出每个双联通分量,对于每个双联通分量,如果点的个数==边的个数,那么这个双联通分量就是个简单环,输出 ...

  7. Educational Codeforces Round 51: F. The Shortest Statement(最短路+LCA)

    F. The Shortest Statement 题意: n个点m条边(m≤n+20)的无向连通图,Q次询问,每次求出给定两点的最短路 思路: 将题意转换一下,给你一棵n个节点的树,并且这个树上还有 ...

  8. Educational Codeforces Round 50: F. Relatively Prime Powers(莫比乌斯函数)

    F. Relatively Prime Powers 题意: 给你一个n,问满足在[2,n]范围内有多少个数是非次方数(也就是不是这样的) 思路: 答案就是 原理是利用容斥,注意n开i次根是向下取整( ...

  9. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

最新文章

  1. Scala开发入门教程
  2. VS C#窗体程序未能找到Form1.resx文件解决方法
  3. 企业网络推广——企业网络有推广专员如何做好基本的网站优化布局
  4. lwip协议栈中超时定时器实现原理
  5. shell训练营Day31
  6. 修建道路 贪心,思维(女赛)
  7. HDU OJ Matrix Swapping II
  8. VTK修炼之道5_Procedural Source Object
  9. 让 FileUpload 文本框只读
  10. Qt路径中常用字符“./”、“../”、“/”、“*”的含义
  11. python点名代码_基于python tkinter的点名小程序功能的实例代码
  12. 十二:内存简单介绍和OC的内存管理
  13. 三星active2怎么连接手机_手机怎么连接隐藏的wifi无线网络
  14. python异步爬虫_Python实现基于协程的异步爬虫
  15. 手机运行内存6+128跟8+128有什么区别?
  16. Java 中的抽象类和接口
  17. 数据库中的范式 Normal Form(用最简单的语言描述!)
  18. 【Luogu1580】yyy loves Easter_Egg I(纯字符串模拟)
  19. 使用OpenCV获取图像中某一点的像素值和修改某一点的像素值
  20. 部分AMD RAID驱动程序需及时升级

热门文章

  1. Jquery和javascript常用技巧
  2. CentOS 5.5 编译安装apache+php+mysql,利用CMS快速建立论坛
  3. 用C#对ADO.NET数据库完成简单操作
  4. WMI技术介绍和应用——查询正在运行的进程信息
  5. libyuv库的使用
  6. 【Qt】通过QtCreator源码学习Qt(六):命令行参数解析实现
  7. python远程登录linux命令,Python+requests通过paramiko远程登录Linux执行sh命令
  8. java filefilter递归_Java中的递归+文件过滤器
  9. qcustomplot 游标吸附_qcustomplot游标测量功能--Apple的学习笔记
  10. java冒泡排序_Java中的经典算法之冒泡排序(Bubble Sort)