求割点个数

#include
#include
#include 
#include
#include
using namespace std;
vector mp[10010];
int root,low[130],dfn[130],vis[130],num[130],cnt;
void Tarjan(int u){
	low[u] = dfn[u] = ++cnt;
	vis[u] = 1;
	for( int i = 0; i < mp[u].size(); i++){
		int v = mp[u][i];
		if(!vis[v]){
			Tarjan(v);
			low[u] = min(low[u],low[v]);
			if(low[v] >= dfn[u] && u != 1){
				num[u]++;
			} 
			else if(u == 1) root++;
		}
		else{
			low[u] = min(low[u],dfn[v]);
		}
	}
}
int main(){
	int n,u,v;
	while(scanf("%d",&n) && n){
		root = cnt = 0;
		memset(low,0,sizeof(low));
		memset(dfn,0,sizeof(dfn));
		memset(vis,0,sizeof(vis));
		memset(num,0,sizeof(num));
		while(scanf("%d",&u) && u){
			while(getchar() != '\n'){
				scanf("%d",&v);
				mp[u].push_back(v);
				mp[v].push_back(u);
			}
		}
		Tarjan(1);
		for(int i = 1; i <= n; i++)
		if(mp[i].size()) mp[i].clear();
		int ans = 0;
		if(root > 1) ans++;
		for( int i = 2; i <= n; i++)
		if(num[i]) ans++;
		printf("%d\n",ans);
	}
	return 0;
} 

你可能感兴趣的:(ACM,poj,图论)