hust 1017 DLX模板

2014-11-24 08:40:37 · 作者: · 浏览: 0
Exact cover
Time Limit:15000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu

[Submit] [Go Back] [Status]

Description

There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in exactly one of the selected rows.
Try to find out the selected rows.

Input

There are multiply test cases.
First line: two integers N, M;
The following N lines: Every line first comes an integer C(1 <= C <= 100), represents the number of 1s in this row, then comes C integers: the index of the columns whose value is 1 in this row.

Output

First output the number of rows in the selection, then output the index of the selected rows.
If there are multiply selections, you should just output any of them.
If there are no selection, just output "NO".

Sample Input

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

Sample Output

3 2 4 6

Source

dupeng

[Submit] [Go Back] [Status]


人生第一道DLX,抄个模板,慢慢理解。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-8 4:41:36
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
                using namespace std; #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; struct DLX{ const static int SZ=1000*100+10; const static int COL=1010; int L[SZ],R[SZ],U[SZ],D[SZ],X[SZ],Y[SZ]; int S[COL],H[COL]; int cnt; int ans[COL],acnt; void del(int *L,int *R,int x){ L[R[x]]=L[x]; R[L[x]]=R[x]; } void res(int *L,int *R,int x){ L[R[x]]=R[L[x]]=x; } void init(int col){ cnt=col+1; memset(H,-1,sizeof(H)); memset(S,0,sizeof(S)); for(int i=0;i<=col;i++){ L[i]=i-1; R[i]=i+1; U[i]=D[i]=i; } L[0]=col;R[col]=0; } void add(int *L,int *R,int head,int now){ int tail=L[head]; L[now]=tail; R[now]=head; R[tail]=L[head]=now; } void insert(int x,int y){ X[cnt]=x; Y[cnt]=y; add(U,D,y,cnt); if(H[x]<0)H[x]=L[cnt]=R[cnt]=cnt; else add(L,R,H[x],cnt); S[y]++; cnt++; } void remove(int y){ if(y==0)return; for(int i=D[y];i!=y;i=D[i]){ for(int j=R[i];j!=i;j=R[j]){ del(U,D,j); S[Y[j]]--; } } del(L,R,y); } void resume(int y){ if(y==0)return; for(int i=U[y];i!=y;i=U[i]){ for(int j=L[i];j!=i;j=L[j]){ res(U,D,j); S[Y[j]]++; } } res(L,R,y); } bool dfs(int k){ if(R[0]==0){ acnt=k; return 1; } int c=1; int mins=10000; for(int i=R[0];i!=0;i=R[i]){ if(S[i]
                
                 >n>>m){ dlx.init(m); for(int i=1;i<=n;i++){ int num,col; cin>>num; while(num--){ cin>>col; dlx.insert(i,col); } } if(dlx.dfs(0)){ printf("%d",dlx.acnt); for(int i=0;i