POJ_3694_Network

#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
#pragma warning(disable:4996)
#include<cmath>
#include<ctime>
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::vector;
using std::string;
using std::map;
using std::sort;
//对于每个连同块取一个点x调用cur_bridge(x,-1,0,邻接矩阵,桥,割点,祖先,深度,访问)
void cut_bridge(int curvex, int parvex, int curdepth, const vector<vector<bool>>&matrix, vector<vector<bool>>&bridge, vector<bool>&cut, vector<int>&ancestor, vector<int>&depth, vector<int>&vis)
{
	vis[curvex] = 1;
	depth[curvex] = ancestor[curvex] = curdepth;
	int children = 0;
	for (size_t i = 1; i < matrix.size(); i++)
	{
		if (matrix[curvex][i])
		{
			if (i != parvex && 1 == vis[i])
			{
				if (depth[i] < ancestor[curvex])
				{
					ancestor[curvex] = depth[i];
				}
			}
			if (!vis[i])
			{
				cut_bridge(i, curvex, curdepth + 1, matrix, bridge, cut, ancestor, depth, vis);
				children++;
				if (ancestor[i] < ancestor[curvex])
				{
					ancestor[curvex] = ancestor[i];
				}
				if ((parvex == -1 && children>1) || (parvex != -1 && ancestor[i] >= depth[curvex]))
				{
					cut[curvex] = true;
				}
				if (ancestor[i] > depth[curvex])
				{
					bridge[curvex][i] = bridge[i][curvex] = true;
				}
			}
		}
	}
	vis[curvex] = 2;
}
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n;
	while (cin >> n)
	{
		if (!n)
		{
			break;
		}
		vector<vector<bool>>matrix(n + 1, (vector<bool>)(n + 1));
		vector<vector<bool>>bridge(n + 1, (vector<bool>)(n + 1));
		vector<bool>cut(n + 1);
		vector<int>ancestor(n + 1), depth(n + 1), vis(n + 1);
		while (cin >> n)
		{
			if (!n)
			{
				break;
			}
			string str; getline(cin, str);
			stringstream stream; stream << str;
			int m;
			while (stream >> m)
			{
				matrix[n][m] = matrix[m][n] = true;
			}
		}
		cut_bridge(1, -1, 0, matrix, bridge, cut, ancestor, depth, vis);
		int count = 0;
		for (size_t i = 0; i < cut.size(); i++)
		{
			if (cut[i])
			{
				count++;
			}
		}
		cout << count << endl;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值