代码改变世界

POJ 1144 Network 无向图求割点

2012-08-25 10:59  javaspring  阅读(392)  评论(0编辑  收藏  举报

来源:http://poj.org/problem?id=1144

题意:就是给你一些点,某些点之间有边。求有多少个点是割点。

思路:模板题目了,直接用无向图求个点模板就可以ac。需要注意的是输入,输入有点麻烦。以换行结尾可以写成while(getchar() != '\n'),其他没什么难度了。无向图求割点的问题可以参考http://blog.csdn.net/wmn_wmn/article/details/7893157

代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;

#define CLR(arr,val) memset(arr,val,sizeof(arr))
const int N = 110;
int low[N],dfn[N],vis[N],numblock[N];
vector<int> vv[N];
int numcnt,timeorder,numpoint,numson;
void init(){
	CLR(low,0);
	CLR(dfn,0);
	CLR(vis,0);
	CLR(vv,0);
	CLR(numblock,0);
	numson = 0;
	numcnt = 0;
	timeorder = 0;
}
int min(int a,int b){
	return a < b ? a : b;
}
void dfs(int x){
	timeorder++;
	low[x] = dfn[x] = timeorder;
	vis[x] = 1;
	for(int i = 0; i < vv[x].size(); ++i){
	   int y = vv[x][i];
	   if(!vis[y]){
	     dfs(y);
		 low[x] = min(low[x],low[y]);
		 if(low[y] >= dfn[x] && x != 1){
		   numblock[x]++;
		 }
		 else if(x == 1)
			 numson++;
	   }
	   else
		   low[x] = min(low[x],dfn[y]);
	}
}
int main(){
	//freopen("1.txt","r",stdin);
	while(scanf("%d",&numpoint) && numpoint){
		init();
	   int x,y;
	   char ch;
	   while(scanf("%d",&x) && x){
		   while(getchar() != '\n'){
		     scanf("%d",&y);
			 vv[x].push_back(y);
			 vv[y].push_back(x);
		   }
	   }
       dfs(1);
	   for(int i = 1; i <= numpoint; ++i)
		   if(numblock[i])
			   numcnt++;
	   if(numson > 1)
		   numcnt++;
	   printf("%d\n",numcnt);
	}
	return 0;
}