标签:
分析:
#include<cstdio> #include<cstring> #include<queue> #include<cstdlib> #include<algorithm> #include<vector> #include<cmath> using namespace std; typedef long long LL; const int N=1e6+5; const double eps=1e-8; int n,m,sz,k; int u[N],l[N],r[N],d[N]; int h[1005],s[1005],col[N]; void init() { for(int i=0; i<=m; ++i) { s[i]=0; u[i]=d[i]=i; l[i]=i-1; r[i]=i+1; } r[m]=0; l[0]=m; sz=m; for(int i=1; i<=n; ++i) h[i]=-1; } void link(int x,int y) { ++sz; ++s[y],col[sz]=y; u[sz]=u[y],d[u[y]]=sz; d[sz]=y,u[y]=sz; if(h[x]==-1)h[x]=l[sz]=r[sz]=sz; { l[sz]=l[h[x]]; r[l[h[x]]]=sz; r[sz]=h[x]; l[h[x]]=sz; } } void del(int y) { for(int i=d[y]; i!=y; i=d[i]) r[l[i]]=r[i],l[r[i]]=l[i]; } void resume(int y) { for(int i=d[y]; i!=y; i=d[i]) r[l[i]]=l[r[i]]=i; } int f() { int ret=0; for(int i=r[0];i;i=r[i])++ret; return ret; } int ans; void dance(int pos) { if(pos+f()<=ans)return; if(!r[0]) { ans=max(pos,ans); return; } int t=r[0]; for(int i=r[0]; i!=0; i=r[i]) if(s[i]<s[t])t=i; for(int i=d[t]; i!=t; i=d[i]) { del(i); for(int j=r[i]; j!=i; j=r[j]) del(j); dance(pos+1); for(int j=l[i]; j!=i; j=l[j]) resume(j); resume(i); } } LL a[1005]; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%I64d",&a[i]); sort(a+1,a+1+n); m=n; init(); for(int i=1;i<=n;++i) for(int 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 重复覆盖
标签:
原文地址:http://www.cnblogs.com/shuguangzw/p/5293822.html