zoukankan      html  css  js  c++  java
  • HUST 1017 Exact cover

    Exact cover

    http://acm.hust.edu.cn/problem.php?id=1017

    Special Judge Time Limit: 15 Sec  Memory Limit: 128 MB Submissions: 5179  Solved: 2748

    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
    

    HINT

    Source

    dupeng

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 
      5 using namespace std;
      6 
      7 const int N=1010;
      8 const int V=1000010;
      9 
     10 int U[V],D[V];
     11 int L[V],R[V];
     12 int C[V];
     13 int H[N],S[N],mark[V];
     14 int size,n,m,vis[N],flag;
     15 
     16 void Link(int r,int c){
     17     S[c]++;C[size]=c;
     18     U[size]=U[c];D[U[c]]=size;
     19     D[size]=c;U[c]=size;
     20     if(H[r]==-1)
     21         H[r]=L[size]=R[size]=size;
     22     else{
     23         L[size]=L[H[r]]; R[L[H[r]]]=size;
     24         R[size]=H[r];  L[H[r]]=size;
     25     }
     26     mark[size]=r;
     27     size++;
     28 }
     29 
     30 void remove(int c){ //删除列
     31     L[R[c]]=L[c];
     32     R[L[c]]=R[c];
     33     int i,j;
     34     for(i=D[c];i!=c;i=D[i])
     35         for(j=R[i];j!=i;j=R[j]){
     36             U[D[j]]=U[j],D[U[j]]=D[j];
     37             S[C[j]]--;
     38         }
     39 }
     40 
     41 void resume(int c){
     42     int i,j;
     43     for(i=U[c];i!=c;i=U[i])
     44         for(j=L[i];j!=i;j=L[j]){
     45             U[D[j]]=j,D[U[j]]=j;
     46             S[C[j]]++;
     47         }
     48     L[R[c]]=c;
     49     R[L[c]]=c;
     50 }
     51 
     52 void Dance(int k){
     53     int i,j,min,c;
     54     if(!R[0]){
     55         flag=1;
     56         printf("%d",k);
     57         for(i=0;i<k;i++)
     58             printf(" %d",mark[vis[i]]);
     59         printf("\n");
     60         return ;
     61     }
     62     for(min=N,i=R[0];i;i=R[i])
     63         if(S[i]<min){
     64             min=S[i];
     65             c=i;
     66         }
     67     remove(c);
     68     for(i=D[c];i!=c;i=D[i]){
     69         vis[k]=i;
     70         for(j=R[i];j!=i;j=R[j])
     71             remove(C[j]);
     72         Dance(k+1);
     73         if(flag)
     74             return ;
     75         for(j=L[i];j!=i;j=L[j])
     76             resume(C[j]);
     77 
     78     }
     79     resume(c);
     80 }
     81 
     82 int main(){
     83 
     84     freopen("input.txt","r",stdin);
     85 
     86     while(~scanf("%d%d",&n,&m)){
     87         int i;
     88         for(i=0;i<=m;i++){
     89             S[i]=0;
     90             D[i]=U[i]=i;
     91             L[i+1]=i;R[i]=i+1;
     92         }
     93         R[m]=0;
     94         size=m+1;
     95         memset(H,-1,sizeof(H));
     96         memset(mark,0,sizeof(mark));
     97         int k,j;
     98         for(i=1;i<=n;i++){
     99             scanf("%d",&k);
    100             while(k--){
    101                 scanf("%d",&j);
    102                 Link(i,j);
    103             }
    104         }
    105         flag=0;
    106         Dance(0);
    107         if(!flag)
    108             printf("NO\n");
    109     }
    110     return 0;
    111 }
  • 相关阅读:
    cookie简介
    Javaweb
    apache-tomcat-7.0.92
    学javaweb 先学Servlet 应用理论很重要
    DataGuard切换(主库为Rac+备库为Rac)
    Oracle 迁移 序列
    oracle SQL 执行进度
    ORA-20011 ORA-29913 KUP-11024 GATHER_TABLE_STATS
    盘点 Oracle 11g 中新特性带来的10大性能影响
    查询近一个月的表空间使用情况
  • 原文地址:https://www.cnblogs.com/jackge/p/3026479.html
Copyright © 2011-2022 走看看