hdu 3335 Divisibility(Dancing Links重复覆盖)

Divisibility

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 749    Accepted Submission(s): 258


Problem Description
As we know,the fzu AekdyCoin is famous of math,especially in the field of number theory.So,many people call him "the descendant of Chen Jingrun",which brings him a good reputation.
AekdyCoin also plays an important role in the ACM_DIY group,many people always ask him questions about number theory.One day,all members urged him to conduct a lesson in the group.The rookie daizhenyang is extremely weak at math,so he is delighted.
However,when AekdyCoin tells us "As we know, some numbers have interesting property. For example, any even number has the property that could be divided by 2.",daizhenyang got confused,for he don't have the concept of divisibility.He asks other people for help,first,he randomizely writes some positive integer numbers,then you have to pick some numbers from the group,the only constraint is that if you choose number a,you can't choose a number divides a or a number divided by a.(to illustrate the concept of divisibility),and you have to choose as many numbers as you can.
Poor daizhenyang does well in neither math nor programming.The responsibility comes to you!
 

Input
An integer t,indicating the number of testcases,
For every case, first a number n indicating daizhenyang has writen n numbers(n<=1000),then n numbers,all in the range of (1...2^63-1).
 

Output
The most number you can choose.
 

Sample Input
   
   
   
   
1 3 1 2 3
 

Sample Output
   
   
   
   
2 Hint: If we choose 2 and 3,one is not divisible by the other,which is the most number you can choose.
 

Author
DaiZhenyang@BUPT
 

Source
HDOJ Monthly Contest – 2010.03.06
 

Recommend
lcy
 
题目: http://acm.hdu.edu.cn/showproblem.php?pid=3335
分析:这题话说是二分图匹配。。。表示看不出来,于是用DLX来搞了,一个n*n的01矩阵,i行j列为1表示,第i个数能被第j个数整除或整除它,于是变成求取最多次使得每一列都至少包含一个1。。。然后居然1Y= =
代码:
#include<cstdio>
using namespace std;
const int mm=1000010;
const int mn=1111;
int D[mm],U[mm],L[mm],R[mm],C[mm];
int H[mn],S[mn];
__int64 a[mn];
bool v[mn];
int n,m,size,ans;
void prepare(int r,int c)
{
    for(int i=0;i<=c;++i)
    {
        S[i]=0;
        U[i]=D[i]=i;
        L[i+1]=i;
        R[i]=i+1;
    }
    R[size=c]=0;
    while(r)H[r--]=-1;
}
void remove(int c)
{
    for(int i=D[c];i!=c;i=D[i])
        L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
    for(int i=U[c];i!=c;i=U[i])
        L[R[i]]=R[L[i]]=i;
}
int f()
{
    int c,ret=0;
    for(c=R[0];c;c=R[c])++ret;
    return ret;
}
void Dance(int k)
{
    if(k+f()<=ans)return;
    if(!R[0])
    {
        ans=k;
        return;
    }
    int i,j,c,tmp=mm;
    for(i=R[0];i;i=R[i])
        if(S[i]<tmp)tmp=S[c=i];
    for(i=D[c];i!=c;i=D[i])
    {
        remove(i);
        for(j=R[i];j!=i;j=R[j])remove(j);
        Dance(k+1);
        for(j=L[i];j!=i;j=L[j])resume(j);
        resume(i);
    }
}
void Link(int r,int c)
{
    ++S[C[++size]=c];
    D[size]=D[c];
    U[D[c]]=size;
    U[size]=c;
    D[c]=size;
    if(H[r]<0)H[r]=L[size]=R[size]=size;
    else
    {
        R[size]=R[H[r]];
        L[R[H[r]]]=size;
        L[size]=H[r];
        R[H[r]]=size;
    }
}
int main()
{
    int i,j,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;++i)scanf("%I64d",&a[i]);
        prepare(n,n);
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)
                if((a[i]%a[j])==0||(a[j]%a[i])==0)Link(i,j);
        ans=0;
        Dance(0);
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(hdu 3335 Divisibility(Dancing Links重复覆盖))