zoukankan      html  css  js  c++  java
  • 搜索(DLX):HOJ 1017

    1017 - Exact cover

    Time Limit: 15s Memory Limit: 128MB

    Special Judge Submissions: 6751 Solved: 3519
    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
    
    这大概就是DLX的模板题了。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int N=1010,M=N*N;
     6 int L[M],R[M],U[M],D[M];
     7 int H[N],C[N],row[M],col[M];
     8 int cnt,n,m,ans[N];
     9 struct DLX{
    10   void Init(int n,int m){
    11     for(int i=0;i<=m;i++){
    12       L[i]=i-1;R[i]=i+1;
    13       C[i]=0;U[i]=D[i]=i;
    14     }cnt=L[0]=m;R[m]=0;
    15     for(int i=1;i<=n;i++)H[i]=0;
    16   }
    17   void Link(int r,int c){
    18     ++cnt;C[c]++;
    19     col[cnt]=c;row[cnt]=r;
    20     U[D[c]]=cnt;U[cnt]=c;
    21     D[cnt]=D[c];D[c]=cnt;
    22     if(H[r]){
    23       R[cnt]=R[H[r]];L[cnt]=H[r];
    24       L[R[H[r]]]=cnt;R[H[r]]=cnt;
    25     }
    26     else H[r]=L[cnt]=R[cnt]=cnt;
    27   }
    28   void Delete(int c){
    29     R[L[c]]=R[c];L[R[c]]=L[c];
    30     for(int i=D[c];i!=c;i=D[i])
    31       for(int j=R[i];j!=i;j=R[j])
    32     --C[col[j]],U[D[j]]=U[j],D[U[j]]=D[j];
    33   }
    34   void Resume(int c){
    35     R[L[c]]=c;L[R[c]]=c;
    36     for(int i=U[c];i!=c;i=U[i])
    37       for(int j=L[i];j!=i;j=L[j])
    38     ++C[col[j]],U[D[j]]=j,D[U[j]]=j;
    39   }
    40   bool Dance(int dep){
    41     if(!R[0]){
    42       printf("%d",dep);
    43       for(int i=1;i<=dep;i++)
    44     printf(" %d",ans[i]);
    45       puts("");return true;
    46     }int p=0;
    47     for(int i=R[0];i;i=R[i])
    48       if(!p||C[p]>C[i])p=i;
    49     Delete(p);
    50     for(int i=D[p];i!=p;i=D[i]){
    51       ans[dep+1]=row[i];
    52       for(int j=R[i];j!=i;j=R[j])Delete(col[j]);
    53       if(Dance(dep+1))return true;
    54       for(int j=L[i];j!=i;j=L[j])Resume(col[j]);
    55     }
    56     Resume(p);
    57     return false;
    58   }
    59 }dlx;
    60 
    61 int main(){
    62   while(scanf("%d%d",&n,&m)!=EOF){
    63     dlx.Init(n,m);
    64     for(int i=1;i<=n;i++){
    65       int k,x;
    66       scanf("%d",&k);
    67       while(k--){
    68     scanf("%d",&x);
    69     dlx.Link(i,x);
    70       }
    71     }if(!dlx.Dance(0))
    72     printf("NO
    ");
    73   }
    74   return 0;
    75 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    time zone list
    docker build doris-0.11.20-release source code
    Apache Flink 开发环境搭建和应用的配置、部署及运行
    locate home of running java application
    Android wpa_supplicant 四次握手 流程分析
    Wifi 开放系统认证和共享密钥身份认证
    Android WiFi 获取国家码
    Android WiFi 扫描流程分析(wpa_supplicant选择网络)
    Android WiFi 日志记录(ASSOC_REJECT)
    Android WiFi 扫描流程分析(wpa_supplicant)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5232801.html
Copyright © 2011-2022 走看看