标签:des style blog color io os ar for sp
Time Limit: 15s Memory Limit: 128MB
6 7 3 1 4 7 2 1 4 3 4 5 7 3 3 5 6 4 2 3 6 7 2 2 7
3 2 4 6
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<cmath> #include<algorithm> #include<set> #include<map> #include<vector> #include<string> #include<queue> using namespace std; #ifdef WIN32 #define LL "%I64d" #else #define LL "%lld" #endif #define MAXN 1100 #define MAXV MAXN*2 #define MAXE MAXV*2 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL #define MAXD 100000 typedef long long qword; inline int nextInt() { char ch; int x=0; bool flag=false; do ch=(char)getchar(),flag=(ch==‘-‘)?true:flag; while(ch<‘0‘||ch>‘9‘); do x=x*10+ch-‘0‘; while (ch=(char)getchar(),ch<=‘9‘ && ch>=‘0‘); return x*(flag?-1:1); } int n,m; struct DLX_t { static const int maxd=1000000; static const int maxn=1001; static const int maxm=1001; int L[maxd],R[maxd],U[maxd],D[maxd]; int rw[maxd]; int head; int topd; int chd[maxm]; int col[maxd]; int tt[maxm]; int n,m; vector<int> res; void init(int nn,int mm) { n=nn;m=mm; topd=0; memset(L,0,sizeof(L)); memset(R,0,sizeof(R)); memset(D,0,sizeof(D)); memset(U,0,sizeof(U)); memset(tt,0,sizeof(tt)); res.clear(); head=++topd; L[head]=R[head]=head; for (int i=1;i<=m;i++) { chd[i]=++topd; col[chd[i]]=i; rw[chd[i]]=0; R[chd[i]]=head; L[chd[i]]=L[head]; R[chd[i]]=head; L[R[chd[i]]]=chd[i]; R[L[chd[i]]]=chd[i]; U[chd[i]]=D[chd[i]]=chd[i]; } } void Add_row(int r,const vector<int> &vec) { int i; int nowh; int now; for (i=0;i<(int)vec.size();i++) { now=++topd; rw[now]=r; col[now]=vec[i]; tt[vec[i]]++; U[now]=U[chd[vec[i]]]; D[now]=chd[vec[i]]; D[U[now]]=now; U[D[now]]=now; } L[U[chd[vec[0]]]]=R[U[chd[vec[0]]]]=U[chd[vec[0]]]; nowh=U[chd[vec[0]]]; for (i=1;i<(int)vec.size();i++) { R[U[chd[vec[i]]]]=nowh; L[U[chd[vec[i]]]]=L[nowh]; L[R[U[chd[vec[i]]]]]=U[chd[vec[i]]]; R[L[U[chd[vec[i]]]]]=U[chd[vec[i]]]; } } void Finish() { vector<int> res2=res; sort(res2.begin(),res2.end()); printf("%d",(int)res2.size()); for (int i=0;i<(int)res2.size();i++) printf(" %d",res2[i]); printf("\n"); } void cover(int c) { int i,j; R[L[chd[c]]]=R[chd[c]]; L[R[chd[c]]]=L[chd[c]]; for (i=D[chd[c]];i!=chd[c];i=D[i]) { for (j=R[i];j!=i;j=R[j]) { tt[col[j]]--; U[D[j]]=U[j]; D[U[j]]=D[j]; } } } void resume(int c) { int i,j; R[L[chd[c]]]=chd[c]; L[R[chd[c]]]=chd[c]; for (i=D[chd[c]];i!=chd[c];i=D[i]) { for (j=R[i];j!=i;j=R[j]) { tt[col[j]]++; U[D[j]]=j; D[U[j]]=j; } } } bool dfs() { int now=head; if (L[now]==now) { Finish(); return true; } int bst=INF,bi=-1; int i,j; for (i=R[head];i!=head;i=R[i]) { if (tt[col[i]]<bst) { bst=tt[i]; bi=i; } } cover(col[bi]); for (i=D[bi];i!=bi;i=D[i]) { res.push_back(rw[i]); for (j=R[i];j!=i;j=R[j]) cover(col[j]); if (dfs())return true; res.pop_back(); for (j=R[i];j!=i;j=R[j]) resume(col[j]); } resume(col[bi]); return false; } }DLX; vector<int> vec; int main() { freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int i,j,k; int x,y,z; while (~scanf("%d%d",&n,&m)) { DLX.init(n,m); for (i=1;i<=n;i++) { scanf("%d",&y); vec.clear(); for (j=1;j<=y;j++) { scanf("%d",&x); vec.push_back(x); } sort(vec.begin(),vec.end()); DLX.Add_row(i,vec); } if (!DLX.dfs()) printf("NO\n"); } return 0; }
hustoj 1017 - Exact cover dancing link
标签:des style blog color io os ar for sp
原文地址:http://www.cnblogs.com/mhy12345/p/4010829.html