Dancing Link---Template title HUST 1017-exact cover

Source: Internet
Author: User

1017-exact Cover problem ' s Link: http://acm.hust.edu.cn/problem/show/1017

Mean:

Slightly

Analyse:

A

Time Complexity:o (n)

Source Code:

#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<time.h>using namespacestd;Const intMaxnode =100010;Const intMAXN =1010;structdlx{intn,m,size; intU[maxnode],d[maxnode],r[maxnode],l[maxnode],row[maxnode],col[maxnode]; intH[MAXN], S[MAXN];//H[i]---the first row i is 1 index s[i]---Number i column 1    intANSD, ANS[MAXN]; voidInitint_n,int_m) {N=_n; M=_m;  for(inti =0; I <= m;i++)//Initialize the first row (c[in the figure]){S[i]=0;//number of 1 in column IU[i] = D[i] =i; L[i]= I1; R[i]= i+1; } R[m]=0; l[0] = m;//the last of the first line points to the first line (looping)size = m;//from M onwards, it's all common knots .         for(inti =1; I <= n;i++) H[i]= -1;//H[i]---The first 1 node number on line I    }    voidLink (intRintC//ranks    {        //D[c]---The next pointer in column Cs[col[++size]=c]++;//Normal node subscript + + The number of columns in the size node is the number of nodes in C column C + +Row[size] = r;//the number of rows for the first size node is R .D[size] = D[c];//The next pointer to the size node is: The next pointer to column No. 0, Row CU[size] = c;//The pointer on the first size node is: column No. 0 (only if the input row is incremented)U[D[C]] = size;//The pointer on column C of line No. 0 is: sizeD[C] = size;//The next pointer to the size above is: size (a bit around)        if(H[r] <0) H[r] = l[size] = r[size] = size;//the line has only one node around the pointer itself pointing itself        Else{R[size]= R[h[r]];//LoopingL[r[h[r]] =size; L[size]=H[r]; R[H[R]]=size; }    }    voidRemoveintc) {L[r[c]]= L[c]; R[L[C]] =R[c];  for(inti = d[c];i! = C;i =D[i]) for(intj = r[i];j! = I;j =R[j]) {U[d[j]]=U[j]; D[U[J]]=D[j]; --S[col[j]]; }    }    voidResumeintc) { for(inti = u[c];i! = C;i =U[i]) for(intj = l[i];j! = I;j =L[j])++s[col[u[d[j]]=d[u[j]]=J]]; L[R[C]]= R[l[c]] =C; }    //d is recursive depth    BOOLDance (intd) {if(r[0] ==0)//R[0]==r[m]//There's no knot in line No. 0 .{ANSD=D; return true; }        intc = r[0];  for(inti = r[0];i! =0; i = R[i])//go right (Find a column with the fewest nodes)            if(S[i] < s[c])//Column I node number < column C node numberc =i; Remove (c); //Remove C node         for(inti = d[c];i! = C;i = D[i])//find the smallest of these columns and go down.{Ans[d]=Row[i];  for(intj = R[i]; J! = I;j = R[j]) remove (col[j]);//remove the column corresponding to the row            if(Dance (d+1))return true;//recursive next layer             for(intj = L[i]; J! = I;j = L[j]) resume (col[j]);//Backwards Connection} resume (c); return false; }};D lx G;intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);    intn,m;  while(SCANF ("%d%d", &n,&m) = =2) {g.init (n,m);  for(inti =1; I <= n;i++)//Line        {            intnum,j; scanf ("%d",&num);  while(num--) {scanf ("%d", &j);//columnG.link (I,J); }        }        if(!g.dance (0)) printf ("no\n"); Else{printf ("%d", G.ANSD);  for(inti =0; I < g.ansd;i++) printf ("%d", G.ans[i]); printf ("\ n"); }    }    return 0;}
View Code

Blog Links:

Http://www.cnblogs.com/grenet/p/3145800.html

Dancing Link---Template title HUST 1017-exact cover

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.