-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvol2.tex
More file actions
8903 lines (8644 loc) · 209 KB
/
Copy pathvol2.tex
File metadata and controls
8903 lines (8644 loc) · 209 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[titlepage,a4paper,14pt]{book}
\usepackage[top=2.54cm, bottom=2.54cm, left=3.18cm, right=3.18cm]{geometry}
\usepackage{ctex}
\usepackage{ccmap}
\usepackage{pdfpages}
\usepackage{amsthm}
\usepackage{longtable}
\usepackage{amssymb}
\usepackage{subfigure}
\usepackage{verbatim}
\newcommand{\tabincell}[2]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{cases}%必须放amsmath下面,否则会冲突
\usepackage[colorlinks,bookmarksnumbered=true,bookmarksopen=true,CJKbookmarks=true,linkcolor=black,anchorcolor=black,citecolor=black]{hyperref}
\usepackage{listings}
\usepackage{titlesec}
\usepackage[title,titletoc]{appendix}
\usepackage[svgnames,x11names]{xcolor}
\usepackage[titles,subfigure]{tocloft}
\lstset{tabsize=4, %
frame=single, %把代码用带有阴影的框圈起来
keywordstyle=\color[rgb]{0,0,1},
commentstyle=\color[rgb]{0.133,0.545,0.133},
stringstyle=\color[rgb]{0.627,0.126,0.941},
%commentstyle=\color{red!50!green!50!blue!50},%浅灰色的注释
rulesepcolor=\color{red!20!green!20!blue!20},%代码块边框为淡青色
%keywordstyle=\color{blue!90}\bfseries, %代码关键字的颜色为蓝色,粗体
showstringspaces=false,%不显示代码字符串中间的空格标记
stringstyle=\ttfamily, % 代码字符串的特殊格式
keepspaces=true, %
breakindent=22pt, %
numbers=left,%左侧显示行号
stepnumber=1,%
numberstyle=\footnotesize, %行号字体用小号
basicstyle=\footnotesize, %
showspaces=false, %
flexiblecolumns=true, %
breaklines=true, %对过长的代码自动换行
breakautoindent=true,%
breakindent=4em, %
escapebegin=\begin{CJK*}{GBK}{hei},escapeend=\end{CJK*},
aboveskip=1em, %代码块边框
%% added by http://bbs.ctex.org/viewthread.php?tid=53451
fontadjust,
captionpos=t,
framextopmargin=2pt,framexbottommargin=2pt,abovecaptionskip=-3pt,belowcaptionskip=3pt,
xleftmargin=4em,xrightmargin=4em, % 设定listing左右的空白
texcl=true,
% 设定中文冲突,断行,列模式,数学环境输入,listing数字的样式
extendedchars=false,columns=flexible,mathescape=true
% numbersep=-1em
}
\def\CU@definezihao#1#2#3{
\def#1{\fontsize{#2}{#3}\selectfont}}
\CU@definezihao{\zihaochu}{42}{50}
\CU@definezihao{\zihaoxiaochu}{36}{44}
\CU@definezihao{\zihaoyi}{28}{34}
\CU@definezihao{\zihaoer}{22}{26}
\CU@definezihao{\zihaoxiaoer}{18}{22}
\CU@definezihao{\zihaosan}{15.7}{19}
\CU@definezihao{\zihaosi}{14}{17}
\CU@definezihao{\zihaoxiaosi}{12}{14}
\CU@definezihao{\zihaowu}{10.5}{12}
\CU@definezihao{\zihaoxiaowu}{9}{11}
\CU@definezihao{\zihaoliu}{7.875}{9}
\CU@definezihao{\zihaoqi}{5.25}{6}
\titleformat{\section}{\zihaosi\bfseries}{$\S\;$\thesection}{1em}{}
\titleformat{\subsection}{\zihaoxiaosi\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}{\zihaoxiaosi\bfseries}{\thesubsubsection}{1em}{}
\renewcommand{\figurename}{\zihaowu{Figure}}
\makeatletter
\newcommand\figcaption{\def\@captype{figure}\caption}
\newcommand\tabcaption{\def\@captype{table}\caption}
\makeatother
%\pagestyle{headings}
\usepackage{fancyhdr}
\usepackage{lastpage}
\pagestyle{fancy}
%\renewcommand{\headrulewidth}{1pt} %页眉线宽,设为0可以去页眉线
%\lhead{{TEAM \#\emph{xiaohai}}}
%\chead{}
%\rhead{\leftmark}
%\lfoot{}
%\cfoot{}
%\rfoot{Page \thepage\ of \pageref{LastPage}}
\fancyhead{}
\fancyhead[LO,RE]{{TEAM \#\emph{xiaohai}}}
\fancyhead[LE,RO]{\leftmark}
\fancyfoot{}
\fancyfoot[RO,LE]{Page \thepage\ of \pageref{LastPage}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{part}{3}
\setcounter{chapter}{14}
\begin{document}
\thispagestyle{empty}
\begin{title}
\
\begin{center}
\ \\ \ \\ \ \\ \ \\ \ \\ \ \\
\textbf{\zihaoxiaoer{The Code Library of xiaohai}}\\
\zihaoxiaosi{\textbf{Version 2.0}}\\
\zihaosi{\textbf{Volume 2 (Part IV, V)}}
\ \\
\ \\
\zihaosi{\textbf{xiaohai}}\\
\zihaosi{\emph{Lingxiao Ma\ \ \ \ \ Yi Li\ \ \ \ \ Lanjun Duan\ \ \ \ \ Anran Li}}\\
\ \\
\zihaosi{College of Imformation Science and Technology}\\
\zihaosi{Beijing Normal University}
\ \\ \ \\ \ \\ \ \\
\end{center}
\begin{center}
\includegraphics[width=300 pt]{title.png}
\end{center}
\begin{center}
\ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\
\zihaosi{\today}
\end{center}
\end{title}
\thispagestyle{empty}
\newpage
\ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\
\ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\
\ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\ \ \\
\textbf{Copyright \copyright\ 2012-2014 {TEAM \#\emph{xiaohai}}}
\thispagestyle{empty}
\newpage
%\title{The Code Library of xiaohai}
%\author{\textbf{xiaohai} \\ \emph{xysmlx\ \ \ \ \ liyi\ \ \ \ \ lianran} \\ \\ College of Imformation Science and Technology \\ Beijing Normal University\\
%}
%\maketitle
%\thispagestyle{fancy}
\tableofcontents
%\thispagestyle{fancy}
\newpage
\part{Algorithm}
\chapter{Graph Theory}%building
\section{Theorm}
\begin{itemize}
\item 将一个树连成双连通分量至少需要的边 $=$ (叶子节点数+1)/2
\item $n$顶点$k$条边的图至少有$n-k$个连通分量
\item 如果一个图有一条不是环的边,则它至少有2个顶点不是割点
\item 一个图是二分图是不存在奇环的充要条件,用交叉染色法判断是不是二分图
\item $n$个顶点,则有$n^2$个有序对,有$2^{n^2}$个简单有向图,有$3^{\binom{n}{2}}$ 个定向图(简单图的定向),有$2^{\binom{n}{2}}$ 个竞赛图(完全图的定向)
\item //
\end{itemize}
\section{图数据结构}
\subsection{链式前向星}
\begin{verbatim}
/**
*链式前向星
*/
const int maxn=0;
const int maxm=0;
struct Edge
{
int u,v;
int w;
int next;
}edge[maxm];
int head[maxn],edgeNum;
void addSubEdge(int u,int v,int w)
{
edge[edgeNum].u=u;
edge[edgeNum].v=v;
edge[edgeNum].w=w;
edge[edgeNum].next=head[u];
head[u]=edgeNum++;
}
void addEdge(int u,int v,int w)
{
addSubEdge(u,v,w);
addSubEdge(v,u,w);
}
void init()
{
memset(head,-1,sizeof(head));
edgeNum=0;
}
/*扫描*/
for(int i=head[u];i!=-1;i=edge[i].next)
{
//内容
}
\end{verbatim}
\section{搜索}
\subsection{Dancing Links}
\subsubsection{精确覆盖}
\paragraph{精确覆盖}
在一个全集$X$中若干子集的集合为$S$,精确覆盖是指,$S$的子集$S*$,满足$X$中的每一个元素在$S*$中\textbf{恰好}出现一次。
\paragraph{精确覆盖DLX模板}
\begin{verbatim}
const int maxnode = 100010;
const int maxr = 1010;
const int maxc = 1010;
struct DLX
{
int n, m, size;
int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
int H[maxc], S[maxr];
int ansd, ans[maxc];
void init(int _n, int _m)
{
n = _n;
m = _m;
for (int i = 0; i <= m; i++)
{
S[i] = 0;
U[i] = D[i] = i;
L[i] = i - 1;
R[i] = i + 1;
}
R[m] = 0; L[0] = m;
size = m;
for (int i = 1; i <= n; i++)
H[i] = -1;
}
void Link(int r, int c)
{
++S[Col[++size] = c];
Row[size] = r;
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size;
if (H[r] < 0)H[r] = L[size] = R[size] = size;
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
void remove(int c)
{
L[R[c]] = L[c]; R[L[c]] = R[c];
for (int i = D[c]; i != c; i = D[i])
for (int j = R[i]; j != i; j = R[j])
{
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[Col[j]];
}
}
void resume(int c)
{
for (int i = U[c]; i != c; i = U[i])
for (int j = L[i]; j != i; j = L[j])
++S[Col[U[D[j]] = D[U[j]] = j]];
L[R[c]] = R[L[c]] = c;
}
//d为递归深度
bool Dance(int d)
{
if (R[0] == 0)
{
ansd = d;
return true;
}
int c = R[0];
for (int i = R[0]; i != 0; i = R[i])
if (S[i] < S[c])
c = i;
remove(c);
for (int i = D[c]; i != c; i = D[i])
{
ans[d] = Row[i];
for (int j = R[i]; j != i; j = R[j])remove(Col[j]);
if (Dance(d + 1))return true;
for (int j = L[i]; j != i; j = L[j])resume(Col[j]);
}
resume(c);
return false;
}
};
\end{verbatim}
\paragraph{例:HUST 1017}
\begin{verbatim}
/*
样例:HUST 1017
*/
#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 namespace std;
const int maxnode = 100010;
const int MaxM = 1010;
const int MaxN = 1010;
struct DLX
{
int n, m, size;
int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];
int H[MaxN], S[MaxM];
int ansd, ans[MaxN];
void init(int _n, int _m)
{
n = _n;
m = _m;
for (int i = 0; i <= m; i++)
{
S[i] = 0;
U[i] = D[i] = i;
L[i] = i - 1;
R[i] = i + 1;
}
R[m] = 0; L[0] = m;
size = m;
for (int i = 1; i <= n; i++)
H[i] = -1;
}
void Link(int r, int c)
{
++S[Col[++size] = c];
Row[size] = r;
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size;
if (H[r] < 0)H[r] = L[size] = R[size] = size;
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
void remove(int c)
{
L[R[c]] = L[c]; R[L[c]] = R[c];
for (int i = D[c]; i != c; i = D[i])
for (int j = R[i]; j != i; j = R[j])
{
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[Col[j]];
}
}
void resume(int c)
{
for (int i = U[c]; i != c; i = U[i])
for (int j = L[i]; j != i; j = L[j])
++S[Col[U[D[j]] = D[U[j]] = j]];
L[R[c]] = R[L[c]] = c;
}
//d为递归深度
bool Dance(int d)
{
if (R[0] == 0)
{
ansd = d;
return true;
}
int c = R[0];
for (int i = R[0]; i != 0; i = R[i])
if (S[i] < S[c])
c = i;
remove(c);
for (int i = D[c]; i != c; i = D[i])
{
ans[d] = Row[i];
for (int j = R[i]; j != i; j = R[j])remove(Col[j]);
if (Dance(d + 1))return true;
for (int j = L[i]; j != i; j = L[j])resume(Col[j]);
}
resume(c);
return false;
}
};
DLX g;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n, m;
while (scanf("%d%d", &n, &m) == 2)
{
g.init(n, m);
for (int i = 1; i <= n; i++)
{
int num, j;
scanf("%d", &num);
while (num--)
{
scanf("%d", &j);
g.Link(i, j);
}
}
if (!g.Dance(0))printf("NO\n");
else
{
printf("%d", g.ansd);
for (int i = 0; i < g.ansd; i++)
printf(" %d", g.ans[i]);
printf("\n");
}
}
return 0;
}
\end{verbatim}
\subsubsection{重复覆盖}
\paragraph{重复覆盖}
在一个全集$X$中若干子集的集合为$S$,重复覆盖是指,$S$的子集$S*$,满足$X$中的每一个元素在$S*$中\textbf{至少}出现一次。
\paragraph{重复覆盖DLX模板}
\begin{verbatim}
/*
重复覆盖:DLX
输入:Link()
输出:ans, bool Dance(int k)
*/
const int maxnode = 360000;
const int maxc = 500;
const int maxr = 500;
const int inf = 0x3f3f3f3f;
struct DLX
{
int L[maxnode], R[maxnode], D[maxnode], U[maxnode], C[maxnode];
int S[maxc], H[maxr], size;
int ans;
///不需要S域
void Link(int r, int c)
{
S[c]++; C[size] = c;
U[size] = U[c]; D[U[c]] = size;
D[size] = c; U[c] = size;
if (H[r] == -1) H[r] = L[size] = R[size] = size;
else
{
L[size] = L[H[r]]; R[L[H[r]]] = size;
R[size] = H[r]; L[H[r]] = size;
}
size++;
}
void remove(int c)
{
for (int i = D[c]; i != c; i = D[i])
L[R[i]] = L[i], R[L[i]] = R[i];
}
void resume(int c)
{
for (int i = U[c]; i != c; i = U[i])
L[R[i]] = R[L[i]] = i;
}
int h() ///用精确覆盖去估算剪枝
{
int ret = 0;
bool vis[maxc];
memset (vis, false, sizeof(vis));
for (int i = R[0]; i; i = R[i])
{
if (vis[i])continue;
ret++;
vis[i] = true;
for (int j = D[i]; j != i; j = D[j])
for (int k = R[j]; k != j; k = R[k])
vis[C[k]] = true;
}
return ret;
}
//根据具体问题选择限制搜索深度或直接求解。
bool Dance(int k)
{
if (k + h() >= ans) return 0;
if (!R[0])
{
if (k < ans)ans = k;
return 1;
}
int c = R[0];
for (int i = R[0]; i; i = R[i])
if (S[i] < S[c])c = i;
for (int i = D[c]; i != c; i = D[i])
{
remove(i);
for (int j = R[i]; j != i; j = R[j])
remove(j);
Dance(k + 1);
for (int j = L[i]; j != i; j = L[j])
resume(j);
resume(i);
}
return 0;
}
void initL(int x) ///col is 1~x,row start from 1
{
for (int i = 0; i <= x; ++i)
{
S[i] = 0;
D[i] = U[i] = i;
L[i + 1] = i; R[i] = i + 1;
}///对列表头初始化
R[x] = 0;
size = x + 1; ///真正的元素从m+1开始
memset (H, -1, sizeof(H));
///mark每个位置的名字
}
};
\end{verbatim}
\paragraph{例:POJ 1084 Square Destroyer}
\begin{verbatim}
/*
1、题意:给你一个n*n(n<=5)的完全由火柴棍组成的正方形,已经去掉了一些火柴棍,问最少去掉多少根火柴棍使得所有1*1、2*2......n*n的正方形均被破坏掉?
2、方法:矩阵的一行代表一根火柴棍,矩阵的一列代表一个正方形
3、处理去掉的火柴棍:先计算不去掉火柴棍的矩阵,对去掉的火柴棍对应的正方形加标记并在DLX里面标记它们已经访问过,然后在添加link时忽略这些标记过的正方形
*/
// #pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <list>
#include <map>
#include <iterator>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long LL;
#define pb push_back
const int maxn = 110;
const int inf = 0x3f3f3f3f;
const int maxnode = 360000;
const int maxc = 500;
const int maxr = 500;
// const int inf = 0x3f3f3f3f;
struct DLX
{
int L[maxnode], R[maxnode], D[maxnode], U[maxnode], C[maxnode];
int S[maxc], H[maxr], size;
int ans;
///不需要S域
void Link(int r, int c)
{
S[c]++; C[size] = c;
U[size] = U[c]; D[U[c]] = size;
D[size] = c; U[c] = size;
if (H[r] == -1) H[r] = L[size] = R[size] = size;
else
{
L[size] = L[H[r]]; R[L[H[r]]] = size;
R[size] = H[r]; L[H[r]] = size;
}
size++;
}
void remove(int c)
{
for (int i = D[c]; i != c; i = D[i])
L[R[i]] = L[i], R[L[i]] = R[i];
}
void resume(int c)
{
for (int i = U[c]; i != c; i = U[i])
L[R[i]] = R[L[i]] = i;
}
int h() ///用精确覆盖去估算剪枝
{
int ret = 0;
bool vis[maxc];
memset (vis, false, sizeof(vis));
for (int i = R[0]; i; i = R[i])
{
if (vis[i])continue;
ret++;
vis[i] = true;
for (int j = D[i]; j != i; j = D[j])
for (int k = R[j]; k != j; k = R[k])
vis[C[k]] = true;
}
return ret;
}
//根据具体问题选择限制搜索深度或直接求解。
bool Dance(int k)
{
if (k + h() >= ans) return 0;
if (!R[0])
{
if (k < ans)ans = k;
return 1;
}
int c = R[0];
for (int i = R[0]; i; i = R[i])
if (S[i] < S[c])c = i;
for (int i = D[c]; i != c; i = D[i])
{
remove(i);
for (int j = R[i]; j != i; j = R[j])
remove(j);
Dance(k + 1);
for (int j = L[i]; j != i; j = L[j])
resume(j);
resume(i);
}
return 0;
}
void initL(int x) ///col is 1~x,row start from 1
{
for (int i = 0; i <= x; ++i)
{
S[i] = 0;
D[i] = U[i] = i;
L[i + 1] = i; R[i] = i + 1;
}///对列表头初始化
R[x] = 0;
size = x + 1; ///真正的元素从m+1开始
memset (H, -1, sizeof(H));
///mark每个位置的名字
}
} dlx;
int kase;
int n;
vector<int> vec;
bool mtx[maxn][maxn];
int row, col;
bool vis[maxn];
void init()
{
kase++;
vec.clear();
memset(mtx, 0, sizeof(mtx));
memset(vis, 0, sizeof(vis));
}
void input()
{
scanf("%d", &n);
int k;
scanf("%d", &k);
while (k--)
{
int x;
scanf("%d", &x);
vec.pb(x);
}
}
void debug()
{
//
}
void calmtx()
{
row = 2 * n * (n + 1);
col = 0;
for (int i = 1; i <= n; i++)
col += i * i;
int cnt = 1;
for (int si = 1; si <= n; si++)
{
for (int i = 1; i <= n - si + 1; i++)
{
for (int j = 1; j <= n - si + 1; j++)
{
for (int k = 0; k < si; k++)
{
mtx[(i - 1) * (2 * n + 1) + j + k][cnt] = 1;
mtx[(i - 1 + si) * (2 * n + 1) + j + k][cnt] = 1;
mtx[i * n + (i - 1) * (n + 1) + j + k * (2 * n + 1)][cnt] = 1;
mtx[i * n + (i - 1) * (n + 1) + j + k * (2 * n + 1) + si][cnt] = 1;
}
cnt++;
}
}
}
}
void build()
{
calmtx();
dlx.initL(col);
for (int i = 0; i < vec.size(); i++)
{
int x = vec[i];
for (int j = 1; j <= col; j++)
if (mtx[x][j] && !vis[j])
{
vis[j] = 1;
dlx.R[dlx.L[j]] = dlx.R[j];
dlx.L[dlx.R[j]] = dlx.L[j];
dlx.R[j] = dlx.L[j] = 0;
}
}
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (mtx[i][j] && !vis[j])
dlx.Link(i, j);
}
}
}
void solve()
{
build();
dlx.ans = inf;
dlx.Dance(0);
printf("%d\n", dlx.ans);
}
void output()
{
//
}
int main()
{
#ifdef xysmlx
freopen("in.cpp", "r", stdin);
#endif
kase = 0;
int T;
scanf("%d", &T);
while (T--)
{
init();
input();
solve();
output();
}
return 0;
}
\end{verbatim}
\subsubsection{DLX标记已经提前选取过的列}
\begin{verbatim}
dlx.R[dlx.L[j]] = dlx.R[j];
dlx.L[dlx.R[j]] = dlx.L[j];
dlx.R[j] = dlx.L[j] = 0;
\end{verbatim}
\section{基本图算法}
\subsection{判断是否存在奇环、是否是二分图:交叉染色法($O(V+E)$)}
\begin{verbatim}
/**
*判断是否存在奇环、是否是二分图:交叉染色法($O(V+E)$)
*使用方法:找一个连通分量用一次(因为fflag[])
*输入:图(链式前向星),now,fflag[](是否在同一连通分量)
*输出:true(是奇环、不是二分图),false(是二分图、不是奇环)
*/
const int maxn=0;
bool fflag[maxn];
bool toColor(int now)
{
queue<int> Q;
int color[maxn];
memset(color,0,sizeof(color));
color[now]=1;
Q.push(now);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!fflag[v]) continue;/* 不是同一连通分量*/
if(!color[v])
{
color[v]=-color[u];
Q.push(v);
}
else if(color[v] == color[u]) return true;
}
}
return false;
}
\end{verbatim}
\subsection{2-SAT 问题($O(V+E)$)}
\subsubsection{理论}
\paragraph{建图}
把所有的输入都转化为$(x_1||y_1) \&\& (x_2||y_2) \&\& \cdots \&\& (x_i||y_i)\cdots$的形式:那么$(xi||yi)$ 就可以转化为$!xi->yi$,和$!yi->xi$两条边。
具体转化过程:$a \&\& b = (a || b) \&\& (!a || b) \&\& (!b || a) $
\begin{enumerate}
\item \textbf{$a\ AND\ b = 1$: } 这个等价于$(a || b) \&\& (!a || b) \&\& (!b || a)$,于是在图中增加六条边 $!a->b\ ,\ !b->a\ ,\ a->b\ ,\ !b->!a\ ,\ b->a\ ,\ !a->!b$
\item \textbf{$a\ AND\ b = 0$: } 这个等价于 $!a || !b$,于是在原图中增加两条边 $a->!b\ ,\ b->!a$
\item \textbf{$a\ OR\ b = 0$: }这个等价于$(!a || !b) \&\& (!a || b) \&\& (!b || a)$,于是在图中增加六条边 $a->!b\ ,\ b->!a\ ,\ a->b\ ,\ !b->!a\ ,\ b->a\ ,\ !a->!b$
\item \textbf{$a\ OR\ b = 1$: }这个等价于 $a || b$,于是在图中增加两条边 $!a->b\ ,\ !b->a$
\item \textbf{$a\ XOR\ b = 0$: } 这个等价于$(a || !b) \&\& (!a || b)$,于是在图中增加四条边 $!a->!b\ ,\ b->a\ ,\ a->b\ ,\ !b->!a$
\item \textbf{$a\ XOR\ b 1$: }这个等价于$(a || b) \&\& (!a || !b)$,于是在图中增加四条边 $!a->b\ ,\ !b->a\ ,\ a->!b\ ,\ b->!a$
\item \textbf{$a\ ->\ b$: }这个等价于$(a -> b) \&\& (!b -> !a)$,于是在图中增加两条边 $a->b\ ,\ !b->!a$
\item \textbf{$a\ =\ 0$: } 这个等价于加边$!a->a$
\item \textbf{$a\ =\ 1$: } 这个等价于加边$a->!a$
\end{enumerate}
\paragraph{可行性判定}检查所有的变量a:!a和a不能够在同一个连通分量中,否则无解.
\paragraph{构造解}强连通分量缩点,在DAG图中按照逆拓扑序,依次选择,每一次选择,同时把和被选择点矛盾矛盾的点及其连通分量标记为不选,直到所有的点都作出了选择.
\subsubsection{实现}
\paragraph{Modified Edition of LRJ}
\begin{verbatim}
/**
*2-SAT模板,Modified Edition of LRJ 按字典序排列结果
*输入:按照法则添加边(参数为2*i或者2*i+1)
*运行:先init(n),再add(),再solve()
*注意:add(2*i,2*j)才行
*输出:mark[](1表示选中),solve()(是否有解)
*/
const int maxn = 0;
struct TwoSAT
{
int n;
vector<int> G[maxn*2];
bool mark[maxn*2];
int S[maxn*2], c;
bool dfs(int x)
{
if (mark[x^1]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x;
for (int i = 0; i < G[x].size(); i++)
if (!dfs(G[x][i])) return false;
return true;
}
void init(int n)
{
this->n = n;
for (int i = 0; i < n*2; i++) G[i].clear();
memset(mark, 0, sizeof(mark));
}
/// x AND y = 1
void add_and_one(int x,int y)
{
G[x^1].push_back(y);
G[y^1].push_back(x);
G[x].push_back(y);
G[y^1].push_back(x^1);
G[y].push_back(x);
G[x^1].push_back(y^1);
}
/// x AND y = 0
void add_and_zero(int x,int y)
{
G[x].push_back(y^1);
G[y].push_back(x^1);
}
/// x OR y = 1
void add_or_one(int x,int y)
{
G[x^1].push_back(y);
G[y^1].push_back(x);
}
/// x OR y = 0
void add_or_zero(int x,int y)
{
G[x].push_back(y^1);
G[y].push_back(x^1);
G[x].push_back(y);
G[y^1].push_back(x^1);
G[x^1].push_back(y^1);
G[y].push_back(x);
}
/// x XOR y = 1
void add_xor_one(int x,int y)
{
G[x^1].push_back(y);
G[y^1].push_back(x);
G[x].push_back(y^1);
G[y].push_back(x^1);
}
/// x XOR y = 0
void add_xor_zero(int x,int y)
{
G[x^1].push_back(y^1);
G[y].push_back(x);
G[x].push_back(y);
G[y^1].push_back(x^1);
}
/// x -> y
void add_to(int x,int y)
{
G[x].push_back(y);
G[y^1].push_back(x^1);
}
bool solve()
{
for(int i = 0; i < n*2; i += 2)
if(!mark[i] && !mark[i+1])
{
c = 0;
if(!dfs(i))
{
while(c > 0) mark[S[--c]] = false;
if(!dfs(i+1)) return false;
}
}
return true;
}
};
\end{verbatim}
\paragraph{链式前向星写法}
\begin{verbatim}
/**
*2-SAT模板: 按字典序排列结果
*输入:按照法则添加边(参数为2*i或者2*i+1)
*运行:先init(n),再add(),再solve()
*注意:add(2*i,2*j)才行
*输出:vis[](1表示选中),solve()(是否有解)
*/
const int maxn = 0;
const int maxm = 0;
struct Edge
{
int u, v;
int next;
};
struct TwoSAT
{
int n, en;
Edge edge[maxm];
int head[maxn];
int vis[maxn], S[maxn];
int cnt;
void init(int _n = 0)
{
n = _n;
memset(head, -1, sizeof(head));
en = 0;
memset(vis, 0, sizeof(vis));
}
void addse(int u, int v)
{
edge[en].u = u;
edge[en].v = v;
edge[en].next = head[u];
head[u] = en++;
}
bool dfs(int u)
{
if (vis[u ^ 1])return 0;
if (vis[u])return 1;
vis[u] = 1;
S[cnt++] = u;
for (int i = head[u]; i != -1; i = edge[i].next)
{
if (!dfs(edge[i].v))return 0;
}
return 1;
}
bool solve()
{
for (int i = 0; i < 2 * n; i += 2)
{
if (vis[i] || vis[i ^ 1])continue;
cnt = 0;
if (!dfs(i))
{
while (cnt)vis[S[--cnt]] = 0;
if (!dfs(i ^ 1))return 0;
}
}
return 1;
}
/// x AND y = 1
void add_and_one(int x, int y)
{
addse(x ^ 1, y);
addse(y ^ 1, x);
addse(x, y);
addse(y ^ 1, x ^ 1);