-
Notifications
You must be signed in to change notification settings - Fork 0
/
Structures.hpp
725 lines (688 loc) · 17.3 KB
/
Structures.hpp
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
/*!
* \file Structures.h
* \author Quentin Fortier
* General definitions of edges and graphs
*/
#ifndef STRUCTURE
#define STRUCTURE
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <assert.h>
#include <algorithm>
#include <limits>
#include <algorithm> // std::random_shuffle
//#include <climits>
namespace sgl
{
template<typename type> inline type max_val() // for old compilators (acm-icpc)...
{
//return INT_MAX;
return std::numeric_limits<type>::max();
}
/*! Edge with two extremities */
class Edge_Base
{
protected:
int v_,w_; // Must be const for Graph
public:
/*! Creates an edge from v to w with weight wt */
Edge_Base(int v, int w): v_(v), w_(w) { };
Edge_Base(Edge_Base *e): v_(e->v_), w_(e->w_) { };
/*! \returns Start vertex */
inline int v() const { return v_; }
/*! \returns End vertex */
inline int w() const { return w_; }
/*! \returns the extremity different from u */
inline int other(int u) const
{
if(u==v_) return w_;
if(u==w_) return v_;
return -1;
}
/*! \returns True if the edge starts from v */
inline bool from (int v) const
{ return v_ == v; }
bool operator==(const Edge_Base &e) const
{
return (e.v() == v_ && e.w() == w_) || (e.w() == v_ && e.v() == w_);
}
};
std::ostream &operator<<(std::ostream &os, const Edge_Base &e)
{
os<<e.v()<<" --> " <<e.w();
return os;
}
template<typename type_wt> std::ostream &operator<<(std::ostream &os, const Edge_Base &e)
{
os<<e.v()<<" --> " <<e.w();
return os;
};
/*! Edge with weight
\todo Class Vertex */
template<typename type_wt = int> class Edge_Weight : virtual public Edge_Base
{
type_wt wt_;
public:
Edge_Weight(int v, int w, type_wt wt): Edge_Base(v, w), wt_(wt) { }
/*! \returns weight */
inline type_wt wt() const { return wt_; }
/*! Sets a new weight */
inline void set_wt(type_wt wt) { wt_ = wt; }
bool operator<(const Edge_Weight &e) const
{
return wt < e.wt || (wt == e.wt && v() < e.v()) || (wt == e.wt && v() == e.v() && w() < e.w());
}
};
template<typename type_wt> std::ostream &operator<<(std::ostream &os, const Edge_Weight<type_wt> &e)
{
os<<e.v()<<" -- "<<e.wt()<<" --> " <<e.w();
return os;
};
/*! Tree
\warning Direction in the Tree_List may not correspond to the direction of the edges (from e->v() to e->w()) */
template<class Edge = Edge_Base> class Tree_List
{
int Vcnt, Ecnt;
struct node
{ Edge* e; node* next; node* last;
node(Edge* e, node* next, node* last): e(e), next(next), last(last) {};
~node() {}
};
std::vector<Edge *> adjPred; // Predecessors
std::vector<node *> adjSucc; // Successors
public:
Tree_List(int V) : Vcnt(V), Ecnt(0), adjPred(V, (Edge*)NULL), adjSucc(V, (node*)NULL) { }
~Tree_List() { }
/*! \returns Number of vertices */
inline int V() const { return Vcnt; }
/*! \returns Number of edges */
inline int E() const { return Ecnt; }
/*! \returns True if v is isolated */
bool isolated(int v)
{
if(pred(v) != (Edge*)NULL) return false;
iterator it(*this, v);
it.beg();
if(it.end()) return true;
return false;
}
/*!
\param v A vertex
\returns the degree of v
*/
int deg(int v) const
{
iterator it(*this, v);
int res = (pred(v) == (Edge*)NULL ? 0 : 1);
for(it.beg(); !it.end(); res++, it.nxt());
return res;
}
/*! \brief Insert an edge */
void insert(Edge *e, int succVertex)
{
adjPred[succVertex] = e;
int predVertex = e->other(succVertex);
node * tmp = adjSucc[predVertex];
adjSucc[predVertex] = new node(e, tmp, NULL);
if(tmp)
tmp->last = adjSucc[predVertex];
Ecnt++;
}
/*! Clear the tree without deleting any edge pointer */
void clear()
{
for(unsigned int i = 0; i < adjSucc.size(); i++)
{
node *cur = adjSucc[i], *nxt = NULL;
while(cur != NULL)
{
nxt = cur->next;
delete cur;
cur = nxt;
}
adjSucc[i] = NULL;
}
for(unsigned int i = 0; i < adjPred.size(); i++)
adjPred[i] = NULL;
}
/*! \returns \li A pointer to a edge from v to w if it exists
\li NULL otherwise
*/
inline Edge *edge(int v, int w) const
{
if(adjPred[v]->other(v) == w)
return adjPred[v];
if(adjPred[w]->other(w) == v)
return adjPred[w];
return NULL;
}
/*! \returns \li Edge of a predecessor of v if it exists
\li NULL otherwise */
inline Edge *pred(int v) const { return adjPred[v]; }
/*! \returns \li Predecessor of v if it exists
\li -1 otherwise */
inline int pred_vertex(int v) const
{
Edge *e = pred(v);
if(e)
return e->other(v);
else
return -1;
}
class iterator;
friend class iterator;
class iterator_all;
friend class iterator_all;
};
/*! Rooted tree with a distance from root */
template<class Edge = Edge_Base, class Tree = Tree_List<Edge> > class Tree_Dist : public Tree
{
std::vector<int> distance; // Distance from source
public:
Tree_Dist(int V) : Tree(V), distance(V, -1) { }
void insert(Edge *e, int succVertex)
{
Tree::insert(e, succVertex);
int pred = e->other(succVertex);
if(distance[pred] == -1) return; // Error
distance[succVertex] = distance[pred] + 1;
}
int dist(int v)
{
if(distance[v] == -1) return max_val<int>();
return distance[v];
}
void set_source(int s)
{
distance[s] = 0;
}
};
/*! Ierates through the \b successors of a vertex
\see Tree_List::pred */
template<class Edge = Edge_Base > class Tree_List<Edge>::iterator
{
const Tree_List<Edge> &T;
int v;
node* t; // Current node
public:
/*! Ierates through the \b successors of v */
iterator(const Tree_List<Edge> &T, int v) : T(T), v(v), t(NULL) {}
/*! Begins an iteration */
inline Edge* beg() { t = T.adjSucc[v]; return t ? t->e : NULL; }
/*! \returns Next edge, NULL if there is no more edge */
inline Edge* nxt() { if (t) t = t->next; return t ? t->e : NULL; }
/*! \returns true if there is no more edge */
inline bool end() { return t == NULL; }
};
/*! Ierates through all edges */
template<class Edge = Edge_Base > class Tree_List<Edge>::iterator_all
{
const Tree_List<Edge> &T;
int i;
public:
iterator_all(const Tree_List<Edge> &T) : T(T), i(0) { }
/*! Begins an iteration */
inline Edge* beg()
{
i = -1;
return nxt();
}
/*! \returns Next edge, NULL if there is no more edge */
inline Edge* nxt()
{
for (i++; i < T.V(); i++)
if(T.adjPred[i])
return T.adjPred[i];
return NULL;
}
/*! \returns True if there is no more edge */
inline bool end()
{ return i >= T.adjPred.size(); }
};
/*! Adjacency list graph
\todo Use list instead of node */
template<class Edge = Edge_Base > class Graph_List
{
int Vcnt;
bool digraph;
struct node
{ Edge* e; node* next; node* last;
node(Edge* e, node* next, node* last): e(e), next(next), last(last) {};
~node() {}
};
// Remove t in adj[posInAdj]
inline void remove_node(node *t, int posInAdj)
{
//assert(t);
if(!t->last)
{
if(!t->next)
adj[posInAdj] = NULL;
else {
t->next->last = NULL;
adj[posInAdj] = t->next;
}
}
else
{
if(!t->next)
t->last->next = NULL;
else
{
t->last->next = t->next;
t->next->last = t->last;
}
}
}
public:
std::vector<node*> adj; // Adjacency list
/*!
\param V Number of vertices
\param digraph Specify if the graph is directed
*/
Graph_List(int V, bool digraph = false) : Vcnt(V), digraph(digraph), adj(V, (node*)NULL) { }
// Todo: undirected
Graph_List(Graph_List const &G_copy) : Vcnt(G_copy.V()), digraph(G_copy.directed()), adj(G_copy.V(), (node*)NULL)
{
for(int i = 0; i < Vcnt; i++)
for(int j = 0; j < Vcnt; j++)
if(G_copy.edge(i, j) != NULL)
insert(new Edge(G_copy.edge(i, j)));
}
~Graph_List() { }
/*! \returns Number of edges */
int size() const
{
int ret = 0;
iterator_all it(*this);
for(it.beg(); !it.end(); ret++, it.nxt());
return ret;
}
/*!
\param v A vertex
\returns the degree of v
*/
int deg(int v) const
{
iterator it(*this, v);
int res;
for(res = 0, it.beg(); !it.end(); res++, it.nxt());
return res;
}
/*! Delete every edge pointer
\see clear to remove edges without deleting them */
void delete_ptr()
{
for(unsigned int i = 0; i < adj.size(); i++)
{
node *cur = adj[i], *nxt = NULL;
while(cur != NULL)
{
nxt = cur->next;
// TODO: if cur->e has been deleted, cur->e->from(i) cannot be accessed properly
if(digraph || i > cur->e->other(i)) // Evite d'avoir des pointeurs invalides
delete cur->e;
delete cur;
cur = nxt;
}
adj[i] = NULL;
}
}
/*! \param V New size */
void resize(int V)
{
Vcnt = V;
adj.resize(Vcnt, (node*)NULL);
}
/*! \returns Number of vertices */
inline int V() const { return Vcnt; }
/*! \returns True iff directed */
inline bool directed() const { return digraph; }
/*! \todo directed case */
bool isolated(int v)
{
iterator it(*this, v);
it.beg();
if(it.end()) return true;
return false;
}
/*! \returns \li A pointer to a edge from v to w if it exists
\li NULL otherwise
\warning See Graph_Matrix for better performances
*/
inline Edge* edge(int v, int w) const
{
iterator it(*this, v);
for(Edge *e = it.beg(); !it.end(); e = it.nxt())
if(e->other(v) == w)
return e;
return NULL;
}
/*! Insert an edge <br>
If directed: create a new pointer for the reverse edge
\param sameEdgePtr Specify, if the graph is not directed,
if the reverse edge must have the same pointer (if sameEdgePtr is false, a new pointer to an edge is created for the reverse edge)
*/
void insert(Edge *e, bool sameEdgePtr = true)
{
if ((e->v() >= Vcnt) || (e->w() >= Vcnt))
resize(e->v() > e->w() ? e->v() : e->w());
node * tmp = adj[e->v()];
adj[e->v()] = new node(e, tmp, NULL);
if(tmp)
tmp->last = adj[e->v()];
if (!digraph)
{
node * tmp_ = adj[e->w()];
if(sameEdgePtr) // (warning : ne pas ecrire adj[e->w()] = adj[e->v()]; car sinon les nodes sont les memes!)
adj[e->w()] = new node(e, tmp_, NULL);
else
adj[e->w()] = new node(new Edge(*e), tmp_, NULL);
if(tmp_)
tmp_->last = adj[e->w()];
}
}
/* Create a complete DIRECTED graph */
void complete()
{
for(int i = 0; i < Vcnt; i++)
{
std::vector<int> tab;
for(int j = 0; j < Vcnt; j++)
if(j != i)
tab.push_back(j);
random_shuffle ( tab.begin(), tab.end() );
for(int k = 0; k < tab.size(); k++)
{
insert(new Edge_Base(i, tab[k]));
}
}
}
/*! Remove vertex v and delete all its associated edges */
inline void remove(int v)
{
node *cur = adj[v], *nxt = NULL;
while(cur != NULL)
{
nxt = cur->next;
Edge *e = cur->e;
if(!digraph)
remove(e, e->other(v));
delete e;
delete cur;
cur = nxt;
}
adj[v] = NULL; // important
}
/*! Remove edge pointer e from the vertex v <b> comparing pointers </b> without deleting the edge *e
\returns Number of elements removed
\warning If the graph is not oriented, use remove(Edge*) instead */
inline int remove(Edge *e, int v)
{
int nRemoved = 0;
for(node *cur = adj[v]; cur; cur = cur->next)
if(cur->e == e) // Pointer comparison
{
remove_node(cur, v);
nRemoved++;
}
return nRemoved;
}
/*! Remove edge e from both extremities without deleting the edge *e
\param e Valid pointer on edge to delete
\returns Number of elements removed */
inline int remove(Edge *e)
{
return remove(e, e->v()) + remove(e, e->w());
}
/*! Clear the graph without deleting any edge pointer
\see delete_ptr to delete edge pointers*/
void clear()
{
for(unsigned int i = 0; i < adj.size(); i++)
{
node *cur = adj[i], *nxt = NULL;
while(cur != NULL)
{
nxt = cur->next;
delete cur;
cur = nxt;
}
adj[i] = NULL;
}
for(unsigned int i = 0; i < adj.size(); i++)
adj[i] = NULL;
}
class iterator;
friend class iterator;
class iterator_all;
friend class iterator_all;
};
/*! Iterates through the edges from a vertex */
template<class Edge = Edge_Base > class Graph_List<Edge>::iterator
{
const Graph_List<Edge> &G;
int v;
node* t; // Current node
public:
/*! Ierates through the edges from v.
If directed, all edges are out of v */
iterator(const Graph_List<Edge> &G, int v) : G(G), v(v), t(NULL) {}
/*! Begins an iteration */
inline Edge* beg() { t = G.adj[v]; return (t != NULL) ? t->e : NULL; }
/*! \returns Next edge, NULL if there is no more edge */
inline Edge* nxt() { if (t != NULL) t = t->next; return (t != NULL) ? t->e : NULL; }
/*! \returns true if there is no more edge */
inline bool end() { return (t == NULL); }
};
/*! Iterates through all edges */
template<class Edge = Edge_Base > class Graph_List<Edge>::iterator_all
{
const Graph_List<Edge> &G;
int pos;
node* t; // t is always valid
bool end_;
inline bool find_valid()
{
while(true)
{
if(t == NULL)
{
if(pos <= G.V() - 2)
t = G.adj[++pos];
else
{
end_ = true;
return false;
}
}
else if(!G.digraph && !t->e->from(pos)) // on ne visite qu'une fois chaque ar�te, si non orient�
t = t->next;
else
break;
}
return true;
}
public:
iterator_all(const Graph_List<Edge> &G) : G(G), pos(0), t(NULL) { }
/*! Begins an iteration */
inline Edge* beg()
{
end_ = false;
pos = 0;
t = G.adj[0];
if(find_valid())
return t->e;
else
return NULL;
}
/*! \returns Next edge, NULL if there is no more edge */
inline Edge* nxt()
{
t = t->next;
if(find_valid())
return t->e;
else
return NULL;
}
/*! \returns True if there is no more edge */
inline bool end()
{ return end_; }
};
/*! Matrix adjacency graph */
template<class Edge = Edge_Base > class Graph_Matrix
{
int Vcnt, Ecnt; bool digraph;
std::vector< std::vector <Edge *> > adj;
public:
/*!
\param V Number of vertices
\param digraph Specifies if the graph is directed
*/
Graph_Matrix(int V, bool digraph = false) : adj(V), Vcnt(V), Ecnt(0), digraph(digraph)
{
for (int i = 0; i < V; i++)
adj[i].assign(V, NULL);
}
/*!
Creates a matrix adjacency graph from an adjacency list graph
\warning Multiple edges in G may be deleted
*/
Graph_Matrix(const Graph_List<Edge> &G) : adj(G.V()), Vcnt(G.V()), Ecnt(0), digraph(G.directed())
{
for (int i = 0; i < Vcnt; i++)
adj[i].assign(Vcnt, NULL);
typename Graph_List<Edge>::iterator_all it(G);
for(Edge *e = it.beg(); !it.end(); e = it.nxt())
insert(e);
}
/*! \returns Number of vertices */
inline int V() const { return Vcnt; }
/*! \returns Number of edges */
inline int E() const { return Ecnt; }
/*! \returns True iff directed */
inline bool directed() const { return digraph; }
/*! Inserts an edge
\warning Existing pointer to edge may be deleted (but without deleting the corresponding edge)
*/
void insert(Edge *e)
{
int v = e->v(), w = e->w();
if (adj[v][w] == NULL) Ecnt++;
adj[v][w] = e;
if (!digraph) adj[w][v] = e;
}
/*! Removes edge e */
void remove(Edge e)
{
int v = e.v(), w = e.w();
if (adj[v][w] != NULL) Ecnt--;
adj[v][w] = NULL;
if (!digraph) adj[w][v] = NULL;
}
/*! \returns \li A pointer to a edge from v to w if it exists
\li NULL otherwise
*/
inline Edge* edge(int v, int w) const
{ return adj[v][w]; }
class iterator;
friend class iterator;
class iterator_all;
friend class iterator_all;
};
/*! Ierates through the edges from a vertex */
template<class Edge = Edge_Base > class Graph_Matrix<Edge>::iterator
{
const Graph_Matrix<Edge> &G;
int i; // Current vertex
int v;
public:
/*! Ierates through the edges from v */
iterator(const Graph_Matrix &G, int v) : G(G), v(v), i(0) { }
/*! Begins an iteration */
Edge *beg() { i = -1; return nxt(); }
/*! \returns Next edge, NULL if there is no more edge */
Edge *nxt()
{
for (i++; i < G.V(); i++)
if (G.edge(v, i)) return G.adj[v][i];
return NULL;
}
/*! \returns true if there is no more edge */
bool end() const { return i >= G.V(); }
};
/*! Ierates through all edges */
template<class Edge = Edge_Base > class Graph_Matrix<Edge>::iterator_all
{
const Graph_Matrix<Edge> &G;
int i, j;
public:
/*! Begins an iteration */
iterator_all(const Graph_Matrix &G) : G(G), j(0), i(0) { }
Edge *beg()
{
i = -1;
j = 0;
return nxt();
}
/*! \returns Next edge, NULL if there is no more edge */
Edge *nxt()
{
for(; j < G.V(); j++, i = -1)
for (i++; i < G.V(); i++)
if (G.edge(i, j)) return G.adj[i][j];
return NULL;
}
/*! \returns True if there is no more edge */
bool end() const { return j >= G.V(); }
};
/*! Bipartite adjacency list graph */
template<class Edge = Edge_Base > class Bipartite_list : public Graph_List<Edge>
{
public:
/*! List of vertices in each partition */
std::list<int> X;
/*! List of vertices in each partition */
std::list<int> Y;
/*!
\param V Number of vertices
\param digraph Specifies if the graph is directed
*/
Bipartite_list(int V, bool directed = false) : Graph_List<Edge>(V, directed), X(), Y() { };
};
/*! Determines if a graph is bipartite
\todo Comment */
template <class Edge = Edge_Base, class Graph = Graph_List<Edge> > class isBipartite
{
const Graph &G;
bool OK;
std::vector <int> vc;
bool dfsR(int v, int c)
{
vc[v] = (c+1) %2;
typename Graph::iterator A(G, v);
for (Edge *e = A.beg(); !A.end(); e = A.nxt())
{
int t = e->other(v);
if (vc[t] == -1)
{ if (!dfsR(t, vc[v])) return false; }
else if (vc[t] != c) return false;
}
return true;
}
public:
isBipartite(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; }
}
/*! \returns True if bipartite */
bool bipartite() const { return OK; }
/*! \returns The color of the vertex (0 or 1) */
int color(int v) const { return vc[v]; }
};
}
#endif