题目链接:http://lightoj.com/volume_showproblem.php?problem=1063
#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],low[maxn],dfs_clock; bool iscut[maxn]; int ans; int n,m; void tarjan(int u,int fa){ pre[u] = low[u] = dfs_clock++; int child = 0; for(int i=0;i<G[u].size();i++){ int v = G[u][i]; if(v == fa) continue; if(!pre[v]){ child++; tarjan(v,u); low[u] = min(low[u],low[v]); if(low[v] >= pre[u]) iscut[u] = true; } else low[u] = min(low[u],pre[v]); } if(fa == -1 && child == 1) iscut[u] = false; } int main() { //freopen("E:\acm\input.txt","r",stdin); int T; cin>>T; for(int t=1;t<=T;t++){ cin>>n>>m; memset(pre,0,sizeof(pre)); memset(iscut,0,sizeof(iscut)); dfs_clock = 1; ans = 0; for(int i=1;i<=n;i++) G[i].clear(); for(int i=1;i<=m;i++){ int a,b; scanf("%d %d",&a,&b); G[a].push_back(b); G[b].push_back(a); } tarjan(1,-1); for(int i=1;i<=n;i++) if(iscut[i]) ans++; printf("Case %d: %d ",t,ans); } }