HUST 1017 Exact Cover (Dancing links)

Source: Internet
Author: User

1017-exact cover

Time limit:15 seconds Memory limit:128 MB

Self-assessment 6,110 commits 3,226 times through
Title Description
there is a n*m matrix with only 0s and 1s, (1 <= n,m <= 1000). An exact cover are a selection of rows such that every column have a 1 in exactly one of the selected rows. Try to find out the selected rows.
Input
There is multiply test cases. First line:two integers N, M; The following N Lines:every line first comes a integer C (1 <= C <=), represents the number of 1s in this row, Then comes C integers:the Index of the columns whose value was 1 in this row.
Output
First
output the number of rows in the selection and then output the index of the selected rows. If There is multiply selections, you should just output any of them. If There is no selection, just output "no".
Sample input
6 73 1 4 72 1 43 4 5 73 3 5 64 2 3 6 72 2 7
Sample output
3 2 4 6
Tips
Source
Dupeng

I have a very low IQ, looked at three days to learn, template validation questions.

Incidentally, this is the first a to be formally transferred to C + +.

1#include <iostream>2#include <cstdio>3 using    namespacestd;4 5 Const    intHEAD =0;6 Const    intN =1005;7 intMap[n][n];8 intU[n * n],d[n * n],l[n * n],r[n * n],h[n * n],c[n * n],ans[n *N];9 Ten voidIniintcol); One BOOLDancingintk); A voidOutputvoid); - voidRemoveintc); - voidResumeintc); the intMainvoid) - { -     intN,m,num,col; -     intCount,front,first; +  -      while(Cin >> N >>m) +     { A INI (m); at  -Count = m +1; -          for(inti =1; I <= N;i + +) -         { -CIN >>num; -Front = first =count; in              while(Num--) -             { toCIN >>Col; +  -U[count] =U[col]; theD[count] =Col; *L[count] =Front; $R[count] =First ;Panax Notoginseng  -D[u[col]] =count; theU[col] =count; +R[front] =count; A  theH[count] =i; +C[count] =Col; -Front =count; $Count + +; $             } -L[first] = count-1; -         } the         if(!dancing (1)) -cout <<"NO"<<Endl;Wuyi     } the  -     return    0; Wu } -  About voidIniintCol) $ { -U[head] = D[head] = H[head] = C[head] =HEAD; -R[head] =1; -L[head] =Col; A  +     intFront =HEAD; the      for(inti =1; I <= Col;i + +) -     { $U[i] = D[i] =i; theL[i] =Front; theR[i] =HEAD; theR[front] =i; theFront =i; -  inC[i] =i; theH[i] =0; the     } About } the  the BOOLDancingintk) the { +     intc =R[head]; -     if(c = =HEAD) the     {Bayi output (); the         return    true; the     } -  - Remove (c[c]); the      for(inti = d[c];i! = C;i =D[i]) the     { theANS[K] =H[i]; the          for(intj = r[i];j! = I;j =R[j]) - Remove (c[j]); the         if(Dancing (k +1)) the             return    true; the          for(intj = l[i];j! = I;j =L[j])94 Resume (c[j]); the     } the Resume (c[c]); the 98     return    false; About } - 101 voidOutputvoid)102 {103     inti,j;104      for(i =1; Ans[i];i + +); thecout << I-1<<" ";106      for(j =1; J < I-1; j + +)107cout << Ans[j] <<" ";108cout << Ans[j] <<Endl;109 } the 111 voidRemoveintc) the {113R[L[C]] =R[c]; theL[R[C]] =L[c]; the  the      for(inti = d[c];i! = C;i =D[i])117          for(intj = r[i];j! = I;j =R[j])118         {119D[U[J]] =D[j]; -U[D[J]] =U[j];121         }122 }123 124 voidResumeintc) the {126R[L[C]] =C;127L[R[C]] =C; - 129      for(inti = u[c];i! = C;i =U[i]) the          for(intj = r[i];j! = I;j =R[j])131         { theD[U[J]] =J;133U[D[J]] =J;134         }135}

HUST 1017 Exact Cover (Dancing links)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.