LOJ-10102(桥的判断)
题目链接:传送门
思路:找桥就行了,条件是num[v]<low[u],pre!=v;
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> #include<stack> using namespace std; const int maxn = 30030; int num[maxn],vis[maxn],low[maxn],tim,cnt; vector <int> vc[maxn]; int MIN(int x,int y) { return x<y?x:y; } int MAX(int x,int y) { return x>y?x:y; } void Init() { memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); memset(low,0,sizeof(low)); for(int i=0;i<maxn;i++) vc[i].clear(); tim=0;cnt=0; } void Tarjan(int u,int pre) { num[u]=low[u]=++tim; vis[u]=1; int v,i; for(i=0;i<vc[u].size();i++){ v=vc[u][i]; if(pre==v) continue; if(!vis[v]){ Tarjan(v,u); low[u]=MIN(low[u],low[v]); if(num[u]<low[v]) cnt++; } else low[u]=MIN(low[u],num[v]); } } int main(void) { int n,m,i,j,x,y; while(~scanf("%d%d",&n,&m)&&(n+m)){ Init(); for(i=1;i<=m;i++){ scanf("%d%d",&x,&y); vc[x].push_back(y); vc[y].push_back(x); } Tarjan(1,-1); printf("%d\n",cnt); } return 0; }