#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <fstream>
#include <cstdlib>
#include <queue>
#include <string.h>
using namespace std;
#define MAX_VERTEX_NUM 20
#define N 100
bool visit[N]; //访问标记数组是全局变量
int dfn[N]; //顶点的先深编号
int count = 1;
int FLAG = 99999;
int in_order[N];
//无向图
struct MTGragh
{
int vertex [N]; //顶点表
int edge[N][N];//邻接矩阵—边表, 可视为边之间的关系
int n, e; //图的顶点数与边数
};
//正向链接表
typedef struct node //边表结点
{
int adjvex; //邻接点域(下标)
int cost; //边上的权值
struct node *next; //下一边链接指针
} EdgeNode;
typedef struct //顶点表结点
{
int vertex; //顶点数据域
EdgeNode * firstedge;//边链表头指针
} VertexNode;
typedef struct //图的邻接表
{
VertexNode vexlist [N];
int n,e; //顶点个数与边数
} AdjGraph;
//十字链表
typedef struct ArcBox
{
int tailvex, headvex; //该弧的尾和头顶点的位置
int cost; //该弧的信息
struct ArcBox * hlink, * tlink; //分别为弧头相同和弧尾相同的弧的链域
} ArcBox;
typedef struct VexNode
{
int data; //顶点值
ArcBox * firstin, * firstout; //分别指向该顶点第一条入弧和出弧
} VexNode;
typedef struct
{
VexNode xlist[MAX_VERTEX_NUM]; //表头向量
int n, e; //有向图的当前顶点数和弧数
} OLGraph;
//无向图的邻接多重表
typedef enum {unvisited, visited} VisitIf;
typedef struct EBox
{
VisitIf mark; //访问标记
int ivex, jvex; //该边依附的两个顶点的位置
struct EBox * ilink, * jlink; //分别指向依附这两个顶点的下一条边
int cost; //该边信息
} EBox;
typedef struct VexBox
{
int data;
EBox * firstedge; //分别指向该顶点第一条入弧和出弧
} VexBox;
struct AMLGraph
{
VexBox adjmulist[MAX_VERTEX_NUM];
int n,e; //无向图的当前顶点数和边数
};
void DFS(OLGraph *G, int v);
void Rev_DFS(OLGraph *G, int v);
//建立图的邻接矩阵
int CreateMGragh(MTGragh *G,int c[N],int flag,int sum)
{
int i, j;
G->n=c[1];
for (i=0; i<G->n; i++) //2.读入顶点信息,建立顶点表
G->vertex[i]=i;
for (i=0; i<G->n; i++)
for (j=0; j<G->n; j++)
G->edge[i][j]=0; //3.邻接矩阵初始化
int a,b;
int temp=0;
i=2;
if(flag==0)
{
while(i<sum)
{
a=c[i];
i++;
b=c[i];
i++;
G->edge[a][b] = c[i];
G->edge[b][a] = c[i];
temp++;
i++;
}
}
else
{
while(i<sum)
{
a=c[i];
i++;
b=c[i];
i++;
G->edge[a][b] = c[i];
//G->edge[b][a] = c[i];
temp++;
i++;
}
}
G->e=temp;
return G->e;
}
//输出无向图的邻接矩阵
void putMgraph(MTGragh G)
{
cout<<"无向图的邻接矩阵:"<<endl;
for(int i=0; i<G.n; i++)
{
cout<<i<<": ";
for(int j=0; j<G.n; j++)
{
cout<<G.edge[i][j]<<" ";
}
cout<<endl;
}
}
//建立图的链接表
AdjGraph CreateGraph(AdjGraph G,int c[N],int k)
{
G.n=c[1]; G.e=k;int j=2;
for(int i = 0; i < G.n; i++) //2.建立顶点表
{
G.vexlist[i].vertex=
#include <stdio.h>
#include <malloc.h>
#include <fstream>
#include <cstdlib>
#include <queue>
#include <string.h>
using namespace std;
#define MAX_VERTEX_NUM 20
#define N 100
bool visit[N]; //访问标记数组是全局变量
int dfn[N]; //顶点的先深编号
int count = 1;
int FLAG = 99999;
int in_order[N];
//无向图
struct MTGragh
{
int vertex [N]; //顶点表
int edge[N][N];//邻接矩阵—边表, 可视为边之间的关系
int n, e; //图的顶点数与边数
};
//正向链接表
typedef struct node //边表结点
{
int adjvex; //邻接点域(下标)
int cost; //边上的权值
struct node *next; //下一边链接指针
} EdgeNode;
typedef struct //顶点表结点
{
int vertex; //顶点数据域
EdgeNode * firstedge;//边链表头指针
} VertexNode;
typedef struct //图的邻接表
{
VertexNode vexlist [N];
int n,e; //顶点个数与边数
} AdjGraph;
//十字链表
typedef struct ArcBox
{
int tailvex, headvex; //该弧的尾和头顶点的位置
int cost; //该弧的信息
struct ArcBox * hlink, * tlink; //分别为弧头相同和弧尾相同的弧的链域
} ArcBox;
typedef struct VexNode
{
int data; //顶点值
ArcBox * firstin, * firstout; //分别指向该顶点第一条入弧和出弧
} VexNode;
typedef struct
{
VexNode xlist[MAX_VERTEX_NUM]; //表头向量
int n, e; //有向图的当前顶点数和弧数
} OLGraph;
//无向图的邻接多重表
typedef enum {unvisited, visited} VisitIf;
typedef struct EBox
{
VisitIf mark; //访问标记
int ivex, jvex; //该边依附的两个顶点的位置
struct EBox * ilink, * jlink; //分别指向依附这两个顶点的下一条边
int cost; //该边信息
} EBox;
typedef struct VexBox
{
int data;
EBox * firstedge; //分别指向该顶点第一条入弧和出弧
} VexBox;
struct AMLGraph
{
VexBox adjmulist[MAX_VERTEX_NUM];
int n,e; //无向图的当前顶点数和边数
};
void DFS(OLGraph *G, int v);
void Rev_DFS(OLGraph *G, int v);
//建立图的邻接矩阵
int CreateMGragh(MTGragh *G,int c[N],int flag,int sum)
{
int i, j;
G->n=c[1];
for (i=0; i<G->n; i++) //2.读入顶点信息,建立顶点表
G->vertex[i]=i;
for (i=0; i<G->n; i++)
for (j=0; j<G->n; j++)
G->edge[i][j]=0; //3.邻接矩阵初始化
int a,b;
int temp=0;
i=2;
if(flag==0)
{
while(i<sum)
{
a=c[i];
i++;
b=c[i];
i++;
G->edge[a][b] = c[i];
G->edge[b][a] = c[i];
temp++;
i++;
}
}
else
{
while(i<sum)
{
a=c[i];
i++;
b=c[i];
i++;
G->edge[a][b] = c[i];
//G->edge[b][a] = c[i];
temp++;
i++;
}
}
G->e=temp;
return G->e;
}
//输出无向图的邻接矩阵
void putMgraph(MTGragh G)
{
cout<<"无向图的邻接矩阵:"<<endl;
for(int i=0; i<G.n; i++)
{
cout<<i<<": ";
for(int j=0; j<G.n; j++)
{
cout<<G.edge[i][j]<<" ";
}
cout<<endl;
}
}
//建立图的链接表
AdjGraph CreateGraph(AdjGraph G,int c[N],int k)
{
G.n=c[1]; G.e=k;int j=2;
for(int i = 0; i < G.n; i++) //2.建立顶点表
{
G.vexlist[i].vertex=