POJ 1144 Network 无向图求割点

来源: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;
}


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值