hdu 3338 Kakuro Extension


最大獨立集

#include <cstdio>
#include <cstring>
int usedif[1005];
int link[1005];
int mat[1005][1005];
long long  a[1010];
int gx, gy;
bool can(int t)
{
    for(int i=1; i<=gy; i++)
    {
        if(usedif[i]==0&&mat[t][i])
        {
            usedif[i]=1;
            if(link[i]==-1||can(link[i]))
            {
                link[i]=t;
                return true;
            }
        }
    }
    return false;
}
int MaxMatch()
{
    int num=0;
    memset(link,-1,sizeof(link));
    for(int i=1; i<=gx; i++)
    {
        memset(usedif,0,sizeof(usedif));
        if(can(i))  num++;
    }
    return num;//返回最大匹配數
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        gx = n;
        gy = n;
        memset(mat, 0, sizeof(mat));
        for(int i = 1; i <= n; i++)  scanf("%I64d", &a[i]);
        for(int i = 1; i <= n; i++)
        {
            for(int j = i + 1; j <= n; j++)
            {
                if(a[i] % a[j] == 0 || a[j] % a[i] == 0)
                mat[i][j] = 1;
            }
        }
        printf("%d\n", n - MaxMatch());
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章