// ACM学习-割点和桥.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int v = 13;
int edge[v][v] = {
{ 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 },
{ 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 }
};
bool vis[v] = { false };
char t[] = { ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘,
‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘ };
//queue<int> q;
//判断i点到j点是否有两条路径
bool is_can_shuang_lian_tong(int i, int j){
for (int i = 0; i<v; i++){
vis[i] = false;
}
queue<int> q;
q.push(i);
vis[i] = true;
int count = 0;
while (q.size() > 0){
int k = q.front();
q.pop();
if (edge[k][j]){
count++;
vis[j] = true;
if (count == 2)
return true;
}
for (int i = 0; i < v; i++){
if (edge[k][i] && !vis[i])
{
vis[i] = true;
q.push(i);
}
}
}
return false;
}
void print(int i)
{
cout << t[i] << ends;
}
void bian_zi_tu(){//广度优先遍历
queue<int> q;
vector<int> vnum;
int yy[v][v];//这个变量可以去除,没用
memcpy(yy, edge, v*v * sizeof(int));
for (int i = 0; i<v; i++){
vis[i] = false;
}
for (int i = 0; i<v; i++){
if (!vis[i])
{
vis[i] = true;
vnum.clear();
q.push(i);
vnum.push_back(i);
while (q.size() > 0){
int k = q.front();
q.pop();
for (int i = 0; i < v; i++){
if (yy[k][i]&&!vis[i])
{
//yy[k][i] = 0;
//yy[i][k] = 0;
vis[i] = true;
q.push(i);
vnum.push_back(i);
}
}
}//while
for_each(vnum.begin(),vnum.end(),print);
cout << endl;
}
}
}
//得到一个图中是否有双连通子图
void get_shuang_lian_tong(){
bool isContinue = true;
while (isContinue){
int count = 0;
int index = 0;
isContinue = false;//由于删除了节点关系,可能要再删一次
for (int i = 0; i < v; i++){//如果某个节点只有一个节点跟它相连,则删除它与连着的那个节点的关系
count = 0;
for (int j = 0; j < v; j++){
if (edge[i][j]){
++count;
index = j;
}
}
if (count ==1)
{
isContinue = true;
edge[i][index] = 0;
edge[index][i] = 0;
}
}
}
for (int i = 0; i < v; i++){
for (int j = 0; j < v; j++){
if (edge[i][j]){
if (!is_can_shuang_lian_tong( i,j))//判断i与j是否有两条路径
{//如果没有删除他们的关系
edge[i][j] = 0;
edge[j][i] = 0;
}
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
get_shuang_lian_tong();
/*for (int i = 0; i < v; i++){
for (int j = 0; j < v; j++){
if (edge[i][j])
{
cout << t[i] << "和" << t[j] << "连着" << endl;
}
}
}*/
bian_zi_tu();
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。