Zoj2588 undirected graph side-cutting Tarjan Algorithm

Source: Internet
Author: User

Burning bridges

Time limit:
5 seconds memory limit:

32768 KB

Ferry
Kingdom
Is a nice little country located on N islands that are
Connected by M bridges. All bridges are very beautiful and are loved
Everyone in the Kingdom. Of course, the system of bridges is designed in such
Way that one can get from any island to any other one.

But recently the great sorrow has come to the Kingdom. Ferry kingdom
Was conquered by the armies of the great warrior Jordan and he has decided
Burn all the bridges that connected the islands. This was a very cruel
Deploy, but the Wizards of Jordan
Have advised him no to do so, because after that his own armies wocould not be
Able to get from one island to another. So Jordan decided to burn as login
Bridges as possible so that is was still possible for his armies to get from
Any island to any other one.

Now the poor people of ferry
Kingdom wonder what
Bridges will be burned. Of course, they cannot learn that, because the list
Bridges to be burned is kept in great secret. However, one old man said that
You can help them to find the set of bridges that certainly will not be burned.

So they came to you and asked for help. Can you do that?

 

Input

The input contains multiple test cases. The first line of the input is a single
Integer T (1 <= T <= 20) which is the number of test cases. t test cases
Follow, each preceded by a single blank line.

The first line of each case contains N and M-the number of islands and
Bridges in ferry Kingdom respectively (2 <= n <= 10
000, 1 <= m <= 100 000). Next m lines contain two different integer
Numbers each and describe bridges. Note that there can be several bridges
Between a pair of islands.

 

Output

On the first line of each case print K-the number of bridges that will
Certainly not be burned. On the second line print K integers-the numbers
These bridges. bridges are numbered starting from one, as they are given in
Input.

Two consecutive cases shocould be separated by a single blank line. No blank
Line shoshould be produced after the last test case.

 

Sample Input

2

 

6 7

1 2

2 3

2 4

5 4

1 3

4 5

3 6

 

10 16

2 6

3 7

6 5

5 9

5 4

1 2

9 8

6 4

2 10

3 8

7 9

1 4

2 4

10 5

1 6

6 10

 

Sample output

2

3 7

 

1

4

 

 

Evaluate all cut edges of an undirected graph
,
Used
Tarjan
Algorithm
,
Due to memory restrictions
,
The adjacent matrix cannot be used.
,
Only an adjacent table can be used.
.

The Code is as follows:
:

 

# Include <stdio. h> <br/> # include <string. h> <br/> # include <stack> <br/> # include <algorithm> <br/> Using STD: sort; <br/> # define maxn 10005 <br/> using namespace STD; <br/> int low [maxn], dfn [maxn], n, CNT, total, res [maxn]; <br/> bool mark [maxn], vis [maxn]; <br/> stack <int> S; <br/> int min (int, int B) <br/>{< br/> return a <B? A: B; <br/>}< br/> struct e <br/>{< br/> int nod; <br/> int num; <br/> bool dB; <br/> E * Next; <br/>} head [10 * maxn]; <br/> void Tarjan (int u) <br/>{< br/> int V; <br/> E * P; <br/> low [u] = dfn [u] = ++ CNT; <br/> S. push (U); <br/> vis [u] = 1; <br/> MARK [u] = 1; <br/> for (P = head [u]. next; P! = NULL; P = p-> next) <br/>{< br/> V = p-> nod; <br/> If (! Mark [v]) <br/>{< br/> If (p-> DB = 0) <br/>{< br/> E * s = new E; <br/> S = & head [v]; <br/> while (S-> next-> nod! = U) S = s-> next; <br/> E * t = new E; <br/> T = s-> next; <br/> S-> next = s-> next; <br/> Delete (t); <br/>}< br/> Tarjan (v ); <br/> low [u] = min (low [u], low [v]); <br/>}< br/> else if (vis [v]) low [u] = min (low [u], dfn [v]); <br/>}< br/> If (low [u] = dfn [u]) <br/>{< br/> while (! S. Empty () & S. Top ()! = U) <br/>{< br/> vis [S. top ()] = 0; <br/> S. pop (); <br/>}< br/> vis [S. top ()] = 0; <br/> S. pop (); <br/>}< br/> bool find (int A, int B) <br/>{< br/> E * P = new E; <br/> P = head [A]. next; <br/> while (P! = NULL & P-> nod! = B) P = p-> next; <br/> If (P! = NULL & P-> Nod = B) Return 0; <br/> else return 1; <br/>}< br/> int main () <br/>{< br/> int U, V; <br/> E * s, * t; <br/> int I, m, a, B, CAS; <br/> scanf ("% d", & CAS); <br/> while (CAS --) <br/>{< br/> total = 0; <br/> memset (low, 0, sizeof (low); <br/> memset (dfn, 0, sizeof (dfn); <br/> memset (mark, 0, sizeof (Mark); <br/> memset (VIS, 0, sizeof (VIS); <br/> memset (Head, 0, sizeof (head )); <br/> scanf ("% d", & N, & M); <br/> For (I = 1; I <= m; ++ I) <br/> {<br/> scanf ("% d", & A, & B ); <br/> If (find (a, B) <br/>{< br/> S = new E; <br/> S-> Nod = B; <br/> S-> DB = 0; <br/> S-> num = I; <br/> S-> next = head [A]. next; <br/> head [A]. next = s; <br/> S = new E; <br/> S-> Nod = A; <br/> S-> DB = 0; <br/> S-> num = I; <br/> S-> next = head [B]. next; <br/> head [B]. next = s; <br/>}< br/> else <br/> {<br/> S = & head [a]; <br/> while (S-> next! = NULL & S-> nod! = B) S = s-> next; <br/> S-> DB = 1; <br/> S = & head [B]; <br/> while (S-> next! = NULL & S-> nod! = A) S = s-> next; <br/> S-> DB = 1; <br/>}< br/> Tarjan (1 ); // The Tarjan algorithm assists in edge Cutting <br/> total = 0; <br/> int find = 0; <br/> for (I = 1; I <= N; ++ I) <br/>{< br/> S = new E; <br/> S = head [I]. next; <br/> while (s! = NULL) <br/>{< br/> If (dfn [I] <low [S-> nod]) <br/>{< br/> total ++; <br/> res [total] = s-> num; <br/>}< br/> S = s-> next; <br/>}< br/> sort (RES + 1, Res + total + 1); <br/> printf ("% d/N ", total); <br/> for (I = 1; I <= total; ++ I) <br/> {<br/> printf ("% d ", res [I]); <br/> if (I! = Total) printf (""); <br/> else printf ("/N"); <br/>}< br/> for (I = 1; I <= N; ++ I) // deletes the adjacent table. Otherwise, the overmemory <br/> {<br/> S = head [I]. next; <br/> T = s; <br/> while (T! = NULL) <br/>{< br/> S = T; <br/> T = T-> next; <br/> Delete (s ); <br/>}< br/> If (CAS! = 0) printf ("/N"); <br/>}< br/> return 0; <br/>}< br/>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.