非常妙的经典模型转化啊……

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.outin 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".


题目大意

给出一个 N×N 的矩阵 G,要求判断其是否满足:

  1. G[i][i]=0;
  2. G[i][j]=G[j][i];
  3. 对于所有的 i,j,k,有 G[i][j]≤max(G[i][k],G[j][k])。

题目分析

这个矩阵的形式非常特殊,然而考场上只是一直在想$n^{2.66}$的鬼畜矩乘……

本题是经典模型的巧妙转化。这里给出的矩阵实际上是一个邻接矩阵的形式,而第三个限制条件说的就是:原图最小瓶颈生成树的距离矩阵等于原矩阵。

 #include<bits/stdc++.h>
const int maxn = ;
const int maxm = ; struct Edge
{
int v,val;
Edge(int a=, int b=):v(a),val(b) {}
}edges[maxm];
int n,a[maxn][maxn],f[maxn][maxn];
int edgeTot,head[maxn],nxt[maxm],pre[maxn],dis[maxn];
bool chk,vis[maxn]; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void addedge(int u, int v, int c)
{
edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
edges[++edgeTot] = Edge(u, c), nxt[edgeTot] = head[v], head[v] = edgeTot;
}
void dfs(int Top, int x, int fa, int c)
{
f[Top][x] = c;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].v;
if (v==fa) continue;
dfs(Top, v, x, std::max(c, edges[i].val));
}
}
int main()
{
memset(head, -, sizeof head);
n = read();
for (int i=; i<=n; i++)
for (int j=; j<=n; j++)
a[i][j] = read();
chk = ;
for (int i=; i<=n&&chk; i++)
if (a[i][i]) chk = false;
for (int i=; i<=n&&chk; i++)
for (int j=i+; j<=n&&chk; j++)
if (a[i][j]!=a[j][i]) chk = false;
if (!chk) puts("NOT MAGIC");
else{
memset(dis, 0x3f3f3f3f, sizeof dis);
dis[] = ;
for (int T=; T<=n; T++)
{
int pos = ;
for (int i=; i<=n; i++)
if (dis[i] < dis[pos]&&!vis[i]) pos = i;
if (!pos) break;
vis[pos] = true;
if (pre[pos]) addedge(pos, pre[pos], dis[pos]);
for (int i=; i<=n; i++)
if (!vis[i]&&dis[i] > a[pos][i])
dis[i] = a[pos][i], pre[i] = pos;
}
for (int i=; i<=n; i++) dfs(i, i, i, );
for (int i=; i<=n&&chk; i++)
for (int j=; j<=n&&chk; j++)
if (a[i][j]!=f[i][j]) chk = false;
puts(chk?"MAGIC":"NOT MAGIC");
}
return ;
}

END

【思维题 经典模型】cf632F. Magic Matrix的更多相关文章

  1. UVA.11300 Spreading the Wealth (思维题 中位数模型)

    UVA.11300 Spreading the Wealth (思维题) 题意分析 现给出n个人,每个人手中有a[i]个数的金币,每个人能给其左右相邻的人金币,现在要求你安排传递金币的方案,使得每个人 ...

  2. [Hdu-5155] Harry And Magic Box[思维题+容斥,计数Dp]

    Online Judge:Hdu5155 Label:思维题+容斥,计数Dp 题面: 题目描述 给定一个大小为\(N*M\)的神奇盒子,里面每行每列都至少有一个钻石,问可行的排列方案数.由于答案较大, ...

  3. Codeforces 878D - Magic Breeding(bitset,思维题)

    题面传送门 很容易发现一件事情,那就是数组的每一位都是独立的,但由于这题数组长度 \(n\) 很大,我们不能每次修改都枚举每一位更新其对答案的贡献,这样复杂度必炸无疑.但是这题有个显然的突破口,那就是 ...

  4. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  5. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  6. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  7. 【Keras篇】---利用keras改写VGG16经典模型在手写数字识别体中的应用

    一.前述 VGG16是由16层神经网络构成的经典模型,包括多层卷积,多层全连接层,一般我们改写的时候卷积层基本不动,全连接层从后面几层依次向前改写,因为先改参数较小的. 二.具体 1.因为本文中代码需 ...

  8. 【神经网络篇】--基于数据集cifa10的经典模型实例

    一.前述 本文分享一篇基于数据集cifa10的经典模型架构和代码. 二.代码 import tensorflow as tf import numpy as np import math import ...

  9. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

随机推荐

  1. Python网络爬虫(二)

    Urllib库之解析链接 Urllib库里有一个parse这个模块,定义了处理URL的标准接口,实现 URL 各部分的抽取,合并以及链接转换.它支持如下协议的 URL 处理:file.ftp.goph ...

  2. BZOJ 1053 [HAOI2007]反素数ant 神奇的约数

    本蒟蒻终于开始接触数学了...之前写的都忘了...忽然想起来某神犇在几个月前就切了FWT了... 给出三个结论: 1.1-N中的反素数是1-N中约数最多但是最小的数 2.1-N中的所有数的质因子种类不 ...

  3. python模块之time方法详细介绍

    >>> import time >>> dir(time) ['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__nam ...

  4. Hie with the Pie(poj3311)

    题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...

  5. 2 - Bootstrap-引导类-Bootstrap/ServerBootstrap

    2.1 ChannelOption和属性 //设置属性 bootstrap.option("xxx", 1231231); bootstrap.attr("xxx&quo ...

  6. ElasticSearch java API-使用More like this实现基于内容的推荐

    ElasticSearch java API-使用More like this实现基于内容的推荐 基于内容的推荐通常是给定一篇文档信息,然后给用户推荐与该文档相识的文档.Lucene的api中有实现查 ...

  7. 去除pycharm的波浪线

    PyCharm使用了较为严格的PEP8的检查规则,如果代码命名不规范,甚至多出的空格都会被波浪线标识出来,导致整个编辑器里铺满了波浪线,右边的滚动条也全是黄色或灰色的标记线,很是影响编辑.这里给大家分 ...

  8. android读写SD卡封装的类

    参考了网上的一些资源代码,FileUtils.java: package com.example.test; import java.io.BufferedInputStream; import ja ...

  9. mybatis-plus 异常 Invalid bound statement (not found)

    最近吧项目中添加使用了mybatis-plus,发现操作sql的时候出现异常: Invalid bound statement (not found) ,异常位置位于mybatis-plus的jar中 ...

  10. angular 学习笔记(1) 使用angular完整写法

    视图部分: <div ng-app="myapp"> <div ng-controller="myctrl"> <p>{{n ...