题解 luogu P2565 [SCOI2009]骰子的学问

一道构造好题

Posted by nomonick on October 20, 2021

P2565 [SCOI2009]骰子的学问

题面解析

题面 link : P2565

算法分析

算法实现

code

#include <bits/stdc++.h>
const int SIZE = (int)2e2 + 50;
const int Sample[4][5] = { {0,0,0,0,0}, {0,1,3,10,11}, {0,2,7,8,9}, {0,4,5,6,12} };
int n,m,tot,head,tail;
int son[SIZE],indegree[SIZE],que[SIZE];
int ans[SIZE][SIZE];
inline void toposort()
{
	for (int i = 1; i <= n; ++i) if (!indegree[i]) que[++tail] = i;
	while (head <= tail)
	{
		int u = que[head++]; --indegree[son[u]];
		for (int i = 1; i <= m; ++i) ans[u][i] = tot--;
		if (!indegree[son[u]]) que[++tail] = son[u];
	}
	for (int i = 1; i <= n; ++i)
	{
		if (!indegree[i]) continue; tail = 0;
		for (int u = i; indegree[u]; u = son[u]) indegree[que[++tail] = u] = 0;
		if (tail < 3) {printf("0\n"); exit(0);}
		tot -= tail * m;
		if (tail == 3 && m == 4)
		{
			ans[que[1]][1] = Sample[1][1] + tot; ans[que[1]][2] = Sample[1][2] + tot;
			ans[que[1]][3] = Sample[1][3] + tot; ans[que[1]][4] = Sample[1][4] + tot;
			ans[que[2]][1] = Sample[2][1] + tot; ans[que[2]][2] = Sample[2][2] + tot;
			ans[que[2]][3] = Sample[2][3] + tot; ans[que[2]][4] = Sample[2][4] + tot;
			ans[que[3]][1] = Sample[3][1] + tot; ans[que[3]][2] = Sample[3][2] + tot;
			ans[que[3]][3] = Sample[3][3] + tot; ans[que[3]][4] = Sample[3][4] + tot;
			continue;
		}
		for (int j = 1,now = 1; now <= m; j = (j == 1 ?  tail : j - 1), ++now)
		{
			ans[que[j]][now] = ++tot;
			for (int k = (j == 1 ? tail : j - 1); k ^ j; k = (k == 1 ?  tail : k - 1)) 
				ans[que[k]][now] = ++tot;
		}
		tot -= tail * m;
	}
}
signed main()
{
	cin >> n >> m; tot = n * m; head = 1;
	for (int i = 1; i <= n; ++i) cin >> son[i],indegree[son[i]]++;
	for (int i = 1; i <= n; ++i) if (son[i] == i) return printf("0\n"),0;
	toposort();
	for (int i = 1; i <= n; ++i,printf("\n"))
		for (int j = 1; j <= m; ++j)
			printf("%d ",ans[i][j]);
	return 0;
}