题目地址:http://poj.org/problem?id=1144
直接上模板就好了
#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<sstream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100+5;
typedef pair<int,int> Edge;
int index=0,rootson;
vector<bool> vis(maxn);
vector<int> dfn(maxn),low(maxn),cutvex;
vector<vector<int> > G(maxn);
void Tarjan(int u,int father)
{dfn[u]=low[u]=++index;vis[u]=true;bool isCut=false;for(int i=0;i<G[u].size();i++){int v=G[u][i];if(!vis[v]){Tarjan(v,u);low[u]=min(low[u],low[v]);if(low[v]>=dfn[u]) isCut=true;}else if(father!=v) //只能由非父子边更新 low[u]=min(low[u],dfn[v]);}if(father==1&&rootson!=-1) rootson++;if(rootson>1) cutvex.push_back(1),rootson=-1; if(u!=1&&isCut) cutvex.push_back(u);
}
int main()
{int n,u,v; string s;while(cin>>n&&n){for(int i=1;i<=n;i++) G[i].clear(),vis[i]=false;cutvex.clear(); while(cin>>u&&u){ getline(cin,s);stringstream ss(s); //这个超级慢 getchar16MS ,这个110MS while(ss>>v){G[u].push_back(v);G[v].push_back(u);}}index=0; rootson=0;Tarjan(1,0); //联通的cout<<cutvex.size()<<endl;}return 0;
}