Exact cover (Dancing Links 模板題)
1017 - Exact cover
時間限制:15秒 記憶體限制:128兆
自定評測 5584 次提交 2975 次通過- 題目描述
- 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.
- 輸入
- 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.
- 輸出
- 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".
- 樣例輸入
-
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
- 樣例輸出
-
3 2 4 6
題目連結:http://acm.hust.edu.cn/problem/show/1017
精確覆蓋入門題。
Dancing Links 就是一種加快搜索速度的方法,採用四向連結串列。
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2014/5/25 22:55:25 4 File Name :E:\2014ACM\專題學習\DLX\HUST1017.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8#include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 const int maxnode = 100010; 21 const int MaxM = 1010; 22 const int MaxN = 1010; 23 struct DLX 24 { 25 int n,m,size; 26 int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode]; 27 int H[MaxN], S[MaxM]; 28 int ansd, ans[MaxN]; 29 void init(int _n,int _m) 30 { 31 n = _n; 32 m = _m; 33 for(int i = 0;i <= m;i++) 34 { 35 S[i] = 0; 36 U[i] = D[i] = i; 37 L[i] = i-1; 38 R[i] = i+1; 39 } 40 R[m] = 0; L[0] = m; 41 size = m; 42 for(int i = 1;i <= n;i++) 43 H[i] = -1; 44 } 45 void Link(int r,int c) 46 { 47 ++S[Col[++size]=c]; 48 Row[size] = r; 49 D[size] = D[c]; 50 U[D[c]] = size; 51 U[size] = c; 52 D[c] = size; 53 if(H[r] < 0)H[r] = L[size] = R[size] = size; 54 else 55 { 56 R[size] = R[H[r]]; 57 L[R[H[r]]] = size; 58 L[size] = H[r]; 59 R[H[r]] = size; 60 } 61 } 62 void remove(int c) 63 { 64 L[R[c]] = L[c]; R[L[c]] = R[c]; 65 for(int i = D[c];i != c;i = D[i]) 66 for(int j = R[i];j != i;j = R[j]) 67 { 68 U[D[j]] = U[j]; 69 D[U[j]] = D[j]; 70 --S[Col[j]]; 71 } 72 } 73 void resume(int c) 74 { 75 for(int i = U[c];i != c;i = U[i]) 76 for(int j = L[i];j != i;j = L[j]) 77 ++S[Col[U[D[j]]=D[U[j]]=j]]; 78 L[R[c]] = R[L[c]] = c; 79 } 80 //d為遞迴深度 81 bool Dance(int d) 82 { 83 if(R[0] == 0) 84 { 85 ansd = d; 86 return true; 87 } 88 int c = R[0]; 89 for(int i = R[0];i != 0;i = R[i]) 90 if(S[i] < S[c]) 91 c = i; 92 remove(c); 93 for(int i = D[c];i != c;i = D[i]) 94 { 95 ans[d] = Row[i]; 96 for(int j = R[i]; j != i;j = R[j])remove(Col[j]); 97 if(Dance(d+1))return true; 98 for(int j = L[i]; j != i;j = L[j])resume(Col[j]); 99 } 100 resume(c); 101 return false; 102 } 103 }; 104 105 DLX g; 106 int main() 107 { 108 //freopen("in.txt","r",stdin); 109 //freopen("out.txt","w",stdout); 110 int n,m; 111 while(scanf("%d%d",&n,&m) == 2) 112 { 113 g.init(n,m); 114 for(int i = 1;i <= n;i++) 115 { 116 int num,j; 117 scanf("%d",&num); 118 while(num--) 119 { 120 scanf("%d",&j); 121 g.Link(i,j); 122 } 123 } 124 if(!g.Dance(0))printf("NO\n"); 125 else 126 { 127 printf("%d",g.ansd); 128 for(int i = 0;i < g.ansd;i++) 129 printf(" %d",g.ans[i]); 130 printf("\n"); 131 } 132 } 133 return 0; 134 }
相關推薦
Exact cover (Dancing Links 模板題)
1017 - Exact cover 時間限制:15秒 記憶體限制:128兆 自定評測 5584 次提交 2975 次通過 題目描述There is an
HDU 2222 Keywords Search(AC自動機模板題)
stack uil empty cst keywords cto ble ont max http://acm.hdu.edu.cn/showproblem.php?pid=2222 題意:給出多個單詞,最後再給出一個模式串,求在該模式串中包含了多少個單詞。 思路
hdu 4347 The Closest M Points (kd tree 模板題)
題目:http://acm.hdu.edu.cn/showproblem.php?pid=4347 #include<bits/stdc++.h> using namespace std; #define sq(x) (x)*(x) const int maxn=6e4+10;
HDU 2063 - 過山車 (二分匹配模板題)
RPG girls今天和大家一起去遊樂場玩,終於可以坐上夢寐以求的過山車了。可是,過山車的每一排只有兩個座位,而且還有條不成文的規矩,就是每個女生必須找個個男生做partner和她同坐。但是,每個女孩都有各自的想法,舉個例子把,Rabbit只願意和XHD或PQK做partner,Grass只願意和l
HDU-3695 Computer Virus on Planet Pandora(ac自動機模板題)
Aliens on planet Pandora also write computer programs like us. Their programs only consist of capital letters (‘A’ to ‘Z’) which th
bzoj1012(線段樹模板題)
題目讀了三遍才讀懂,對於蒟蒻來說,看上去很難得樣子,實際就是線段樹的單點更新,然後求區間最值,無奈之前還想著怎麼建樹插進去。。結果RE n次,感覺自己水的一匹,唉~ 做題效率極低。。。。 真是被自己蠢哭了 #include<iostream> #i
HDU2222(AC自動機模板題)
咳咳~ 因為暫時看不懂,所以先儲存個模板題吧。 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total
HDU 2544 最短路(dijkstra演算法模板題)
Problem Description 在每年的校賽裡,所有進入決賽的同學都會獲得一件很漂亮的t-shirt。但是每當我們的工作人員把上百件的衣服從商店運回到賽場的時候,卻是非常累的!所以現在他們想要尋找最短的從商店到賽場的路線,你可以幫助他們嗎?
HDU 1166 敵兵佈陣(線段樹模板題)
敵兵佈陣 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 127082 Accepted Submissi
POJ 3255 Roadblocks(次短路模板題)
http://poj.org/problem?id=3255 寫了還算多的最短路題目發現,沒寫過次短路。順著挑戰刷,就看到了次短路,然後發現一臉懵逼。 其實理解了後發現超級簡單。次短路,每個點都記錄兩個距離就好了,最短的和次短的。然後最後輸出第n個點的次短的即可。 程式碼如
POJ - 3740 Easy Finding (Dance link 模板題)
題意: 很裸的舞蹈鏈。 分析: 舞蹈鏈板題。 #include<cstdio> using namespace std; const int maxn=20*400; struct DLX{ int n,m,size; int U[max
hdu 2665 Kth number(劃分樹模板題)
題意:給定一個長度為n的序列,進行m次查詢,求出區間[l,r]中的第k大值。 思路:劃分樹模板題。上學的時候做過這道題,當時看了下劃分樹的講解,看得很頭大,然後就一直放著了。十一回家的時候在高鐵上沒什麼事情,就重新學習了一遍劃分樹。 劃分樹是通過模擬快速排序,記錄快速排序的
1631-Bridging signals (LIS二分模板題)
‘Oh no, they’ve done it again’, cries the chief designer at the Waferland chip factory. Once more the routing designers have screwe
[poj2823]sliding windows(單調佇列模板題)
DescriptionAn array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array t
UVa 11149 矩陣的冪(矩陣倍增法模板題)
ble 化簡 .cn target ans txt put std net https://vjudge.net/problem/UVA-11149 題意: 輸入一個n×n矩陣A,計算A+A^2+A^3+...A^k的值。 思路: 矩陣倍增法。
【POJ3074】Sudoku DLX(Dancing Links)
puts struct pre i++ 能夠 ring include 為什麽 處理 數獨就要DLX,不然不樂意。 數獨的DLX構造:9*9個點每一個點有9種選擇,這構成了DLX的729行,每行、列、陣有限制,均為9行(/列/陣),然後每行(/列/陣)都有九
【POJ3740】Easy Finding DLX(Dancing Links)精確覆蓋問題
ren .cn string 應該 進行 int 函數 操作 urn 題意:多組數據。每組數據給你幾行數,要求選出當中幾行,使得每一列都有且僅有一個1,詢問是可不可行,或者說能不能找出來。 題解:1、暴搜。2、DLX(Dancing links)。 本文寫的是DLX。算
hdu1115 Lifting the Stone(幾何,求多邊形重心模板題)
class str math i++ clu pull area this net 轉載請註明出處:http://blog.csdn.net/u012860063 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1115
HDU 3068 最長回文(manacher模板題)
style hdu log pre using ret algo names print 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 題目大意:求字符串s中最長的回文子串 解題思路:manacher模板 代
Unknown Treasure (盧卡斯 + 孫子定理, 模板題)
std 參考 模板題 family can 組合數 typedef www. class Unknown Treasure 參考鏈接 : https://www.cnblogs.com/linyujun/p/5199684.html 盧卡斯定理 : C(n, m) %