AT4162 [ARC099C] Independence

AT4162 [ARC099C] Independence

对于完全子图,考虑补图性质,即图内任意两个均无边。

若原图可分为两个完全子图,那么补图内子图无边,两子图间可有边,这不正是二分图吗?

染色判断奇环无解,求出每连通块答案,可行性 DP 计算答案即可。

时间复杂度 O ( n ) \mathcal O(n) O(n)

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define int long long

#define ha putchar(' ')
#define he putchar('\n')

inline int read() {
	int x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
		x = x * 10 + c - '0', c = getchar();
	return x * f;
}

inline void write(int x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + '0');
}

const int _ = 705;

int n, m, cnt, ans, t[3], c[_], t1[_], t2[_];

bool mp[_][_], f[_][_];

int tot, head[_], to[_ * _], nxt[_ * _];

void add(int u, int v) {
	to[++tot] = v, nxt[tot] = head[u], head[u] = tot;
	to[++tot] = u, nxt[tot] = head[v], head[v] = tot;
}

void dfs(int u) {
	t[c[u]]++;
	for (int i = head[u]; i; i = nxt[i]) {
		int v = to[i];
		if (c[v]) {
			if (c[u] == c[v]) {
				write(-1);
				exit(0);
			}
			continue;
		}
		c[v] = (c[u] == 1 ? 2 : 1);
		dfs(v);
	}
}

signed main() {
	n = read(), m = read(), ans = n * n;
	for (int i = 1, u, v; i <= m; ++i) {
		u = read(), v = read();
		mp[u][v] = mp[v][u] = 1;
	}
	for (int i = 1; i <= n; ++i)
		for (int j = i + 1; j <= n; ++j)
			if (!mp[i][j]) add(i, j);
	for (int i = 1; i <= n; ++i)
		if (!c[i]) {
			cnt++, c[i] = 1, t[1] = t[2] = 0;
			dfs(i);
			t1[cnt] = t[1], t2[cnt] = t[2];
		}
	f[0][0] = 1;
	for (int i = 1; i <= cnt; ++i)
		for (int j = 0; j <= n; ++j)
			if (f[i - 1][j])
				f[i][j + t1[i]] = f[i][j + t2[i]] = 1;
	for (int i = 0; i <= n; ++i)
		if (f[cnt][i]) ans = min(ans, i * (i - 1) / 2 + (n - i) * (n - i - 1) / 2);
	write(ans), he;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值