- #include<iostream>
- #include<stdlib.h>
- using namespace std;
- #define OK 1
- #define OVERFLOW -2
- typedef int Status;
- typedef char VertexType;
- typedef enum{DG,DN,UDG,UDM} GraphKind;
- typedef struct ArcNode{
- int adjvex;
- struct ArcNode *nextarc;
- }ArcNode;
- typedef struct VNode{
- VertexType data;
- ArcNode *firstarc;
- }VNode,*AdjList;
- typedef struct {
- AdjList vertices;
- int vexnum,arcnum;
- GraphKind kind;
- }ALGraph;
- int const N=100;
- int visited[N],low[N];
- int LocateVex(ALGraph G,VertexType v)
- {
- for(int i=0;i<G.vexnum;i++)
- if(G.vertices[i].data==v) return i;
- return G.vexnum;}
- Status CreateUDG(ALGraph &G)
- {
- G.kind=UDG; //无向图
- cin>>G.vexnum>>G.arcnum;
- if(!(G.vertices=new VNode[G.vexnum])) exit(OVERFLOW);
- for(int i=0;i<G.vexnum;i++) {
- cin>>G.vertices[i].data;
- G.vertices[i].firstarc=NULL;}
- VertexType v1,v2;
- ArcNode *p,*q;
- int i,j,k=0;
- for(;k<G.arcnum;k++) {
- do
- {
- cin>>v1>>v2;
- i=LocateVex(G,v1);
- j=LocateVex(G,v2);
- }while(i==G.vexnum||j==G.vexnum);
- p=new ArcNode;
- q=new ArcNode;
- if(!p||!q) exit(OVERFLOW);
- p->adjvex=j;
- p->nextarc=G.vertices[i].firstarc;
- G.vertices[i].firstarc=p;
- q->adjvex=i;
- q->nextarc=G.vertices[j].firstarc;
- G.vertices[j].firstarc=q;
- }
- return OK;
- }
- void FindArticul(ALGraph G,int u,int parent)
- {
- static int idx=0;
- int child_cnt=0;
- bool flag=false;
- visited[u]=++idx;
- low[u]=visited[u];
- for(ArcNode *p=G.vertices[u].firstarc;p;p=p->nextarc)
- {
- if(visited[p->adjvex]==-1) //未访问
- {
- FindArticul(G,p->adjvex,u); //DFS遍历
- if(low[p->adjvex]<low[u]) low[u]=low[p->adjvex]; //low[u]取DFS子树最小
- else if(low[p->adjvex]>=visited[u])
- flag=true; //u是关节点
- ++child_cnt; //u节点子树个数
- }
- else if(p->adjvex!=parent&&visited[p->adjvex]<low[u])
- low[u]=visited[p->adjvex];
- }
- if (parent!=-1&&flag||parent==-1&&child_cnt>=2)
- cout<<G.vertices[u].data<<" ";
- }
- int main(){
- ALGraph G;
- CreateUDG(G);
- for(int i=0;i<N;i++)
- visited[i]=-1;
- FindArticul(G,0,-1);
- system("pause");
- return 0;}
- 输入
- 13 17
- a b c d e f g h i j k l m
- a b
- a c
- a f
- a l
- b c
- b d
- b g
- b h
- b m
- d e
- g h
- g i
- g k
- h k
- j l
- j m
- l m
- 输出
- g d b a 请按任意键继续. . .
关节点
最新推荐文章于 2023-02-04 14:21:36 发布