这句话到底有什么魔力啊
查看原帖
这句话到底有什么魔力啊
482728
Engulf楼主2023/2/13 18:22

这样写 58 分,如果把

if (!a[t][c]) continue;

给去掉,就 AC 了,非常的难以理解。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;

const int N = 40;

int n, m;
int a[N][N];

void gauss() {
	for (int c = 1, r = 1; c <= n; c++) {
		int t = r;
		for (int i = r + 1; i <= n; i++)
			if (a[i][c])
				t = i;
		if (!a[t][c]) continue;
		for (int i = c; i <= n + 1; i++) swap(a[t][i], a[r][i]);
		for (int i = r + 1; i <= n; i++)
			for (int j = n + 1; j >= c; j--)
				a[i][j] ^= a[r][j] & a[i][c];
		r++;
	}
}

int press[N];

int ans = inf;

void dfs(int u, int cnt) {
	if (!u) {
		ans = cnt;
		return;
	}
	if (cnt >= ans) return;
	if (a[u][u]) {
		press[u] = a[u][n + 1];
		for (int j = u + 1; j <= n; j++)
			press[u] ^= a[u][j] & press[j];
		dfs(u - 1, cnt + press[u]);
	} else {
		press[u] = 0;
		dfs(u - 1, cnt);
		press[u] = 1;
		dfs(u - 1, cnt + 1);
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		a[i][i] = a[i][n + 1] = 1;
	for (int i = 1, x, y; i <= m; i++) {
		cin >> x >> y;
		a[x][y] = a[y][x] = 1;
	}
	gauss();
	dfs(n, 0);
	cout << ans << endl;
	return 0;
}
2023/2/13 18:22
加载中...