无向图求割点

参考资料
http://www.cnblogs.com/en-heng/p/4002658.html

测试数据
13 18
A B
A C
A F
A L
B D
B E
B C
B G
B H
B M
D E
G I
G H
G K
H K
J L
J M
L M

#include<iostream>
#include<stdio.h>
#include<malloc.h>
#define maxn 100
using namespace std;
struct node2
{
char A;
node2* next;
};
struct node1
{
char A;
node2 *firarc;
};
struct node
{
int vexnum,edgenum;
node1 adj[maxn];
};
node  Node;
int Find(char a)
{
for(int i=0;i<Node.vexnum;i++)
    if(a==Node.adj[i].A)
{
    return i;
}
return -1;
}
void init()
{
char a,b;
node2 *p;
int i,j=0,k;
printf("请输入顶点数和边数\n");
scanf("%d%d",&Node.vexnum,&Node.edgenum);
for( i=0;i<Node.vexnum;i++)
{
Node.adj[i].firarc=NULL;
}
for( i=0;i<Node.edgenum;i++)
{
printf("请输入两个顶点\n");
cin>>a>>b;
p=(node2*)malloc(sizeof(node2));
p->A=b;
if((k=Find(a))==-1)
{
Node.adj[j++].A=a;
p->next=Node.adj[j-1].firarc;
Node.adj[j-1].firarc=p;
}
else
{
p->next=Node.adj[k].firarc;
Node.adj[k].firarc=p;
}
p=(node2*)malloc(sizeof(node2));
p->A=a;
if((k=Find(b))==-1)
{
 Node.adj[j++].A=b;
 p->next=Node.adj[j-1].firarc;
 Node.adj[j-1].firarc=p;
}
else
{
p->next=Node.adj[k].firarc;
Node.adj[k].firarc=p;
}
}
}
int Min(int a,int b)
{
return (a<b)?a:b;
}
int parent[maxn];
int visit[maxn];
int dfn[maxn];
int low[maxn]={0};
int cnt=0;
bool IDE[maxn];
void dfs(int i)//无向图的割点
{
int v;
int children =0;
visit[i]=1;
low[i]=dfn[i]=++cnt;
node2 *p=Node.adj[i].firarc;
for(;p!=NULL;p=p->next)
{
v=Find(p->A);
if(!visit[v])
{
children++;
parent[v]=i;
dfs(v);
low[i]=Min(low[i],low[v]);
if(parent[i]==-1&&children>1)
{

IDE[i]=true;
}
if(parent[i]!=-1&&low[v]>=dfn[i])
{
IDE[i]=true;
}
}
else
{
if(v!=parent[i])
{
low[i]=Min(low[i],dfn[v]);    //更新
}
}

}
}
void display()
{
    node2 *p;
    for(int i=0;i<Node.vexnum;i++)
    {
    p=Node.adj[i].firarc;
    printf("%c ",Node.adj[i].A);
    while(p)
    {
    printf("%c ",p->A);
    p=p->next;
    }
    putchar(10);
    }
}
int main()
{

    init();
    parent[0]=-1;
    //display();
    dfs(0);
    for(int i=0;i<Node.vexnum;i++)
        pr````
ntf("%d ",low[i]);
    putchar(10);
    for(int i=0;i<Node.vexnum;i++)
      if(IDE[i]) printf("割点为%c\n",Node.adj[i].A);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/kala0/article/details/53344149
今日推荐