标签:
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
题意:n个岛屿m条桥 其中有重边,现在要烧掉没用的桥(保留的是双连通分量的割边)让你输出割边的总数以及 编号
题解:求出桥 输出(注意对重边的处理)
#include<stdio.h> #include<string.h> #include<algorithm> #include<stack> #define MAX 200100 using 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) //处理重边 { if(edge[i].end==v) break; } if(i!=-1)//i!=-1证明未处理完所有的边(即发现了重边) edge[i].chon=1;//标记重边 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)//有桥并且不是重边 { 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)); dfsclock=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")//注意输出格式 } return 0; }
zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4868385.html