Magic Matrix

有趣的题。

首先第一个和第二个条件可以暴力枚举,复杂度 O(n2)O(n^2)

可以发现如果满足了第一个和第二个条件,这个矩阵其实就是一个无向完全图的邻接矩阵,ai,ja_{i,j} 表示 iji \leftrightarrow j 的边权。

接着思考第三个。

我们发现第三个限制相当于,对于任意 i,ji,j,不存在一个点 kk 使得 ikji \rightarrow k \rightarrow j 的路径的边权最大值小于 ai,ja_{i,j}

看着是一条长度为 33 的路径,但是我们可以把 kk 带入 jj 中,变成长度为 44 的路径。依次循环带入,就变成任意长的路径。

所以我们要判断,对于所有 i,ji,j,是否存在一条路径的边权最大值小于 ai,ja_{i,j}。换句话说,对于 i,ji,j,我们要找到从 iijj 的一条边权最大值最小的路径。这是什么?经典的最小瓶颈路。

于是求出最小生成树之后维护树上两点之间边权最大值即可。使用树剖加 ST 表可以做到 O(n2logn)O(n^2 \log n)

此外,建议加上快读。我不用快读用 scanf 一直 TLE。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>
#include <utility>
#include <string>
using namespace std;

const int N = 6e6 + 5, M = 5e3 + 5;

int n, a[M][M], m = 0, aa[M];
vector<pair<int, int> > G[M];
bool vs[N];

struct Edge
{
	int u, v, w;
	bool operator<(const Edge& g) const
	{
		return w < g.w;
	}
}p[N];

class Union_Find
{
public:
	int fa[M];
	void Init()
	{
		for (int i = 0; i < M; i++) fa[i] = i;
	}
	int find(int u)
	{
		return (fa[u] == u ? u : fa[u] = find(fa[u]));
	}
	void merge(int u, int v)
	{
		fa[find(u)] = find(v);
	}
}s;

void kruskal()
{
	s.Init();
	sort(p + 1, p + m + 1);
	for (int i = 1; i <= m; i++)
	{
		int u = p[i].u, v = p[i].v;
		if (s.find(u) == s.find(v))
		{
			continue;
		}
		G[u].emplace_back(make_pair(v, p[i].w));
		G[v].emplace_back(make_pair(u, p[i].w));
		s.merge(u, v);
		vs[i] = 1;
	}
}

class ST
{
public:
	int f[M][30], LG2[M];
	void Init(int* a)
	{
		for (int i = 1; i <= n; i++)
		{
			f[i][0] = a[i];
		}
		for (int i = 2; i < M; i++)
		{
			LG2[i] = LG2[i / 2] + 1;
		}
		for (int i = 1; i <= LG2[n]; i++)
		{
			for (int j = 1; j + (1 << i) - 1 <= n; j++)
			{
				f[j][i] = max(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
			}
		}
	}
	int query(int l, int r)
	{
		if (l > r) return 0;
		int p = LG2[r - l + 1];
		return max(f[l][p], f[r - (1 << p) + 1][p]);
	}
};

class TreeCut
{
public:
	ST st;
	int id[M], top[M], sz[M], fa[M], dep[M], son[M], na[M], idx;
	void dfs1(int u, int f)
	{
		sz[u] = 1;
		dep[u] = dep[f] + 1;
		fa[u] = f;
		for (auto& j : G[u])
		{
			if (j.first == f) continue;
			dfs1(j.first, u);
			sz[u] += sz[j.first];
			if (sz[son[u]] < sz[j.first]) son[u] = j.first;
		}
	}
	void dfs2(int u, int f)
	{
		top[u] = f;
		id[u] = ++idx;
		na[idx] = aa[u];
		if (!son[u]) return;
		dfs2(son[u], f);
		for (auto& j : G[u])
		{
			if (j.first != son[u] && j.first != fa[u])
			{
				dfs2(j.first, j.first);
			}
		}
	}
	void build()
	{
		dfs1(1, 1);
		for (int i = 1; i <= m; i++)
		{
			if (!vs[i]) continue;
			if (dep[p[i].u] > dep[p[i].v]) swap(p[i].u, p[i].v);
			aa[p[i].v] = p[i].w;
		}
		dfs2(1, 1);
		st.Init(na);
	}
	int query(int u, int v)
	{
		int res = 0;
		while (top[u] ^ top[v])
		{
			if (dep[top[u]] < dep[top[v]]) swap(u, v);
			res = max(res, st.query(id[top[u]], id[u]));
			u = fa[top[u]];
		}
		if (dep[u] > dep[v]) swap(u, v);
		res = max(res, st.query(id[u] + 1, id[v]));
		return res;
	}
}tc;

struct FastIO {
	static const int S = 1e7;
	int wpos;
	char wbuf[S];
	FastIO() : wpos(0) {}
	inline int xchar() {
		static char buf[S];
		static int len = 0, pos = 0;
		if (pos == len)
			pos = 0, len = fread(buf, 1, S, stdin);
		if (pos == len) exit(0);
		return buf[pos++];
	}
	inline int xuint() {
		int c = xchar(), x = 0;
		while (c <= 32) c = xchar();
		for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
		return x;
	}
	inline int xint()
	{
		int s = 1, c = xchar(), x = 0;
		while (c <= 32) c = xchar();
		if (c == '-') s = -1, c = xchar();
		for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';
		return x * s;
	}
	inline void xstring(char* s)
	{
		int c = xchar();
		while (c <= 32) c = xchar();
		for (; c > 32; c = xchar()) *s++ = c;
		*s = 0;
	}
	inline void wchar(int x)
	{
		if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;
		wbuf[wpos++] = x;
	}
	inline void wstring(const char* s)
	{
		while (*s) wchar(*s++);
	}
	~FastIO()
	{
		if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;
	}
} io;

int main()
{
	n = io.xint();
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			a[i][j] = io.xint();
		}
		if (a[i][i] != 0)
		{
			printf("NOT MAGIC\n");
			return 0;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (a[i][j] != a[j][i])
			{
				printf("NOT MAGIC\n");
				return 0;
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = i + 1; j <= n; j++)
		{
			p[++m] = { i, j, a[i][j] };
		}
	}
	kruskal();
	tc.build();
	for (int i = 1; i <= n; i++)
	{
		for (int j = i + 1; j <= n; j++)
		{
			int p = tc.query(i, j);
			if (p < a[i][j])
			{
				printf("NOT MAGIC\n");
				return 0;
			}
		}
	}
	printf("MAGIC\n");
	return 0;
}
posted @   HappyBobb  阅读(2)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示