求无向图的割切点,删除割切点及其相关的边将导致图的连通分量增加,采用了邻接矩阵表示图。
//============================================================================
// Name : CutPointProblem.cpp
// Author : Qichi
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int N; //number of vertex
bool *graph; //matrix of graph
int *low, *mark; //mark
void dfs(int u, int v, int num);
void explore_cont_block() {
int i;
for ( i=0; i<N; i++ ) if ( mark[i] == 0 ) break;
if ( i < N ) {
dfs(-1, i,1);
}
}
void dfs(int u, int v, int num) {
mark[v] = low[v] = num;
int count = (u==-1)?0:1;
bool potential = false;
for ( int j=0; j<N; j++ ) {
if ( graph[v*N+j] && j!=u ) {
// an edge <v,j> is found
if ( mark[j] == 0 ) {
dfs(v,j,num+1);
if ( low[j] < low[v] )
low[v] = low[j];
count++;
} else {
if ( mark[j] < low[v] )
low[v] = mark[j];
}
// child j with low[j] >= mark[v]
if ( low[j] >= mark[v] )
potential = true;
}
}
if ( potential && count>=2 )
cout << "Cut point : " << v << endl;
}
int main() {
cout << "Enter number of vertex : ";
cin >> N;
graph = (bool*)malloc(N*N*sizeof(bool));
low = new int[N];
mark = new int[N];
memset(graph,0,N*N*sizeof(bool));
memset(mark,0,N*sizeof(int));
memset(low,0,N*sizeof(int));
int i,j;
cout << "Enter an edge : ";
while ( cin >> i >> j ) {
if ( i < N && j < N && i >=0 && j >= 0 ) {
graph[i*N+j] = graph[j*N+i] = true;
} else {
cout << "Invalid!" << endl;
}
cout << "Enter an edge : ";
}
explore_cont_block();
free(graph);
free(low);
free(mark);
return 0;
}