标签:
题意:将无向图中的桥找出来,并将他们的输入序号输出
思路:模版找桥,但是注意处理重边和后面的输出,别的没什么了,模版题
#include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=20010; struct edge{ int to,id,num; edge(int a,int b,int c){to=a;id=b;num=c;} }; vector<edge>G[maxn]; int L[maxn],E[maxn],ans[maxn],vis[maxn]; int n,m,k,kk; void dfs(int x,int fa){ vis[x]=1;L[x]=k;E[x]=k++; for(unsigned int i=0;i<G[x].size();i++){ edge e=G[x][i]; if(!vis[e.to]){ dfs(e.to,x); L[x]=min(L[x],L[e.to]); if(L[e.to]>E[x]&&e.num==0) ans[kk++]=e.id; }else if(e.to!=fa) L[x]=min(L[x],E[e.to]); } } int tarjan(){ k=0;kk=0;dfs(1,1); return kk; } int main(){ int T,a,b,flag; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(int i=0;i<maxn;i++){ G[i].clear();vis[i]=0; } for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); flag=0; for(unsigned int j=0;j<G[a].size();j++){ edge e=G[a][j]; if(e.to==b){ G[a][j].num=1;flag=1; for(unsigned int l=0;l<G[b].size();l++){ edge ee=G[b][l]; if(ee.to==a) G[b][l].num=1; } } } if(flag==0){ G[a].push_back(edge(b,i,0)); G[b].push_back(edge(a,i,0)); } } int ans1=tarjan(); printf("%d\n",ans1); sort(ans,ans+kk); for(int i=0;i<kk-1;i++) printf("%d ",ans[i]); if(kk>0) printf("%d\n",ans[kk-1]); if(T) printf("\n"); } return 0; }
标签:
原文地址:http://blog.csdn.net/dan__ge/article/details/51315497