题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2588
题意:
Ferry王国是一个漂亮的岛国,一共有N个岛国、M座桥,通过这些桥可以从每个小岛都能// tarjan 求割边的个数
两个岛还可能存在多个桥的情况
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 10100; const int maxm = 221010; const int inf = 1e8; #define MIN INT_MIN #define MAX 1e6 #define LL long long #define init(a) memset(a,0,sizeof(a)) #define FOR(i,a,b) for(int i = a;i<b;i++) #define max(a,b) (a>b)?(a):(b) #define min(a,b) (a>b)?(b):(a) using namespace std; struct node { int next,v,flag,id; }edge[maxm]; int DFN[maxn],low[maxn],bnum,bianhao; int ans[maxn],ge; int head[maxn]; int st[maxn],en[maxn]; int cmp(const void *a,const void *b) { return *(int *)a - *(int *)b; } void initt() { memset(head,0,sizeof(head)); bnum = 1; bianhao = 1; init(DFN);init(low);init(ans); ge = 0; init(st);init(en); } void add(int u,int v,int id) { int i; for(i = head[u];i!=0;i=edge[i].next) { if(edge[i].v == v) break; } if(i) { edge[i].flag = 1; return ; } edge[bnum].v = v; edge[bnum].id = id; edge[bnum].flag = 0; edge[bnum].next = head[u]; head[u] = bnum++; } void tarjan(int s,int father) { DFN[s] = low[s] = bianhao++; for(int i = head[s];i!=0;i = edge[i].next) { if(DFN[edge[i].v]==0) { tarjan(edge[i].v,s); low[s] = min(low[s],low[edge[i].v]); if(DFN[s] < low[edge[i].v] && edge[i].flag==0) { ans[ge++] = edge[i].id; st[ge-1] = s; en[ge-1] = edge[i].v; } } else if(edge[i].v != father) { low[s] = min(low[s],DFN[edge[i].v]); } } } int main() { int t,m,n,a,b; scanf("%d",&t); while(t-- && scanf("%d%d",&n,&m)) { initt(); FOR(i,1,m+1) { scanf("%d%d",&a,&b); add(a,b,i); add(b,a,i); } tarjan(1,-1); printf("%d\n",ge); if(ge!=0) { qsort(ans,ge,sizeof(int),cmp); FOR(i,0,ge-1) { printf("%d ",ans[i]); } printf("%d\n",ans[ge-1]); /* puts("割边是"); FOR(i,0,ge) { printf("%d->%d\n",st[i],en[i]); }*/ } if(t) cout<<endl; } return 0; }