Reprinted please indicate the source: Thank youHttp://blog.csdn.net/lyy289065406/article/details/6762432
General question:
In order to protect the grazing environment and prevent livestock from biting the turf of the same place excessively, the farm owner decided to use continuous migration of livestock for feeding to protect the pasture. However, during the migration process, livestock will also eat pasture on the road. Therefore, if the same road is used for each migration, the road will also be damaged by excessive biting.
Now, a farm owner owns F farms. It is known that these farms have at least one path to connect (not necessarily directly), but at least one road is accessible from some farms to other farms. To protect the pasture on the road, the farmer hopes to build several more roads so that there are at least two migration approaches for each livestock migration, so as to avoid repeated migration. If we know the current R Road, how many new roads should the farmer build to meet the requirements?
Solution:
"There are at least two migration methods for each migration of livestock to avoid repeated migration steps ." That is to say, when F farms are regarded as vertices and roads as edges to construct an undirected graph G, graph G does not have a bridge.
You can create a model:
Given a connected undirected graph G, you must add at least several edges to change it to a dual connected graph.
This question is exactly the same as that of poj3352, but the expression is different.
For details about the problem solving process, see my poj3352 Problem Solving report.
Portal:
My poj3353 Problem Solving report:
Http://blog.csdn.net/lyy289065406/article/details/6762370
After reading the poj3353 solution report, I will come back to see some points of attention in this question:
The biggest difference between this question and 3353 is that 3353 ensures that there is no overlap, and this question has a overlap.
As mentioned in the 3353 issue-solving report, when there is a duplicate edge between two points, we cannot use the low value to divide the [edge dual-Connected Component, at this time, the two points of different low values may belong to the same [edge dual-connectivity component ]!
So how can we solve the problem of overlap?
1. When constructing the graph G, the duplicate edge is also taken into account, and then the bridge is deleted and then divided when the edge is divided into two connected components, the cut points at one end of the bridge are classified as the edge dual-connectivity component currently being divided. This process is troublesome;
2. When the edge of graph G is input, if a duplicate edge is displayed, the duplicate edge is not placed in graph G. Then, the dual-connected component of the edge is still divided by low.
The two are mutually exclusive, of course, the 2nd methods are better handled.
In fact, if you use an adjacent matrix to store graph G, you do not need to consider the problem of duplicate edges.
However, those who store graph G with an adjacent linked list have to consider it, because they basically insert edges in the linked list using the header insertion method, so they cannot detect the duplicate edges. If you use the tail insert method, you can add a judgment when the pointer moves from the chain header to the end of the chain. If there is a duplicate edge, it will not be inserted. Otherwise, it will be inserted to the end.
// Memory time // 236 K 16 Ms # include <iostream> using namespace STD; Class node {public: int ID; Class node * Next; node (): ID (0), next (0) {}}; class solve {public: Solve (int f, int R): F (F), R (r) {initial (); input_creat (); Tarjan (1,-1); // The graph G given in this question is connected, therefore, you can search for printf ("% d \ n", bcc_sp_d_e ();} ~ from any node ());}~ Solve () {Delete [] dfn; Delete [] low; Delete [] degree; emptylist () ;}int min (int A, int B) const {return a <B? A: B;} void initial (void); // apply for the bucket and initialize void input_creat (void); // enter and create the figure gvoid addedge (int A, int B ); // insert edge a to the linked list <-> B (tail insertion method to avoid duplicate edge) void Tarjan (INT S, int father); // calculate the low [] array, int bcc_sp_d_e (void), double-connected component of all edges; // construct each edge double-connected component (BCC) as a contraction point (SP ), calculate the degree (D) of each contraction point. // the return value is the number of void dellink (node * P) that makes the graph G the minimum number of edges (e) to be added for the dual-connection ); // release the entire chain void emptylist (void) with P as the header; // release the adjacent linked list protected: int F; // The number of islandsint R; // The number of roadsnode ** Li Nkhead; // int timestamp; // (external) timestamp int * dfn; // dfn [u]: search order of node u (timestamp) int * low; // low [u]: the number of the first node in the stack that can be traced back to the subtree of node u or U. int * degree; // record the total degree of each contraction point}; void solve: initial (void) {linkhead = new node * [F + 1]; for (INT I = 1; I <= f; I ++) linkhead [I] = 0; timestamp = 0; dfn = new int [F + 1]; Low = new int [F + 1]; memset (dfn, 0, sizeof (INT) * (F + 1); memset (low, 0, sizeof (INT) * (F + 1 )); degree = new int [F + 1]; memset (degree, 0, sizeof (INT) * (F + 1); return;} Vo Id solve: input_creat (void) {int A, B; node * TMP; For (Int J = 1; j <= r; j ++) {scanf ("% d", & A, & B); If (! Linkhead [a]) linkhead [a] = new node; If (! Linkhead [B]) linkhead [B] = new node; addedge (a, B);} return;} void solve: addedge (int A, int B) {node * pA = linkhead [a]; node * P1 = new node; P1-> id = B; while (Pa-> next) {Pa = pa-> next; if (Pa-> id = p1-> ID) // If a duplicate edge is displayed, return is not inserted into the linked list.} pa-> next = p1; node * pb = linkhead [B]; node * P2 = new node; P2-> id = A; while (Pb-> next) // It can be executed here to explain that a <-> B is not a duplicate edge Pb = Pb-> next; // you can directly search for the end of the chain table and insert Pb-> next = P2; return ;} void solve: Tarjan (int s, int father) {dfn [s] = low [s] = ++ times Tamp; For (node * P = linkhead [s]-> next; P = p-> next) {int T = p-> ID; If (T! = Father & dfn [T] <dfn [s]) {If (dfn [T] = 0) // s-> T is the branch edge {Tarjan (t, s); low [s] = min (low [s], low [T]);} else // s-> T is the backward side {LOW [s] = min (low [s], dfn [T]) ;}}return ;} int solve :: bcc_sp_d_e (void) {for (INT I = 1; I <= f; I ++) if (linkhead [I]) {for (node * P = linkhead [I]-> next; P = p-> next) // enumerate the connected vertex I in graph G <-> J {// Since graph G is an undirected graph, the connected vertex is a bidirectional Int J = p-> ID; if (low [I]! = Low [J]) // The two points with the same low value in graph G must be in the same edge dual-connected component (that is, the same contraction point) {// check if I and j are not in the same shrink point degree [low [I] ++; // The degree of contraction of node I + 1 degree [low [J] ++; // degree of contraction of node J + 1 }}int leave = 0; // The total number of records = 1 (leaf) shrinkage point for (int K = 1; k <= f; k ++) // enumerate the degree of each contraction point DIF (degree [k]/2 = 1) // as it is an undirected graph, therefore, the degree of each contraction point is calculated twice. After the sum of 2, the true degree leave ++; Return (leave + 1)/2 is obtained; // Number of edges to be added for connecting a tree into an edge pair Connected Component = (number of leaf nodes + 1)/2} void solve: dellink (node * P) {If (p-> next) P = p-> next; Delete [] P; return;} void solve: emptylist (voi D) {for (INT I = 1; I <= f; I ++) if (linkhead [I]) dellink (linkhead [I]); return ;} int main (void) {int F, R; while (scanf ("% d", & F, & R )! = EOF) Solve poj3177 (F, R); Return 0 ;}
Sample Input
7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
7
1 2
2 3
3 4
2 5
4 5
3 6
5 7
6 5
3 1
3 2
3 4
3 5
3 6
10 12
1 2
2 3
3 1
3 4
4 8
4 5
5 6
6 7
7 5
8 9
9 10
10 8
10 10
1 8
6 3
7 1
3 5
5 2
2 9
9 7
8 4
4 10
10 6
10 9
1 2
7 4
9 6
10 6
8 4
3 5
3 4
3 6
1 3
16 22
1 3
7 1
5 1
12 7
6 3
4 7
8 3
10 7
14 6
11 5
9 7
15 4
2 6
13 12
8 2
2 11
6 1
4 11
1 14
3 10
13 16
13 16
27 35
1 3
10 3
22 3
15 3
11 15
5 15
12 22
18 10
23 11
7 1
2 15
25 1
14 10
24 11
8 2
19 22
4 12
16 4
13 18
9 14
21 13
6 4
17 23
20 17
17 6
3 21
20 3
9 13
17 12
20 18
2 26
26 27
27 8
2 27
26 8
75 81
1 3
58 3
37 1
36 58
15 36
9 58
10 37
8 1
44 10
33 44
61 8
54 9
4 3
20 44
53 37
26 20
67 54
71 20
5 3
70 54
45 67
14 54
74 67
41 53
52 37
63 53
31 20
55 63
60 55
40 5
75 58
62 31
68 52
72 36
49 9
66 31
43 41
22 52
35 8
21 45
30 15
11 61
7 68
57 30
12 40
27 71
25 40
46 66
42 61
24 37
29 4
59 11
16 74
47 5
69 74
64 59
56 75
19 9
48 56
23 9
13 72
2 43
32 1
73 13
28 7
6 45
18 4
38 42
50 72
17 53
39 50
51 63
34 25
65 64
67 41
58 5
5 27
75 63
7 50
20 18
38 65
200 250
1 3
106 1
134 1
157 134
23 106
60 134
44 60
117 1
126 1
11 134
139 44
178 3
97 60
101 157
118 44
30 23
128 30
174 3
108 23
110 128
132 157
92 106
173 132
79 106
82 178
7 44
52 79
74 30
4 23
49 7
164 139
127 30
156 4
65 7
120 101
46 97
112 178
8 46
59 60
198 174
100 134
90 92
192 60
125 100
26 178
19 192
63 125
155 126
70 100
35 63
151 126
165 157
146 70
84 157
141 52
160 70
163 8
38 127
171 139
62 101
133 11
177 146
158 125
41 165
145 52
98 30
5 177
68 164
168 173
107 178
86, 132
199 127
136 168
71 155
50 128
189 35
193 46
105 70
195 189
89 158
69 177
190 50
28 19
21 92
93 71
170 86
122 21
131 136
197 158
16 108
33 195
18 164
196 141
94 92
61 79
149 26
169 193
124 163
78 189
147 108
150 49
129 70
77 168
194 18
54 100
140 127
24 196
109 158
2 97
17 195
64 28
115 174
185 41
81 141
45 62
180 18
167 109
27 65
123 140
188 77
91 129
73 110
76 173
14 149
103 105
51 11
57 84
58 101
148 193
43 156
162 109
22 61
179 52
67 74
200 117
6 167
119 192
113 41
184 16
32 98
39 160
75 32
175 98
121 78
183 26
47 174
102 79
83 23
172 127
176 74
138 121
182 90
29 156
153 183
114 162
152 47
15 136
12 64
143 155
161 89
99 90
87 114
25 193
144 86
137 64
135 52
56 14
55 112
20 71
142 5
34 126
116 56
40 79
130 89
187 49
85 62
111 136
191 39
166 16
159 120
13 50
95 55
154 33
96 171
181 115
88 21
80 24
48 14
72 21
31 67
9 31
66 143
37 117
104 56
36 86
42 125
186 33
10 184
53 18
164 64
136 63
77 25
128 105
133 147
130 1
67 161
10 132
190 173
195 80
123 1
70 82
126 38
163 7
193 17
152 105
44 24
168 185
174 163
177 40
79 173
70 19
26 60
198 130
97 22
143 67
97 25
119 89
194 163
188 180
49 173
109 71
4 124
58 79
151 178
74 93
34 96
161 65
167 16
172 114
183 14
46 116
199 187
118 175
109 23
101 115
160 114
110 173
96 28
77 182
27 116
14 16
1 2
1 12
12 2
3 2
4 3
4 5
4 6
6 14
7 14
7 6
7 8
7 9
10 9
11 10
11 13
13 10
Sample output
2
2
3
2
0
3
2
4
16
32
2