#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;
}
POJ_3694_Network
最新推荐文章于 2020-09-18 11:42:25 发布