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; }