You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
1.8 KiB

# y
#define max 100
#define maxint 0
typedef struct arc{
int adj;
struct arc * next;
int info;
}arc;
typedef struct vnode{
int data;
arc *first;
}vnode,adj[max];
typedef struct{
adj vertices;
int dingdian,bian;
}alg;
int getvex(alg g,int n){
for(int i=0;i<g.dingdian;++i){
if(g.vertices[i].data==n)return i;
}
}
void creat(alg &g){
cout << "请输入总顶点数和总边数" << endl;
cin >> g.dingdian >> g.bian;
for(int i=0;i<g.dingdian;++i){
cin >> g.vertices[i].data;
g.vertices[i].first=NULL;
}
int v1=0,v2=0,i,j;
for(int k=0;k<g.bian;++k){
cin >> v1 >> v2;
i=getvex(g,v1);j=getvex(g,v2);
arc*p1=new arc;
arc*p2=new arc;
p1->adj=j;
p1->next=g.vertices[i].first;g.vertices[i].first=p1;
p2->adj=i;
p2->next=g.vertices[j].first;g.vertices[j].first=p2;
}
return;
}
void dfsal(alg g,int v){
int visited[max];
for(int i=0;i<g.dingdian;++i){
visited[i]=0;
}
cout << v;visited[v]=1;
int w=0;
arc *p=g.vertices[v].first;
while(p!=NULL){
w=p->adj;
if(visited[w]) dfsal(g,w);
p=p->next;
}
}
typedef struct{
int vexs[max];
int arcs [max][max];
int dingdian1,bian1;
}amg;
int getvex1(amg g,int n){
for(int i=0;i<g.dingdian1;++i){
if(g.vexs[i]==n){return n;}
}
}
void creat1(amg &g){
cout << "请输入总顶点数和总边数" << endl;
cin >> g.dingdian1 >> g.bian1;
for(int i=0;i<g.dingdian1;++i){
cin >> g.vexs[i];
}
for(int i=0;i<g.dingdian1;++i){
for(int j=0;j<g.dingdian1;++j){
g.arcs[i][j]=maxint;
}
}
int v1=0,v2=0,i,j;
for(int k=0;k<g.bian1;++k){
cin >> v1 >> v2;
i=getvex1(g,v1);j=getvex1(g,v2);
g.arcs[i][j]=1;
g.arcs[j][i]=g.arcs[i][j];
}
return;
}
void dfsam(amg g,int v){
int visited[max];
for(int i=0;i<g.dingdian1;++i){
visited[i]=0;
}
cout << v;visited[v]=1;
for(int w=0;w<g.dingdian1;++w){
if((g.arcs[v][w]!=maxint)&&(!visited[w])) dfsam(g,w);
}
}