比赛 4043级NOIP2022欢乐赛1st 评测结果 AWWWAAWWTA
题目名称 Multiplayer Moo 最终得分 40
用户昵称 yrtiop 运行时间 1.750 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2022-10-28 21:24:17
显示代码纯文本
#include <bits/stdc++.h>
#define pb emplace_back
typedef std::pair<int,int> pii;

const int maxn = 305;
int cnt,cur,n,a[maxn][maxn],id[maxn][maxn],sum[maxn * maxn],c[maxn * maxn];
int fx[4] = {1 , 0 , -1 , 0},fy[4] = {0 , 1 , 0 , -1};
bool vis[maxn][maxn],inq[maxn * maxn];
std::map<int,bool> s[maxn * maxn];
std::vector<int> S[maxn * maxn];

void dfs(int x,int y,int col) {
	if(x <= 0||x > n||y <= 0||y > n)return ;
	++ cnt;
	vis[x][y] = true;
	id[x][y] = cur;
	for(int k = 0;k < 4;++ k) {
		int nx = x + fx[k],ny = y + fy[k];
		if(a[nx][ny] != col||vis[nx][ny])continue ;
		dfs(nx , ny , col);
	}
	return ;
}

std::vector<int> G;
void DFS(int x,int col1,int col2) {
	cnt += sum[x];
	inq[x] = true;
	G.pb(x);
	for(auto& v : S[x]) {
		if(inq[v])continue ;
		if(c[v] == col1||c[v] == col2)
			DFS(v , col1 , col2);
	}
	return ;
}

int main() {
	freopen("multimoo_silver_18open.in","r",stdin);
	freopen("multimoo_silver_18open.out","w",stdout);
	scanf("%d",&n);
	cnt = 0;
	for(int i = 1;i <= n;++ i) {
		for(int j = 1;j <= n;++ j) {
			scanf("%d",&a[i][j]);
			c[++ cnt] = a[i][j];
		}
	}
	
	std::sort(c + 1 , c + 1 + cnt);
	cnt = std::unique(c + 1 , c + 1 + cnt) - c - 1;
	for(int i = 1;i <= n;++ i)
		for(int j = 1;j <= n;++ j)
			a[i][j] = std::lower_bound(c + 1 , c + 1 + cnt , a[i][j]) - c;
	
	memset(c , 0 , sizeof(c));
	int ans = 0;
	for(int i = 1;i <= n;++ i) {
		for(int j = 1;j <= n;++ j) {
			if(!vis[i][j]) {
				++ cur;
				c[cur] = a[i][j];
				cnt = 0;
				dfs(i , j , a[i][j]);
				ans = std::max(ans , cnt);
				sum[cur] = cnt;
			}
		}
	}
	
	printf("%d\n",ans);
	
	for(int i = 1;i <= n;++ i) {
		for(int j = 1;j <= n;++ j) {
			for(int k = 0;k < 2;++ k) {
				int x = i + fx[k],y = j + fy[k];
				if(x < 1||x > n||y < 1||y > n)continue ;
				if(id[x][y] == id[i][j])continue ;
				if(s[id[i][j]][id[x][y]])continue ;
				s[id[i][j]][id[x][y]] = s[id[x][y]][id[i][j]] = true;
				S[id[i][j]].pb(id[x][y]);
			}
		}
	}
	
	int lst = ans;
	ans = 0;
	
	for(int i = 1;i <= cur;++ i) {
		for(auto& v : S[i]) {
			cnt = 0;
			G.clear();
			DFS(i , c[i] , c[v]);
			ans = std::max(ans , cnt);
			for(auto& k : G)inq[k] = false;
		}
	}
	
	printf("%d\n",std::max(ans , lst));
	return 0;
}