[最大团][二分图] LOJ #526. 「LibreOJ β Round #4」子集

Solution

(i,j) 存在当且仅当 [(ai,aj)1(ai+1,aj+1)1]
就等价于求最大团。
随机乱搞好像这种题都过不了qwq
发现一个性质, ai aj 奇偶性相同时是必定有边的。考虑补图的最大独立集,显然这是一个二分图,跑最大匹配就好啦。

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> Pairs;

const int N = 505;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; int sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

int n, ans, clc;
ll a[N];
int mp[N][N];
int usd[N], mat[N];

ll gcd(ll a, ll b) {
    ll c = 1;
    while (c) {
        c = a % b;
        a = b; b = c;
    }
    return a;
}
inline int match(int u) {
    for (int i = 1; i <= n; i++)
        if (usd[i] != clc && mp[u][i]) {
            usd[i] = clc;
            if (!mat[i] || match(mat[i])) {
                mat[i] = u; return 1;
            }
        }
    return 0;
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    read(n); ans = n;
    for (int i = 1; i <= n; i++) read(a[i]);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            mp[i][j] = ((gcd(a[i], a[j]) == 1) && (gcd(a[i] + 1, a[j] + 1) == 1));
    for (int i = 1; i <= n; i++)
        if (a[i] & 1) {
            ++clc; ans -= match(i);
        }
    cout << ans << endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值