Educational Codeforces Round 9 F. Magic Matrix 最小生成树
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 最小生成树的更多相关文章
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Educational Codeforces Round 8 D. Magic Numbers
Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...
- Educational Codeforces Round 61 F 思维 + 区间dp
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
- Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...
- 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 ...
- Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
- Educational Codeforces Round 6 F. Xors on Segments 暴力
F. Xors on Segments 题目连接: http://www.codeforces.com/contest/620/problem/F Description You are given ...
- 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 ...
随机推荐
- 【Python学习笔记】Coursera课程《Using Python to Access Web Data 》 密歇根大学 Charles Severance——Week2 Regular Expressions课堂笔记
Coursera课程<Using Python to Access Web Data > 密歇根大学 Charles Severance Week2 Regular Expressions ...
- linux 设备树【转】
转自:http://blog.csdn.net/chenqianleo/article/details/77779439 [-] linux 设备树 为什么要使用设备树Device Tree 设备树的 ...
- linux内核启动分析(3)
主要分析do_basic_setup函数里面的do_initcalls()函数,这个函数用来调用所有编译内核的驱动模块中的初始化函数. static void __init do_initcalls( ...
- win10安装提示“我们无法创建新的分区”
今日于笔记本安装win10时突然出现提示:我们无法创建新的分区.网上搜了不少建议,尝试了都无果. 由于我的笔记本是固态硬盘与机械硬盘混合,所以情况可能更加特殊. 最后成功的方法是: 1. 先将Win1 ...
- POJ-3268
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13738 Accepted: 6195 ...
- Django Ajax学习一
1. 简单的加法 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 都是干货---真正的了解scrapy框架
去重规则 在爬虫应用中,我们可以在request对象中设置参数dont_filter = True 来阻止去重.而scrapy框架中是默认去重的,那内部是如何去重的. from scrapy.dupe ...
- 【转】Debug 运行正常,Release版本不能正常运行
http://blog.csdn.net/ruifangcui7758/archive/2010/10/18/5948611.aspx引言 如果在您的开发过程中遇到了常见的错误,或许您的Release ...
- 关于云平台中OFFICE预览与视频预览的解决办法
最近,随着firefox x64的升级,出现flash插件完全被禁止的现象,html5替换是大势所趋,原来我们在云平台中有多处使用flash的地方,比如OFFICE预览,视频播放,游戏等,现对于OFF ...
- CentOS7.5右键创建空白文档
首先我们进入centos7桌面 在桌面上右键“打开终端” 在终端我们使用cd命令进入用户目录下的模板文件夹cd ~/模板 ---->中文版cd ~/Templates ---->中 ...