Zoj 2588 Burning Bridges "dual connectivity components for bridge output bridge number"

Source: Internet
Author: User
Tags integer numbers

Burning Bridges Time limit: 5 Seconds Memory Limit: 32768 KB

Ferry Kingdom is a nice little country located on N Islands that's connected by M bridges. All bridges is very beautiful and is loved by everyone in the kingdom. Of course, the system of bridges is designed in such a-on-one, can-get-from-any-to-any other one.

But recently the great sorrow have come to the kingdom. Ferry Kingdom was conquered by the armies of the great warrior Jordan and he had decided to burn all the bridges that conn Ected the islands. This is a very cruel decision, but the Wizards of Jordan has advised him no to does so, because after that he own armies Would not being able to get from one island to another. So-Jordan decided to burn as many bridges as possible so that's was-still possible for he armies to get from any island to any other one.

Now the poor people of Ferry kingdom wonder what bridges would be burned. Of course, they cannot learn, because the list of bridges to be burned are kept in great secret. However, one old man said so can help them to find the set of bridges that certainly would not be burned.

So they came to you and asked. Can do?

Input

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

The first line of all case contains N and m-the number of islands and bridges in Ferry Kingdom respectively (2 <= N <=, 1 <= M <= 100 000). Next M lines contain the different integer numbers each and describe bridges. Note that there can is several bridges between a pair of islands.

Output

On the first line of all case print k-the number of bridges that would certainly not be burned. On the second line print K integers-the numbers of these bridges. Bridges is numbered starting from one, as they is given in the input.

The consecutive cases should is separated by a and blank line. No blank line should is produced after the last Test case.

Sample Input

26 71 22 32 45 41 34 53 610 162 63 76 55 95 41 29 86 42 103 87 91 42 410 51 66 10

Sample Output


Test instructions: N Islands m -bridge with heavy edges, now burn the useless bridge (preserving the cut edge of the double-connected component) to let you output the total number of cut edges and number

Solving the bridge output (pay attention to the processing of the heavy edges)
#include <stdio.h> #include <string.h> #include <algorithm> #include <stack> #define MAX 200100using namespace std;struct node{int beg,end,chon,next;int ID;} Edge[max];int dfsclock;int n,m,ans;int low[max],dfn[max];int head[max],mark;int bridge[MAX],sum;void init () {ans=0; Sum=0;memset (Bridge,0,sizeof (bridge)); Memset (Head,-1,sizeof (Head));} void Add (int u,int v,int ID) {int i;for (i=head[u];i!=-1;i=edge[i].next)//handle heavy Edge {if (edge[i].end==v) break;} The IF (i!=-1)//i!=-1 proves that all edges are not processed (that is, a heavy edge is found) edge[i].chon=1;//marks a heavy edge else{edge[ans].beg=u;edge[ans].end=v;edge[ans].chon=0; edge[ans].id=id;edge[ans].next=head[u];head[u]=ans++;}} void Getmap () {int a,b;for (int i=1;i<=m;i++) {scanf ("%d%d", &a,&b); add (a,b,i); add (b,a,i);}} void Tarjan (int u,int fa) {int v;dfn[u]=low[u]=++dfsclock;for (int i=head[u];i!=-1;i=edge[i].next) {v=edge[i].end;if (v ==FA) continue;if (!dfn[v]) {Tarjan (v,u); Low[u]=min (Low[u],low[v]); if (dfn[u]<low[v]&&edge[i].chon==0)/    /has a bridge and is not a heavy edge {bridge[sum++]=edge[i].id;} }eLSE Low[u]=min (LOW[U],DFN[V]);}} void Find () {memset (low,0,sizeof (Low)), memset (dfn,0,sizeof (DFN));d Fsclock=0;tarjan (1,-1);} void Solve () {printf ("%d\n", sum), if (sum==0) return, sort (bridge,bridge+sum), for (int i=0;i<sum;i++) {if (!i) printf ( "%d", bridge[i]); else printf ("%d", Bridge[i]);} printf ("\ n");}    int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d%d", &n,&m); init (); Getmap (); find (); solve (); if (t) printf ("\ n")//Note output format}return 0;}

  

Zoj 2588 Burning Bridges "dual connectivity components for bridge output bridge number"

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.