Acm Cheat Sheet

Share Embed Donate


Short Description

Descripción: jhhkhjh...

Description

ACM ICPC Cheat Sheet

University of Tehran Faculty of Engineering

Mohsen Vakilian [email protected]

November 1, 2006

1

Contents 1 Probable Mistakes

4

2 Skeleton

5

3 Java I/O

6

4 Time Measurement in C++ 4.1 Measuring Program Running Time in C/C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Creating a Simple Timer Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7 7 7

5 Number Theoretic Algorithms 5.1 The Sieve of Eratosthenes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Big Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8 8 8

6 Generating Permutations and Combinations 6.1 Generating the next largest permutation in lexicographical order . . . . . . . . . . . . . . . . . . . . . 6.2 Generating the next largest bit string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3 Generating the next r-combination in lexicographical order . . . . . . . . . . . . . . . . . . . . . . . .

13 13 13 13

7 Quicksort and Order Statistics

14

8 Dynamic Programming 8.1 Longest Increasing Subsequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15 15

9 Elementary Graph Algorithms 9.1 Breadth First Search . . . . . . . . . . . . . . . . . . . . . 9.1.1 Pseudocode . . . . . . . . . . . . . . . . . . . . . . 9.1.2 Analysis . . . . . . . . . . . . . . . . . . . . . . . . 9.1.3 Classifying edges by breadth-first search . . . . . . 9.2 Depth First Search . . . . . . . . . . . . . . . . . . . . . . 9.2.1 Pseudocode . . . . . . . . . . . . . . . . . . . . . . 9.2.2 Analysis . . . . . . . . . . . . . . . . . . . . . . . . 9.2.3 Parenthesis theorem . . . . . . . . . . . . . . . . . 9.2.4 Classification of edges . . . . . . . . . . . . . . . . 9.2.5 Theorem . . . . . . . . . . . . . . . . . . . . . . . 9.3 Topological Sort . . . . . . . . . . . . . . . . . . . . . . . 9.3.1 Pseudocode . . . . . . . . . . . . . . . . . . . . . . 9.3.2 Analysis . . . . . . . . . . . . . . . . . . . . . . . . 9.4 Strongly connected components . . . . . . . . . . . . . . . 9.4.1 Pseudocode . . . . . . . . . . . . . . . . . . . . . . 9.4.2 Analysis . . . . . . . . . . . . . . . . . . . . . . . . 9.5 Articulation points, bridges, and biconnected components 9.5.1 Articulation points . . . . . . . . . . . . . . . . . . 9.5.2 Bridges and biconnected components . . . . . . . . 9.6 All-Pairs Shortest Paths . . . . . . . . . . . . . . . . . . . 9.6.1 Floyd-Warshall . . . . . . . . . . . . . . . . . . . . 9.7 Eulerian Tours . . . . . . . . . . . . . . . . . . . . . . . . 9.7.1 Analysis . . . . . . . . . . . . . . . . . . . . . . . . 9.7.2 Nonrecursive Eulerian Tour . . . . . . . . . . . . .

18 18 18 18 18 19 19 19 19 19 20 20 20 21 21 21 21 21 21 24 24 24 24 25 25

2

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

10 Maximum Bipartite Matching 10.1 Pseudocode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.3 Implementation in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26 26 26 26

11 String Matching 11.1 Naive String Matching Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Rabin-Karp String Matching Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 The Knuth-Morris-Pratt Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

29 29 29 30

12 Geometric Algorithms 12.1 Trigonometric Functions . . . . . . 12.2 Distances . . . . . . . . . . . . . . 12.3 Intersection . . . . . . . . . . . . . 12.3.1 Point of Intersection of Two 12.4 Projection . . . . . . . . . . . . . . 12.5 Reflection . . . . . . . . . . . . . . 12.6 Area of polygon . . . . . . . . . . . 12.7 Transformations . . . . . . . . . . 12.7.1 Rotation . . . . . . . . . . . 12.7.2 Line Reflection . . . . . . . 12.7.3 Scale . . . . . . . . . . . . . 12.7.4 Perpendicular Projection . 12.7.5 Amood Monasef . . . . . . 12.8 Point in Triangle . . . . . . . . . . 12.9 Convex Hull (Graham’s scan) . . .

. . . . . . . . . . . . Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

32 32 36 37 37 39 39 39 39 39 40 40 40 40 40 40

13 Libraries 13.1 STL . . . . . . . . . . . 13.1.1 bitset . . . . . . 13.1.2 string . . . . . . 13.2 Character Classification 13.3 Streams . . . . . . . . . 13.3.1 ios-base . . . . . 13.3.2 File Streams . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

42 42 42 42 42 42 43 43

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

3

Chapter 1

Probable Mistakes • Should it really be a map or a multimap? • less than operator should impose a total ordering for the map to work. • Resolve all the compiler warnings. Pay attention to type cast conversions in STL size(). • A two dimensional array cannot be initialized by fill(). • The predicate passed to the set template must have a default constructor. • EP S ≥ 1e − 14 • Be careful about the range of arrays. [] is your enemy!

4

Chapter 2

Skeleton // S k e l e t o n . cpp #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std ; //#d e f i n e NDEBUG #i f n d e f #define #define #e l s e #define #define #endif

NDEBUG D ( args ) args D P ( exp ) { c e r r = 0 ; −−i ) { char swp = result [ i ] ; result [ i ] = ’ 0 ’ + result [ result . size ( ) − i − 1 ] ; result [ result . size ( )−i −1] = ’ 0 ’ + swp ; } i f ( nr & 1 ) result [ ( nr >> 1 ) ] += ’ 0 ’ ; i f ( negative ) result . insert ( 0 , ”−” , 0 , 1 ) ; return result ; } int main ( int argc , char ∗∗ argv ) { #define X ” 123456789 ” #define Y ” 23456789 ” #define Z ” 456789 ” BigInteger x ( X ) ; BigInteger y ( Y ) ; BigInteger z ( Z ) ; BigInteger result ; result . MultiplyAdd ( x , y ) ; result . Add ( z ) ; printf ( ”%s ∗ %s + %s = \n\ t%s \n” , X , Y , Z , result . ToString ( ) . c_str ( ) ) ; /∗ In> 123456789 ∗ 23456789 + 456789 Out> 2895899850647310 ∗/ BigInteger yacasResult ( ” 2895899850647310 ” ) ; printf ( ” Yacas s a y s \n\ t%s \n” , yacasResult . ToString ( ) . c_str ( ) ) ; return 0 ; }

12

Chapter 6

Generating Permutations and Combinations 6.1

Generating the next largest permutation in lexicographical order

Next-Permutation(a) 1  a1 a2 · · · an is a permutation of {1, 2, . . . , n} not equal to n n − 1 · · · 2 1 2 j ←n−1 3 while aj > aj+1 4 do j ← j − 1 5 j is the largest subscript with aj < aj+1 6 k←n 7 while aj > ak 8 do k ← k − 1 9  ak is the smallest integer greater than aj to the right of aj . 10 exchange aj ↔ ak 11 r ← n 12 s ← j + 1 13 while r > s 14 do exchange ar ↔ as 15 r ←r−1 16 s←s+1 17  This puts the tail end of the permutation after the jth position in increasing order.

6.2

Generating the next largest bit string

Next-Bit-String(b) 1  bn−1 bn−2 . . . b1 b0 is a bit string not equal to 11 . . . 11 2 i←0 3 while bi = 1 4 do bi ← 0 5 i←i+1 6 bi ← 1

6.3

Generating the next r-combination in lexicographical order

Next-r-Combination(a) 1  {a1 , a2 , . . . , ar } is a proper subset of {1, 2, . . . , n} not equal to {n − r + 1, . . . , n} with a1 < a2 < · · · < ar . 2 i←r 3 while ai = n − r + i 4 do i ← i − 1 5 ai ← ai + 1 6 for j ← i + 1 to r 7 do aj ← ai + j − i

13

Chapter 7

Quicksort and Order Statistics Quicksort sorts the entire array A[p . . r]. Quicksort(A, p, r) 1 if p < r 2 then q ← Partition(A, p, r) 3 Quicksort(A, p, q − 1) 4 Quicksort(A, q + 1, r) Partition rearranges the subarray A[p . . r] in place. Partition(A, p, r) 1 x ← A[r] 2 i←p−1 3 for j ← p to r − 1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i] ↔ A[j] 7 exchange A[i + 1] ↔ A[r] 8 return i + 1 Select returns the ith smallest element of the array A[p . . r]. Select(A, p, r, i) 1 if p = r 2 then return A[p] 3 q ← Partition(A, p, r) 4 k ←q−p+1 5 if i = k  the pivot value is the answer 6 then return A[q] 7 elseif i < k 8 then return Select(A, p, q − 1, i) 9 else return Select(A, q + 1, r, i − k)

14

Chapter 8

Dynamic Programming 8.1

Longest Increasing Subsequence

#define MAX 400 int data [ MAX ] [ MAX ] ; int length [ MAX ] ; /∗ l e n g t h [ i ] i s t h e l e n g t h o f t h e maximal i n c r e a s i n g s u b s e q u e n c e o f x [ 0 ] . . . x [ n−1] t h a t i n v o l v e s x [ i ] as i t s l a s t e l e m e n t . d a t a [ i ] [ 0 ] . . . d a t a [ i ] [ l e n g t h [ i ] −1] i s t h e s e q u e n c e o f members o f t h a t maximal s u b s e q u e n c e ( so t h e l a s t e l e m e n t i s i ) . ∗/ c l a s s Sequences { public : /∗ i f t h e r e a r e s e v e r a l answers o f maximum l e n g t h , r e t u r n one whose l a s t e l e m e n t i s as s m a l l as p o s s i b l e ∗/ vector LongestIncreasing ( vector &x ) { int n = x . size ( ) ; length [ 0 ] = 0 ; length [ 1 ] = 1 ; int i , j , k ; data [ 0 ] [ 0 ] = 0 ; length [ 0 ] = 1 ; f o r ( i = 1 ; i < n ; i++) { int max1 = 0 ; // Find t h e max l e n g t h o f i n c r e a s i n g s u b s e q u e n c e s o f x [ 0 ] . . . x [ i −1] e n d i n g in values < x [ i ] int index1 = −1; f o r ( j = 0 ; j < i ; j++) { if (x [ j ] < x [ i ]) { i f ( max1 < length [ j ] && x [ j ] < x [ i ] ) { max1 = length [ j ] ; index1 = j ; } } } i f ( index1 == −1) { length [ i ] = 1 ; data [ i ] [ 0 ] = x [ i ] ; } 15

else { length [ i ] = max1 + 1 ; for ( k = 0 ; k < max1 ; k++) { data [ i ] [ k ] = data [ index1 ] [ k ] ; } data [ i ] [ max1 ] = i ; } } //Now d a t a i s c o m p l e t e int max = 0 ; int index = −1; f o r ( i = 0 ; i < n ; i++) { i f ( length [ i ] > max ) { max = length [ i ] ; index = i ; } } vector < int >ans ; f o r ( j = 0 ; j < length [ index ] ; j++) { ans . push_back ( data [ index ] [ j ] ) ; } return ans ; } }; int input [ ] = { 5 , 1 , 6 , 7 , 3 , 4 , 5 } ; int main ( ) { Sequences z ; vector x ; int i , j ; f o r ( i = 0 ; i < s i z e o f ( input ) / s i z e o f ( int ) ; i++) x . push_back ( input [ i ] ) ; vector ans ; ans = z . LongestIncreasing ( x ) ; int n = x . size ( ) ; printf ( ” \ nInput : ” ) ; f o r ( i = 0 ; i < n ; i++) printf ( ”%d ” , x [ i ] ) ; f o r ( i = 0 ; i < n ; i++) { printf ( ” \ nending a t [%d ] = %d” , i , x [ data [ i ] [ 0 ] ] ) ; f o r ( j = 1 ; j < length [ i ] ; j++) printf ( ” , %d” , x [ data [ i ] [ j ] ] ) ; } printf ( ” \n answer i s ” ) ; int m = ans . size ( ) ; f o r ( i = 0 ; i < m ; i++) printf ( ”%d ” , x [ ans [ i ] ] ) ; return 0 ;

16

}

17

Chapter 9

Elementary Graph Algorithms 9.1

Breadth First Search

The breadth-first-search procedure BFS below assumes that the input graph G = (V, E) is represented using adjacency lists.

9.1.1

Pseudocode

BFS(G, s) 1 for each vertex u ∈ V [G] − {s} 2 do color[u] ← white 3 d[u] ← ∞ 4 π[u] ← nil 5 color[s] ← gray 6 d[s] ← 0 7 π[s] ← nil 8 Q←∅ 9 Enqueue(Q, s) 10 while Q 6= ∅ 11 do u ← Dequeue(Q) 12 for each v ∈ Adj[u] 13 do if color[v] = white 14 then color[v] ← gray 15 d[v] ← d[u] + 1 16 π[v] ← u 17 Enqueue(Q, v) 18 color[u] ← black Print-Path(G, s, v) 1 if v = s 2 then print s 3 else if π[v] = nil 4 then print “no path from” s “to” v “exists” 5 else Print-Path(G, s, π[v]) 6 print v

9.1.2

Analysis

Because the adjacency list of each vertex is scanned at most once, the total time spent in adjacency lists is O(E). The overhead for initialization is O(V ), and thus the total running time of BFS is O(V + E).

9.1.3

Classifying edges by breadth-first search

A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source of the search into the same four categories. 1. Prove that in a breadth-first search of an undirected graph, the following properties hold:

18

(a) There are no back edges and no forward edges. (b) For each tree edge (u, v), we have d[v] = d[u] + 1. (c) For each cross edge (u, v), we have d[v] = d[u] or d[v] = d[u] + 1. 2. Prove that in a breadth-first search of a directed graph, the following properties hold: (a) There are no forward edges. (b) For each tree edge (u, v), we have d[v] = d[u] + 1. (c) For each cross edge (u, v), we have d[v] ≤ d[u] + 1. (d) For each back edge (u, v), we have 0 ≤ d[v] ≤ d[u].

9.2

Depth First Search

Vertex u is white before time d[u], gray between time d[u] and time f [u], and black thereafter. The variable time is a global variable that we use for timestamping.

9.2.1

Pseudocode

DFS(G) 1 for each vertex u ∈ V [G] 2 do color[u] ← white 3 π[u] ← nil 4 df sT ime ← 0 5 for each vertex u ∈ V [G] 6 do if color[u] = white 7 then DFS-Visit(u) DFS-Visit(u) 1 color[u] ← gray  White vertex u has just been discovered. 2 d[u] ← df sT ime ← df sT ime + 1 3 for each v ∈ Adj[u]  Explore edge (u, v). 4 do if color[v] = white 5 then π[v] ← u 6 DFS-Visit(v) 7 color[u] ← black  Blacken u; it is finished. 8 f [u] ← df sT ime ← df sT ime + 1

9.2.2

Analysis

The running time of DFS is Θ(V + E).

9.2.3

Parenthesis theorem

In any depth-first search of a (directed or undirected) graph G = (V, E), for any two vertices u and v, exactly one of the following three conditions holds: • the intervals [d[u], f [u]] and [d[v], f [v]] are entirely disjoint, and neither u nor v is a descendant of the other in the depth-first forest, • the interval [d[u], f [u]] is contained entirely within the interval [d[v], f [v]], and u is a descendant of v in a depth-first tree, or • the interval [d[v], f [v]] is contained entirely within the interval [d[u], f [u]], and v is a descendant of u in a depth-first tree, or

9.2.4

Classification of edges

We can define four edge types in terms of the depth-first forest Gπ produced by a depth-first search on G. 1. Tree edges are edges in the depth-first forest Gπ . Edge (u, v) is a tree edge if v was first discovered by exploring edge (u, v).

19

2. Back edges are those edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree. Self-loops, which may occur in directed graphs, are considered to be back edges. 3. Forward edges are those nontree edges (u, v) connecting a vertex u to a descendant v in a depth-first tree. 4. Cross edges are all other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees. The DFS algorithm can be modified to classify edges as it encounters them. The key idea is that each edge (u, v) can be classified by the color of the vertex v that is reached when the edge is first explored (except that forward and cross edges are not distinguished): 1. white indicates a tree edge, 2. gray indicates a back edge, and 3. black indicates a forward or cross edge. In an undirected graph, there may be some ambiguity in the type classification, since (u, v) and (v, u) are really the same edge. In such a case, the edge is classified as the first type in the classification list that applies. Equivalently, the edge is classified according to whichever of (u, v) or (v, u) is encountered first during the execution of the algorithm. Edge (u, v) is 1. a tree edge or forward edge if and only if d[u] < d[v] < f [v] < f [u], 2. back edge if and only if d[v] < d[u] < f [u] < f [v], and 3. a cross edge if and only if d[v] < f [v] < d[u] < f [u].

9.2.5

Theorem

In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge.

9.3

Topological Sort

A topological sort of a dag G = (V, E) is a linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v in the ordering.

9.3.1

Pseudocode

Here are two possible algorithms to solve this problem: Topological-Sort(G) 1 call DFS(G) to compute finishing times f [v] for each vertex v 2 as each vertex is finished, insert it onto the front of a linked list 3 return the linked list of vertices Topological-Sort(G) 1  The d array indicates a topological sorting of G. 2 initialize indegree[v] for all vertices 3 time ← 0 4 Q←∅ 5 for i ← 1 to n 6 do if indegree[vi ] = 0 7 then Enqueue(Q, vi ) 8 repeat v ← Dequeue(Q) 9 time ← time + 1 10 d[v] ← time 11 for all edges (v, w) 12 do indegree[w] ← indegree[w] − 1 13 if indegree[w] = 0 14 then Enqueue(Q, w) 15 until Q = ∅

20

9.3.2

Analysis

We can perform a topological sort in time Θ(V + E).

9.4

Strongly connected components

A strongly connected component of a directed graph G = (V, E) is a maximal set of vertices C ⊆ V such that for every pair of vertices u and v in C, we have both u ; v and v ; u; that is, vertices u and v are reachable from each other.

9.4.1

Pseudocode

Strongly-Connected-Components(G) 1 call DFS(G) to compute finishing times f [u] for each vertex u 2 compute GT 3 call DFS(GT ), but in the main loop of DFS, consider the vertices in order of decreasing f [u] (as computed in line 1) 4 output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component

9.4.2

Analysis

The above algorithm is linear (Θ(V + E)-time).

9.5

Articulation points, bridges, and biconnected components

Let G = (V, E) be a connected, undirected graph. An articulation point of G is a vertex whose removal disconnects G. A bridge of G is an edge whose removal disconnects G. A biconnected component of G is a maximal set of edges such that any two edges in the set lie on a common simple cycle. We can determine articulation points, bridges, and biconnected components using depth-first search. Let Gπ = (V, Eπ ) be a depth-first tree of G.

9.5.1

Articulation points

1. Prove that the root of Gπ is an articulation point of G if and only if it has at least two children in Gπ . 2. Let v be a nonroot vertex of Gπ . Prove that v is an articulation point of G if and only if v has a child s such that there is no back edge from s or any descendant of s to a proper ancestor of v. 3. Let

 low[v] = min

d[v], d[w] : (u, w) is a back edge for u = v or some descendant u of v.

4. Show how to compute low[v] for all vertices v ∈ V in O(E) time. 5. Show how to compute all articulation points in O(E) time. #include #include using namespace std ; #define #define #define #define #define #define

INF 1000000000 MAX 1000 NOPAR −1 WHITE 0 GRAY 1 BLACK 2

int g [ MAX ] [ MAX ] ; int n ; // number o f v e r t i c e s . ( Note : v e r t i c e s a r e numbered from 1 t o n b u t t h i s can be e a s i l y changed by c h a n g i n g l o o p s ) int color [ MAX ] , p [ MAX ] , d [ MAX ] , f [ MAX ] ;

21

int dfsTime ; // These a r e used t o d e l e t e and r e s t o r e v e r t i c e s bool deleted [ MAX ] ; int backup [ MAX ] ; int low [ MAX ] , nchilds [ MAX ] , iscut [ MAX ] ; void init ( ) { int i , j ; f o r ( i = 0 ; i < MAX ; ++i ) f o r ( j = 0 ; j < MAX ; ++j ) g [ i ] [ j ] = 0; n = 0; fill ( deleted , deleted + MAX , true ) ; } void readNext ( ) { /∗ Remember t o 1 . S e t ‘ n ’ ( t h e number o f v e r t i c e s ) 2 . Keep t h e symmetry o f t h e a d j a c e n c y m a t r i x 3 . S e t t h e d e l e t e d [ ] i f i t ’ s needed ∗/ // . . . } // Returns t h e a d j a c e n t s o f u vector adjs ( int u ) { vector a ; f o r ( int v = 1 ; v = 1 ) a . push_back ( v ) ; } return a ; } void delVertex ( int u ) { f o r ( int v = 1 ; v
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF