// ACM学习-自己写割点.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
using namespace std;
const int v = 4;
int edge[v][v] = {
{ 0, 1, 1, 0 },
{ 0, 0, 0, 1},
{ 1, 0, 0, 0 },
{ 1, 1, 0, 0}
};
/*const int v = 10;
int edge[v][v] = {
//A,B,C,D,E,F,G,H,I,J
{0,1,1,0,0,1,1,0,0,0},//A
{1,0,0,1,0,0,0,0,0,0},//B
{1,0,0,0,1,0,0,0,0,0},//C
{0,1,0,0,0,0,0,1,0,0},//D
{0,0,1,0,0,0,1,0,0,0},//E
{1,0,0,0,0,0,0,0,0,0},//F
{1,0,0,0,1,0,0,0,0,0},//G
{0,0,0,1,0,0,0,0,1,1},//H
{0,0,0,0,0,0,0,1,0,1},//I
{0,0,0,0,0,0,0,1,1,0} //J
};*/
bool bridge[v][v], cut[v];
int low[v] = { 0 }, dfn[v] = { 0 }, vis[v] = { 0 };
void cut_bridge(int cur, int father, int dep, int n){
vis[cur] = 1; dfn[cur] = low[cur] = dep;
int children = 0;
for (int i = 0; i < n; i++){
if (edge[cur][i]){
if (i != father && 1 == vis[i]){
//看看周围有没有更快到达祖先的点,有的话减少low[cur]
//只针对0 == vis[i]段代码还没执行完的点,而且它不是父亲节点
if (dfn[i] < low[cur])
low[cur] = dfn[i];
}
if (0 == vis[i]){//没有访问过,叶子节点肯定不访问该段代码块
cut_bridge(i, cur, dep + 1, n);
children++;
//儿子更快到达祖先,改善一下爸爸的low[i];
if (low[i] < low[cur]) low[cur] = low[i];
//如果是根节点(没有父亲)有两个孩子,则它是关键节点
//如果子节点,没有周围的节点改善它到祖先的深度,即只有一个父节点
if ((father == -1 && children>1 || (father != -1 && low[i] >= dfn[cur]))){
cut[cur] = true; //如果是相等的话,说明他(i)有孩子到达
//它的父亲cur
cout << "关键节点:" << cur << endl;
}
if (low[i] > dfn[cur])
{
bridge[cur][i] = true;
cout << "桥是:" << cur << endl;
}
}
}
}
vis[cur] = 2;
}
int depth[v];
bool visted[v] = {false};
char t[v] = { '1', '2', '3','4' };
//char t[v] = {'A','B','C','D','E','F','G','H','I','J'};
int pre[v] = {-1};
bool isge[v] = { false };
void get_gedian(int point){
depth[point]++;
//cout << "访问:" << t[point] << " 深度:" << depth[point] << endl;
visted[point] = true;
int count = 0;
for (int i = 0; i < v; i++){
if (edge[point][i])
{
count++;
if (!visted[i])
{
depth[i] = depth[point];
pre[i] = point;
get_gedian(i);
if (depth[i] >=depth[point] && !isge[point]){
isge[point] = true;
if (pre[point]!=-1)
cout << "割边:" << t[point] << "-" << t[pre[point]] << endl;
cout << t[point] << "是割点" << endl;
}
else if (depth[i]
depth[point] = depth[i];
}
}
else{
if (i!=pre[point])
//cout << t[i] << "改善" << t[point] << endl;
depth[point] = depth[i] < depth[point] ? depth[i] : depth[point];
}
}
}
if (count == 1 && !isge[point]){
isge[point] = true;
cout << t[point] << "是割点" << endl;
if (pre[point] != -1)
cout << "割边:" << t[point] << "-" << t[pre[point]] << endl;
}
else if (count == 0 && !isge[point])
{
isge[point] = true;
cout << t[point] << "是割点" << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cut_bridge(0, -1, 1, v);
//get_gedian(1);
return 0;
}