zoj2588 (connected components, solving the cut edges of undirected graphs)

Source: Internet
Author: User
B-burning Bridges Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%lld &%llu Submit Status

Description

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

2

6 7
1 2
2 3
2 4 5
4
1 3
4 5
3 6

ten
2 6 3
7
6 5
5 9 5 4
  1 2
9 8
6 4
2 ten
3 8 7
9
1 4
2 4 5 1 6 6 10

Sample Output

2
3 7)

1
4 


The topic requires a clipping edge of the graph, and the output, Tarjan algorithm template.

For the non-graph storage two times u->v,v->u with the VIS mark is used, in the algorithm, U->v edge, if LOW[V] > Dnf[u], it proves that u->v is cut edge.

#include <cstdio> #include <cstring> #include <stack> #include <algorithm> using namespace std;
    #define MAXN 11000 struct node{int u, v;
int next;
} edge[210000];
int HEAD[MAXN], CNT, vis[210000];
int DNF[MAXN], LOW[MAXN], time;
int P[MAXN], ans;
Stack <int> STA;
    void Add (int u,int v) {edge[cnt].u = u; edge[cnt].v = v; Edge[cnt].next = Head[u];
    Head[u] = cnt++; edge[cnt].u = v;
    EDGE[CNT].V = u; Edge[cnt].next = Head[v];
HEAD[V] = cnt++;
    } void Tarjan (int u) {dnf[u] = low[u] = ++time;
    int V, I, J;
        for (i = head[u]; i =-1; i = edge[i].next) {if (vis[i]) continue;
        Vis[i] = vis[i^1] = 1;
        v = edge[i].v;
            if (!dnf[v]) {Sta.push (i);
            Tarjan (v);
            Low[u] = min (low[u],low[v]);
            if (Low[v] > Dnf[u]) {p[ans++] = i/2+1; }} else if (Dnf[v] < DNf[u]) {Sta.push (i);
        Low[u] = min (low[u],dnf[v]);
    }}} int main () {int T, n, M, I, u, v;
    scanf ("%d", &t);
        while (t--) {memset (head,-1,sizeof (head));
        memset (vis,0,sizeof (VIS));
        memset (dnf,0,sizeof (DNF));
        memset (low,0,sizeof (Low));
        memset (instack,0,sizeof (instack));
        CNT = ans = time = 0;
        scanf ("%d%d", &n, &m);
            while (m--) {scanf ("%d%d", &u, &v);
        Add (U,V);
        } while (!sta.empty ()) Sta.pop ();
        Tarjan (1);
        printf ("%d\n", ans);
        Sort (P,p+ans);
            for (i = 0; i < ans; i++) {if (i = = ans-1) printf ("%d\n", P[i]);
        else printf ("%d", p[i]);
    } if (t) printf ("\ n");
} return 0;
 }

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.