-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgs2.txt
More file actions
1599 lines (1586 loc) · 41 KB
/
algs2.txt
File metadata and controls
1599 lines (1586 loc) · 41 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
This file contains the code from "Algorithms in C++, Third Edition,
Part 5," by Robert Sedgewick, and is covered under the copyright
and warranty notices in that book. Permission is granted for this
code to be used for educational purposes in association with the text,
and for other uses not covered by copyright laws, provided that
the following notice is included with the code:
"This code is from "Algorithms in C++, Third Edition,"
by Robert Sedgewick, Addison-Wesley, 2002."
Commercial uses of this code require the explicit written
permission of the publisher. Send your request for permission,
stating clearly what code you would like to use, and in what
specific way, to: aw.cse@aw.com
----------
CHAPTER 17. Graph Properties and Types
-----
struct Edge
{ int v, w;
Edge(int v = -1, int w = -1) : v(v), w(w) { }
};
class GRAPH
{ private:
// Implementation-dependent code
public:
GRAPH(int, bool);
~GRAPH();
int V() const;
int E() const;
bool directed() const;
int insert(Edge);
int remove(Edge);
bool edge(int, int);
class adjIterator
{
public:
adjIterator(const GRAPH &, int);
int beg();
int nxt();
bool end();
};
};
-----
template <class Graph>
vector <Edge> edges(Graph &G)
{ int E = 0;
vector <Edge> a(G.E());
for (int v = 0; v < G.V(); v++)
{
typename Graph::adjIterator A(G, v);
for (int w = A.beg(); !A.end(); w = A.nxt())
if (G.directed() || v < w)
a[E++] = Edge(v, w);
}
return a;
}
-----
template <class Graph>
void IO<Graph>::show(const Graph &G)
{
for (int s = 0; s < G.V(); s++)
{
cout.width(2); cout << s << ":";
typename Graph::adjIterator A(G, s);
for (int t = A.beg(); !A.end(); t = A.nxt())
{ cout.width(2); cout << t << " "; }
cout << endl;
}
}
-----
template <class Graph>
class IO
{
public:
static void show(const Graph &);
static void scanEZ(Graph &);
static void scan(Graph &);
};
-----
template <class Graph>
class CC
{
private:
// implementation-dependent code
public:
CC(const Graph &);
int count();
bool connect(int, int);
};
-----
#include <iostream.h>
#include <stdlib.h>
#include "GRAPH.cc"
#include "IO.cc"
#include "CC.cc"
main(int argc, char *argv[])
{ int V = atoi(argv[1]);
GRAPH G(V);
IO<GRAPH>::scan(G);
if (V < 20) IO<GRAPH>::show(G);
cout << G.E() << " edges ";
CC<GRAPH> Gcc(G);
cout << Gcc.count() << " components" << endl;
}
-----
class DenseGRAPH
{ int Vcnt, Ecnt; bool digraph;
vector <vector <bool> > adj;
public:
DenseGRAPH(int V, bool digraph = false) :
adj(V), Vcnt(V), Ecnt(0), digraph(digraph)
{
for (int i = 0; i < V; i++)
adj[i].assign(V, false);
}
int V() const { return Vcnt; }
int E() const { return Ecnt; }
bool directed() const { return digraph; }
void insert(Edge e)
{ int v = e.v, w = e.w;
if (adj[v][w] == false) Ecnt++;
adj[v][w] = true;
if (!digraph) adj[w][v] = true;
}
void remove(Edge e)
{ int v = e.v, w = e.w;
if (adj[v][w] == true) Ecnt--;
adj[v][w] = false;
if (!digraph) adj[w][v] = false;
}
bool edge(int v, int w) const
{ return adj[v][w]; }
class adjIterator;
friend class adjIterator;
};
-----
class DenseGRAPH::adjIterator
{ const DenseGRAPH &G;
int i, v;
public:
adjIterator(const DenseGRAPH &G, int v) :
G(G), v(v), i(-1) { }
int beg()
{ i = -1; return nxt(); }
int nxt()
{
for (i++; i < G.V(); i++)
if (G.adj[v][i] == true) return i;
return -1;
}
bool end()
{ return i >= G.V(); }
};
-----
class SparseMultiGRAPH
{ int Vcnt, Ecnt; bool digraph;
struct node
{ int v; node* next;
node(int x, node* t) { v = x; next = t; }
};
typedef node* link;
vector <link> adj;
public:
SparseMultiGRAPH(int V, bool digraph = false) :
adj(V), Vcnt(V), Ecnt(0), digraph(digraph)
{ adj.assign(V, 0); }
int V() const { return Vcnt; }
int E() const { return Ecnt; }
bool directed() const { return digraph; }
void insert(Edge e)
{ int v = e.v, w = e.w;
adj[v] = new node(w, adj[v]);
if (!digraph) adj[w] = new node(v, adj[w]);
Ecnt++;
}
void remove(Edge e);
bool edge(int v, int w) const;
class adjIterator;
friend class adjIterator;
};
-----
class SparseMultiGRAPH::adjIterator
{ const SparseMultiGRAPH &G;
int v;
link t;
public:
adjIterator(const SparseMultiGRAPH &G, int v) :
G(G), v(v) { t = 0; }
int beg()
{ t = G.adj[v]; return t ? t->v : -1; }
int nxt()
{ if (t) t = t->next; return t ? t->v : -1; }
bool end()
{ return t == 0; }
};
-----
template <class Graph> class DEGREE
{ const Graph &G;
vector <int> degree;
public:
DEGREE(const Graph &G) : G(G), degree(G.V(), 0)
{
for (int v = 0; v < G.V(); v++)
{ typename Graph::adjIterator A(G, v);
for (int w = A.beg(); !A.end(); w = A.nxt())
degree[v]++;
}
}
int operator[](int v) const
{ return degree[v]; }
};
-----
static void randE(Graph &G, int E)
{
for (int i = 0; i < E; i++)
{
int v = int(G.V()*rand()/(1.0+RAND_MAX));
int w = int(G.V()*rand()/(1.0+RAND_MAX));
G.insert(Edge(v,w));
}
}
-----
static void randG(Graph &G, int E)
{ double p = 2.0*E/G.V()/(G.V()-1);
for (int i = 0; i < G.V(); i++)
for (int j = 0; j < i; j++)
if (rand() < p*RAND_MAX)
G.insert(Edge(i, j));
}
-----
#include "ST.cc"
template <class Graph>
void IO<Graph>::scan(Graph &G)
{ string v, w;
ST st;
while (cin >> v >> w)
G.insert(Edge(st.index(v), st.index(w)));
}
-----
#include <string>
class ST
{ int N, val;
struct node
{ int v, d; node* l, *m, *r;
node(int d) : v(-1), d(d), l(0), m(0), r(0) {}
};
typedef node* link;
link head;
link indexR(link h, const string &s, int w)
{ int i = s[w];
if (h == 0) h = new node(i);
if (i == 0)
{
if (h->v == -1) h->v = N++;
val = h->v;
return h;
}
if (i < h->d) h->l = indexR(h->l, s, w);
if (i == h->d) h->m = indexR(h->m, s, w+1);
if (i > h->d) h->r = indexR(h->r, s, w);
return h;
}
public:
ST() : head(0), N(0) { }
int index(const string &key)
{ head = indexR(head, key, 0); return val; }
};
-----
template <class Graph> class sPATH
{ const Graph &G;
vector <bool> visited;
bool found;
bool searchR(int v, int w)
{
if (v == w) return true;
visited[v] = true;
typename Graph::adjIterator A(G, v);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (!visited[t])
if (searchR(t, w)) return true;
return false;
}
public:
sPATH(const Graph &G, int v, int w) :
G(G), visited(G.V(), false)
{ found = searchR(v, w); }
bool exists() const
{ return found; }
};
-----
bool searchR(int v, int w, int d)
{
if (v == w) return (d == 0);
visited[v] = true;
typename Graph::adjIterator A(G, v);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (!visited[t])
if (searchR(t, w, d-1)) return true;
visited[v] = false;
return false;
}
-----
template <class Graph> class ePATH
{ Graph G;
int v, w;
bool found;
STACK <int> S;
int tour(int v);
public:
ePATH(const Graph &G, int v, int w) :
G(G), v(v), w(w)
{ DEGREE<Graph> deg(G);
int t = deg[v] + deg[w];
if ((t % 2) != 0) { found = false; return; }
for (t = 0; t < G.V(); t++)
if ((t != v) && (t != w))
if ((deg[t] % 2) != 0)
{ found = false; return; }
found = true;
}
bool exists() const
{ return found; }
void show();
};
-----
template <class Graph>
int ePATH<Graph>::tour(int v)
{
while (true)
{ typename Graph::adjIterator A(G, v);
int w = A.beg(); if (A.end()) break;
S.push(v);
G.remove(Edge(v, w));
v = w;
}
return v;
}
template <class Graph>
void ePATH<Graph>::show()
{
if (!found) return;
while (tour(v) == v && !S.empty())
{ v = S.pop(); cout << "-" << v; }
cout << endl;
}
----------
CHAPTER 18. Graph Search
-----
#include <vector>
template <class Graph> class cDFS
{ int cnt;
const Graph &G;
vector <int> ord;
void searchC(int v)
{
ord[v] = cnt++;
typename Graph::adjIterator A(G, v);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (ord[t] == -1) searchC(t);
}
public:
cDFS(const Graph &G, int v = 0) :
G(G), cnt(0), ord(G.V(), -1)
{ searchC(v); }
int count() const { return cnt; }
int operator[](int v) const { return ord[v]; }
};
-----
template <class Graph> class SEARCH
{
protected:
const Graph &G;
int cnt;
vector <int> ord;
virtual void searchC(Edge) = 0;
void search()
{ for (int v = 0; v < G.V(); v++)
if (ord[v] == -1) searchC(Edge(v, v)); }
public:
SEARCH (const Graph &G) : G(G),
ord(G.V(), -1), cnt(0) { }
int operator[](int v) const { return ord[v]; }
};
-----
template <class Graph>
class DFS : public SEARCH<Graph>
{ vector<int> st;
void searchC(Edge e)
{ int w = e.w;
ord[w] = cnt++; st[e.w] = e.v;
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (ord[t] == -1) searchC(Edge(w, t));
}
public:
DFS(const Graph &G) : SEARCH<Graph>(G),
st(G.V(), -1) { search(); }
int ST(int v) const { return st[v]; }
};
-----
template <class Graph> class CC
{ const Graph &G;
int ccnt;
vector <int> id;
void ccR(int w)
{
id[w] = ccnt;
typename Graph::adjIterator A(G, w);
for (int v = A.beg(); !A.end(); v = A.nxt())
if (id[v] == -1) ccR(v);
}
public:
CC(const Graph &G) : G(G), ccnt(0), id(G.V(), -1)
{
for (int v = 0; v < G.V(); v++)
if (id[v] == -1) { ccR(v); ccnt++; }
}
int count() const { return ccnt; }
bool connect(int s, int t) const
{ return id[s] == id[t]; }
};
-----
template <class Graph>
class EULER : public SEARCH<Graph>
{
void searchC(Edge e)
{ int v = e.v, w = e.w;
ord[w] = cnt++;
cout << "-" << w;
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (ord[t] == -1) searchC(Edge(w, t));
else if (ord[t] < ord[v])
cout << "-" << t << "-" << w;
if (v != w) cout << "-" << v; else cout << endl;
}
public:
EULER(const Graph &G) : SEARCH<Graph>(G)
{ search(); }
};
-----
template <class Graph> class BI
{ const Graph &G;
bool OK;
vector <int> vc;
bool dfsR(int v, int c)
{
vc[v] = (c+1) %2;
typename Graph::adjIterator A(G, v);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (vc[t] == -1)
{ if (!dfsR(t, vc[v])) return false; }
else if (vc[t] != c) return false;
return true;
}
public:
BI(const Graph &G) : G(G), OK(true), vc(G.V(),-1)
{
for (int v = 0; v < G.V(); v++)
if (vc[v] == -1)
if (!dfsR(v, 0)) { OK = false; return; }
}
bool bipartite() const { return OK; }
int color(int v) const { return vc[v]; }
};
-----
template <class Graph>
class EC : public SEARCH<Graph>
{ int bcnt;
vector <int> low;
void searchC(Edge e)
{ int w = e.w;
ord[w] = cnt++; low[w] = ord[w];
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (ord[t] == -1)
{
searchC(Edge(w, t));
if (low[w] > low[t]) low[w] = low[t];
if (low[t] == ord[t])
bcnt++; // w-t is a bridge
}
else if (t != e.v)
if (low[w] > ord[t]) low[w] = ord[t];
}
public:
EC(const Graph &G) : SEARCH<Graph>(G),
bcnt(0), low(G.V(), -1)
{ search(); }
int count() const { return bcnt+1; }
};
-----
#include "QUEUE.cc"
template <class Graph>
class BFS : public SEARCH<Graph>
{ vector<int> st;
void searchC(Edge e)
{ QUEUE<Edge> Q;
Q.put(e);
while (!Q.empty())
if (ord[(e = Q.get()).w] == -1)
{ int v = e.v, w = e.w;
ord[w] = cnt++; st[w] = v;
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (ord[t] == -1) Q.put(Edge(w, t));
}
}
public:
BFS(Graph &G) : SEARCH<Graph>(G), st(G.V(), -1)
{ search(); }
int ST(int v) const { return st[v]; }
};
-----
void searchC(Edge e)
{ QUEUE<Edge> Q;
Q.put(e); ord[e.w] = cnt++;
while (!Q.empty())
{
e = Q.get(); st[e.w] = e.v;
typename Graph::adjIterator A(G, e.w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (ord[t] == -1)
{ Q.put(Edge(e.w, t)); ord[t] = cnt++; }
}
}
-----
#include "GQ.cc"
template <class Graph>
class PFS : public SEARCH<Graph>
{ vector<int> st;
void searchC(Edge e)
{ GQ<Edge> Q(G.V());
Q.put(e); ord[e.w] = cnt++;
while (!Q.empty())
{
e = Q.get(); st[e.w] = e.v;
typename Graph::adjIterator A(G, e.w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (ord[t] == -1)
{ Q.put(Edge(e.w, t)); ord[t] = cnt++; }
else
if (st[t] == -1) Q.update(Edge(e.w, t));
}
}
public:
PFS(Graph &G) : SEARCH<Graph>(G), st(G.V(), -1)
{ search(); }
int ST(int v) const { return st[v]; }
};
-----
template <class Item>
class GQ
{
private:
vector<Item> s; int N;
public:
GQ(int maxN) : s(maxN+1), N(0) { }
int empty() const
{ return N == 0; }
void put(Item item)
{ s[N++] = item; }
void update(Item x) { }
Item get()
{ int i = int(N*rand()/(1.0+RAND_MAX));
Item t = s[i];
s[i] = s[N-1];
s[N-1] = t;
return s[--N]; }
};
----------
CHAPTER 19. Digraphs and DAGs
-----
template <class inGraph, class outGraph>
void reverse(const inGraph &G, outGraph &R)
{
for (int v = 0; v < G.V(); v++)
{ typename inGraph::adjIterator A(G, v);
for (int w = A.beg(); !A.end(); w = A.nxt())
R.insert(Edge(w, v));
}
}
-----
template <class Graph> class DFS
{ const Graph &G;
int depth, cnt, cntP;
vector<int> pre, post;
void show(char *s, Edge e)
{ for (int i = 0; i < depth; i++) cout << " ";
cout << e.v << "-" << e.w << s << endl; }
void dfsR(Edge e)
{ int w = e.w; show(" tree", e);
pre[w] = cnt++; depth++;
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
{ Edge x(w, t);
if (pre[t] == -1) dfsR(x);
else if (post[t] == -1) show(" back", x);
else if (pre[t] > pre[w]) show(" down", x);
else show(" cross", x);
}
post[w] = cntP++; depth--;
}
public:
DFS(const Graph &G) : G(G), cnt(0), cntP(0),
pre(G.V(), -1), post(G.V(), -1)
{ for (int v = 0; v < G.V(); v++)
if (pre[v] == -1) dfsR(Edge(v, v)); }
};
-----
template <class tcGraph, class Graph> class TC
{ tcGraph T;
public:
TC(const Graph &G) : T(G)
{
for (int s = 0; s < T.V(); s++)
T.insert(Edge(s, s));
for (int i = 0; i < T.V(); i++)
for (int s = 0; s < T.V(); s++)
if (T.edge(s, i))
for (int t = 0; t < T.V(); t++)
if (T.edge(i, t))
T.insert(Edge(s, t));
}
bool reachable(int s, int t) const
{ return T.edge(s, t); }
};
-----
template <class Graph> class tc
{ Graph T; const Graph &G;
void tcR(int v, int w)
{
T.insert(Edge(v, w));
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (!T.edge(v, t)) tcR(v, t);
}
public:
tc(const Graph &G) : G(G), T(G.V(), true)
{ for (int v = 0; v < G.V(); v++) tcR(v, v); }
bool reachable(int v, int w)
{ return T.edge(v, w); }
};
-----
int compressR(link h)
{ STx st;
if (h == NULL) return 0;
l = compressR(h->l);
r = compressR(h->r);
t = st.index(l, r);
adj[t].l = l; adj[t].r = r;
return t;
}
-----
template <class Dag> class dagTS
{ const Dag &D;
int cnt, tcnt;
vector<int> pre, post, postI;
void tsR(int v)
{
pre[v] = cnt++;
typename Dag::adjIterator A(D, v);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (pre[t] == -1) tsR(t);
post[v] = tcnt; postI[tcnt++] = v;
}
public:
dagTS(const Dag &D) : D(D), tcnt(0), cnt(0),
pre(D.V(), -1), post(D.V(), -1), postI(D.V(), -1)
{ for (int v = 0; v < D.V(); v++)
if (pre[v] == -1) tsR(v); }
int operator[](int v) const { return postI[v]; }
int relabel(int v) const { return post[v]; }
};
-----
void tsR(int v)
{
pre[v] = cnt++;
for (int w = 0; w < D.V(); w++)
if (D.edge(w, v))
if (pre[w] == -1) tsR(w);
post[v] = tcnt; postI[tcnt++] = v;
}
-----
#include "QUEUE.cc"
template <class Dag> class dagTS
{ const Dag &D;
vector<int> in, ts, tsI;
public:
dagTS(const Dag &D) : D(D),
in(D.V(), 0), ts(D.V(), -1), tsI(D.V(), -1)
{ QUEUE<int> Q;
for (int v = 0; v < D.V(); v++)
{
typename Dag::adjIterator A(D, v);
for (int t = A.beg(); !A.end(); t = A.nxt())
in[t]++;
}
for (int v = 0; v < D.V(); v++)
if (in[v] == 0) Q.put(v);
for (int j = 0; !Q.empty(); j++)
{
ts[j] = Q.get(); tsI[ts[j]] = j;
typename Dag::adjIterator A(D, ts[j]);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (--in[t] == 0) Q.put(t);
}
}
int operator[](int v) const { return ts[v]; }
int relabel(int v) const { return tsI[v]; }
};
-----
template <class tcDag, class Dag> class dagTC
{ tcDag T; const Dag &D;
int cnt;
vector<int> pre;
void tcR(int w)
{
pre[w] = cnt++;
typename Dag::adjIterator A(D, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
{
T.insert(Edge(w, t));
if (pre[t] > pre[w]) continue;
if (pre[t] == -1) tcR(t);
for (int i = 0; i < T.V(); i++)
if (T.edge(t, i)) T.insert(Edge(w, i));
}
}
public:
dagTC(const Dag &D) : D(D), cnt(0),
pre(D.V(), -1), T(D.V(), true)
{ for (int v = 0; v < D.V(); v++)
if (pre[v] == -1) tcR(v); }
bool reachable(int v, int w) const
{ return T.edge(v, w); }
};
-----
template <class Graph> class SC
{ const Graph &G;
int cnt, scnt;
vector<int> postI, postR, id;
void dfsR(const Graph &G, int w)
{
id[w] = scnt;
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (id[t] == -1) dfsR(G, t);
postI[cnt++] = w;
}
public:
SC(const Graph &G) : G(G), cnt(0), scnt(0),
postI(G.V()), postR(G.V()), id(G.V(), -1)
{ Graph R(G.V(), true);
reverse(G, R);
for (int v = 0; v < R.V(); v++)
if (id[v] == -1) dfsR(R, v);
postR = postI; cnt = scnt = 0;
id.assign(G.V(), -1);
for (int v = G.V()-1; v >= 0; v--)
if (id[postR[v]] == -1)
{ dfsR(G, postR[v]); scnt++; }
}
int count() const { return scnt; }
bool stronglyreachable(int v, int w) const
{ return id[v] == id[w]; }
};
-----
#include "STACK.cc"
template <class Graph> class SC
{ const Graph &G;
STACK<int> S;
int cnt, scnt;
vector<int> pre, low, id;
void scR(int w)
{ int t;
int min = low[w] = pre[w] = cnt++;
S.push(w);
typename Graph::adjIterator A(G, w);
for (t = A.beg(); !A.end(); t = A.nxt())
{
if (pre[t] == -1) scR(t);
if (low[t] < min) min = low[t];
}
if (min < low[w]) { low[w] = min; return; }
do
{ id[t = S.pop()] = scnt; low[t] = G.V(); }
while (t != w);
scnt++;
}
public:
SC(const Graph &G) : G(G), cnt(0), scnt(0),
pre(G.V(), -1), low(G.V()), id(G.V())
{ for (int v = 0; v < G.V(); v++)
if (pre[v] == -1) scR(v); }
int count() const { return scnt; }
bool stronglyreachable(int v, int w) const
{ return id[v] == id[w]; }
};
-----
void scR(int w)
{ int v;
pre[w] = cnt++;
S.push(w); path.push(w);
typename Graph::adjIterator A(G, w);
for (int t = A.beg(); !A.end(); t = A.nxt())
if (pre[t] == -1) scR(t);
else if (id[t] == -1)
while (pre[path.top()] > pre[t]) path.pop();
if (path.top() == w) path.pop(); else return;
do { id[v = S.pop()] = scnt; } while (v != w);
scnt++;
}
-----
template <class Graph> class TC
{ const Graph &G; DenseGRAPH *K;
dagTC<DenseGRAPH, Graph> *Ktc;
SC<Graph> *Gsc;
public:
TC(const Graph &G) : G(G)
{
Gsc = new SC<Graph>(G);
K = new DenseGRAPH(Gsc->count(), true);
for (int v = 0; v < G.V(); v++)
{ typename Graph::adjIterator A(G, v);
for (int t = A.beg(); !A.end(); t = A.nxt())
K->insert(Edge(Gsc->ID(v), Gsc->ID(t))); }
Ktc = new dagTC<DenseGRAPH, Graph>(*K);
}
~TC() { delete K; delete Ktc; delete Gsc; }
bool reachable(int v, int w)
{ return Ktc->reachable(Gsc->ID(v), Gsc->ID(w));}
};
----------
CHAPTER 20. Minimum Spanning Trees
-----
class EDGE
{
public:
EDGE(int, int, double);
int v() const;
int w() const;
double wt() const;
bool from(int) const;
int other(int) const;
};
template <class Edge> class GRAPH
{
public:
GRAPH(int, bool);
~GRAPH();
int V() const;
int E() const;
bool directed() const;
int insert(Edge *);
int remove(Edge *);
Edge *edge(int, int);
class adjIterator
{
public:
adjIterator(const GRAPH &, int);
Edge *beg();
Edge *nxt();
bool end();
};
};
-----
template <class Graph, class Edge>
vector <Edge *> edges(const Graph &G)
{ int E = 0;
vector <Edge *> a(G.E());
for (int v = 0; v < G.V(); v++)
{
typename Graph::adjIterator A(G, v);
for (Edge* e = A.beg(); !A.end(); e = A.nxt())
if (e->from(v)) a[E++] = e;
}
return a;
}
-----
template <class Edge> class DenseGRAPH
{ int Vcnt, Ecnt; bool digraph;
vector <vector <Edge *> > adj;
public:
DenseGRAPH(int V, bool digraph = false) :
adj(V), Vcnt(V), Ecnt(0), digraph(digraph)
{
for (int i = 0; i < V; i++)
adj[i].assign(V, 0);
}
int V() const { return Vcnt; }
int E() const { return Ecnt; }
bool directed() const { return digraph; }
void insert(Edge *e)
{ int v = e->v(), w = e->w();
if (adj[v][w] == 0) Ecnt++;
adj[v][w] = e;
if (!digraph) adj[w][v] = e;
}
void remove(Edge *e)
{ int v = e->v(), w = e->w();
if (adj[v][w] != 0) Ecnt--;
adj[v][w] = 0;
if (!digraph) adj[w][v] = 0;
}
Edge* edge(int v, int w) const
{ return adj[v][w]; }
class adjIterator;
friend class adjIterator;
};
-----
template <class Edge>
class DenseGRAPH<Edge>::adjIterator
{ const DenseGRAPH<Edge> &G;
int i, v;
public:
adjIterator(const DenseGRAPH<Edge> &G, int v) :
G(G), v(v), i(0) { }
Edge *beg()
{ i = -1; return nxt(); }
Edge *nxt()
{
for (i++; i < G.V(); i++)
if (G.edge(v, i)) return G.adj[v][i];
return 0;
}
bool end() const
{ return i >= G.V(); }
};
-----
template <class Edge> class SparseMultiGRAPH
{ int Vcnt, Ecnt; bool digraph;
struct node
{ Edge* e; node* next;
node(Edge* e, node* next): e(e), next(next) {}
};
typedef node* link;
vector <link> adj;
public:
SparseMultiGRAPH(int V, bool digraph = false) :
adj(V), Vcnt(V), Ecnt(0), digraph(digraph) { }
int V() const { return Vcnt; }
int E() const { return Ecnt; }
bool directed() const { return digraph; }
void insert(Edge *e)
{
adj[e->v()] = new node(e, adj[e->v()]);
if (!digraph)
adj[e->w()] = new node(e, adj[e->w()]);
Ecnt++;
}
class adjIterator;
friend class adjIterator;
};
-----
template <class Graph, class Edge> class MST
{ const Graph &G;
vector<double> wt;
vector<Edge *> fr, mst;
public:
MST(const Graph &G) : G(G),
mst(G.V()), wt(G.V(), G.V()), fr(G.V())
{ int min = -1;
for (int v = 0; min != 0; v = min)
{
min = 0;
for (int w = 1; w < G.V(); w++)
if (mst[w] == 0)
{ double P; Edge* e = G.edge(v, w);
if (e)
if ((P = e->wt()) < wt[w])
{ wt[w] = P; fr[w] = e; }
if (wt[w] < wt[min]) min = w;
}
if (min) mst[min] = fr[min];
}
}
void show()
{ for (int v = 1; v < G.V(); v++)
if (mst[v]) mst[v]->show();}
};
-----
template <class Graph, class Edge> class MST
{ const Graph &G;
vector<double> wt;
vector<Edge *> fr, mst;
void pfs(int s)
{ PQi<double> pQ(G.V(), wt);
pQ.insert(s);
while (!pQ.empty())
{ int v = pQ.getmin();
mst[v] = fr[v];