题解 P5441 【【XR-2】伤痕】
题目大意
前置知识
骗分构造
题解
一组不强连通的四个点只有
- 从一个点向另外三个点各连一条有向边,记这样的四个点有
X 组。 - 在不满足第
1 种可能的前提下,从三个点向另外一个点各连一条有向边。 - A 与 B、C 与 D 之前都是无向边,但从 A 向 CD 各连一条有向边,从 B 向 CD 各连一条有向边。
记点
因为
又
因此,最少有
接下来,我们只需要构造出只有
构造方案显然不唯一,接下来给出一种构造方案:
将
n 个点放在一个圆内接正n 边形的顶点上,所有最长的对角线为无向边,每个点都向顺时针接下来的\frac{n - 3}{2} 个点连一条有向边。
显然,这种构造方案满足条件。
综上所述,最终的答案为:
注意
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n == 1) {
cout << 0 << endl << 0 << endl;
return 0;
}
cout << n * (n - 3) * (n * n + 6 * n - 31) / 48 << endl;
int m = (n + 1) >> 1;
for (int i = 1; i <= n; i++) {
int a[n+1];
memset(a, 0, sizeof(a));
for (int j = 1; j <= m; j++)
a[(i+j-1)%n+1] = 1;
for (int j = 1; j < n; j++) cout << a[j] << " ";
cout << a[n] << endl;
}
return 0;
}
Special Judge
#include "testlib.h"
#define AC return quitf(_ok, "The answer is correct."), 0
#define WA return quitf(_wa, "The answer is wrong."), 0
#define WArules return quitp(0.5, "The answer is correct, but your plan breaks the rules."), 0
#define WAplan return quitp(0.5, "The answer is correct, but your plan is wrong."), 0
int main(int argc, char* argv[]) {
registerTestlibCmd(argc, argv);
int n = inf.readInt(), ans = ouf.readInt(), a[n+1][n+1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
a[i][j] = ouf.readInt();
if ((n == 1 && ans) || (n != 1 && ans != n * (n - 3) * (n * n + 6 * n - 31) / 48)) WA;
int cnt = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (a[i][j] && a[i][j] != 1) WArules;
if (i == j) {
if (a[i][j]) WArules;
} else {
if (!a[i][j] && !a[j][i]) WArules;
if (i < j && a[i][j] && a[j][i]) ++cnt;
}
}
if (cnt > n) WArules;
int k = 0, p[5];
for (p[1] = 1; p[1] <= n; p[1]++)
for (p[2] = p[1] + 1; p[2] <= n; p[2]++)
for (p[3] = p[2] + 1; p[3] <= n; p[3]++)
for (p[4] = p[3] + 1; p[4] <= n; p[4]++) {
bool flag = 0;
int o[5];
for (int i = 1; i <= 4; i++) o[i] = p[i];
do if (a[o[1]][o[2]] && a[o[2]][o[3]] && a[o[3]][o[4]] && a[o[4]][o[1]]) flag = 1;
while (!flag && std::next_permutation(o + 1, o + 5));
k += flag;
}
if (k != ans) WAplan;
AC;
}