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 ...
随机推荐
- make command explaination 編譯命令解釋
Creating .config file make ARCH=arm CROSS_COMPILE=arm-none-eabi- stm32_defconfig 以上命令是 將變數 ARCH=arm, ...
- ERROR: do not initialise statics to false
Question about git commit rule I git commit a patch, The patch has a "static int xxxxxxxxxxxxxx ...
- Bookmarks_www2
Bookmarks Bookmarks alexis- (Alex Incogito) - Repositories · GitHub GitHub - aetcnc-Arduino_DeltaHMI ...
- 初识ES6
1.ECMAScript的官网地址:http://www.ecma-international.org/cma-262/6.0/,其是JS语言的下一代标准,已经在2015年6月正式发布,目标是让JS可 ...
- [New learn]讲解Objective-c的block知识-实践
1.简介 在之前的文章[New learn]讲解Objective-c的block知识中介绍了block的相关知识.本章中我们将以一个实际例子来简单介绍一下block如何代替代理. 2.原有通过代理实 ...
- Nginx惊群处理
惊群:是指在多线程/多进程中,当有一个客户端发生链接请求时,多线程/多进程都被唤醒,然后只仅仅有一个进程/线程处理成功,其他进程/线程还是回到睡眠状态,这种现象就是惊群. 惊群是经常发生现在serve ...
- redis之(八)redis的有序集合类型的命令
[一]增加元素 --->命令:ZADD key score member [score member] --->向有序集合放入一个分数为score的member元素 --->元素存在 ...
- Multiply Strings——面试题
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))
D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- php在web服务器中的工作原理
1.web工作原理 我是学习PHP网站建设的,那么网站在客户端和服务端的运行是网站运行的根本所在,那个这个运行过程是怎样的呢?我们一探就将! Web:终端 服务器web:我们把提供(响应)服务的计算机 ...