lightoj 1026 无向图 求桥
题目链接:http://lightoj.com/volume_showproblem.php?problem=1026
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 10050; const int INF = 0x3f3f3f; vector<int> G[maxn]; int pre[maxn],dfs_clock,low[maxn]; struct ANS{ int l,r; bool operator < (const ANS& a) const{ return l < a.l || (l == a.l && r < a.r); //排序被坑了,WA了好几次啊,没想后面的情况。 } }ans[maxn]; int cnt; int n; void tarjan(int u,int fa){ low[u] = pre[u] = dfs_clock++; for(int i=0;i<G[u].size();i++){ int v = G[u][i]; if(v == fa) continue; if(!pre[v]){ tarjan(v,u); low[u] = min(low[u],low[v]); if(low[v] > pre[u]){ ans[cnt].l = min(u,v); ans[cnt].r = max(u,v); cnt++; } } else low[u] = min(low[u],pre[v]); } } void init(){ cnt = 0; dfs_clock = 1; memset(pre,0,sizeof(pre)); for(int i=0;i<n;i++) G[i].clear(); } int main() { freopen("E:\\acm\\input.txt","r",stdin); int T; cin>>T; for(int t=1;t<=T;t++){ scanf("%d",&n); init(); for(int i=0;i<n;i++){ int u,m; scanf("%d (%d)",&u,&m); for(int j=1;j<=m;j++){ int v; scanf("%d",&v); G[u].push_back(v); } } for(int i=0;i<n;i++){ if(!pre[i]){ tarjan(i,-1); } } printf("Case %d:\n",t); printf("%d critical links\n",cnt); sort(ans,ans+cnt); for(int i=0;i<cnt;i++){ printf("%d - %d\n",ans[i].l,ans[i].r); } } }