ZOJ2588 無向圖求割邊 tarjan演算法

來源:互聯網
上載者: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 by everyone in the kingdom. Of course, the system of bridges is designed in such a 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 to burn all the bridges that connected the islands. This was a very cruel decision, but the wizards of Jordan have advised him no to do so, because after that his own armies would not be able to get from one island to another. So Jordan decided to burn as many 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 of 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 of these bridges. Bridges are numbered starting from one, as they are given in the input.

Two consecutive cases should be separated by a single blank line. No blank line should 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

 

 

求無向圖的所有割邊 , 用了 tarjan 演算法 , 由於記憶體的限制 , 無法使用鄰接矩陣 , 只能用鄰接表了 .

代碼如下 :

 

#include<stdio.h> #include<string.h> #include<stack> #include<algorithm> using std::sort; #define MAXN 10005 using namespace std; int low[MAXN],dfn[MAXN],n,cnt,total,res[MAXN]; bool mark[MAXN],vis[MAXN]; stack <int> s; int min(int a,int b) { return a<b?a:b; } struct E { int nod; int num; bool db; E *next; } head[10*MAXN]; void tarjan(int u) { int v; E *p; low[u]=dfn[u]=++cnt; s.push(u); vis[u]=1; mark[u]=1; for (p=head[u].next; p!=NULL; p=p->next) { v=p->nod; if (!mark[v]) { if (p->db==0) { E *s=new E; s=&head[v]; while (s->next->nod!=u) s=s->next; E *t=new E; t=s->next; s->next=s->next->next; delete(t); } tarjan(v); low[u]=min(low[u],low[v]); } else if (vis[v]) low[u]=min(low[u],dfn[v]); } if (low[u]==dfn[u]) { while (!s.empty()&&s.top()!=u) { vis[s.top()]=0; s.pop(); } vis[s.top()]=0; s.pop(); } } bool find(int a ,int b) { E *p=new E; p=head[a].next; while (p!=NULL&&p->nod!=b) p=p->next; if (p!=NULL&&p->nod==b) return 0; else return 1; } int main() { int u,v; E *s,*t; int i,m,a,b,cas; scanf("%d",&cas); while (cas--) { total=0; memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(mark,0,sizeof(mark)); memset(vis,0,sizeof(vis)); memset(head,0,sizeof(head)); scanf("%d%d",&n,&m); for (i=1; i<=m; ++i) { scanf("%d%d",&a,&b); if (find(a,b)) { s=new E; s->nod=b; s->db=0; s->num=i; s->next=head[a].next; head[a].next=s; s=new E; s->nod=a; s->db=0; s->num=i; s->next=head[b].next; head[b].next=s; } else { s=&head[a]; while (s->next!=NULL&&s->nod!=b) s=s->next; s->db=1; s=&head[b]; while (s->next!=NULL&&s->nod!=a) s=s->next; s->db=1; } } tarjan(1); //tarjan演算法輔助求割邊 total=0; int find=0; for (i=1;i<=n;++i) { s=new E; s=head[i].next; while (s!=NULL) { if (dfn[i]<low[s->nod]) { total++; res[total]=s->num; } s=s->next; } } sort(res+1,res+total+1); printf("%d/n",total); for (i=1;i<=total;++i) { printf("%d",res[i]); if (i!=total) printf(" "); else printf("/n"); } for (i=1;i<=n;++i) //刪除鄰接表,否則超記憶體 { s=head[i].next; t=s; while (t!=NULL) { s=t; t=t->next; delete(s); } } if (cas!=0) printf("/n"); } return 0; }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.