7043
7039
#include <bits/stdc++.h>
using namespace std;
typedef struct arcnode{
int adjvex;
struct arcnode *nextarc;
}arcnode;
typedef struct vnode{
int data;
struct arcnode *firstarc;
}vnode,*adjlist;
typedef struct {
int vnum,arcnum;
adjlist vers;
}algraph;
int locate(algraph g, int index)
{
int flag=0;
for(int i=0; i<g.vnum;i++)
{if (g.vers[i].data==index)
{flag = 1;
return i;}
}
if (flag == 0)
return -1;
}
void creatDG(algraph &g)
{
cin >> g.vnum >> g.arcnum;
g.vers = new vnode[g.vnum];
for (int i = 0; i < g.vnum; i++) {
cin >> g.vers[i].data;
g.vers[i].firstarc = NULL;
}
for (int i = 0; i < g.arcnum; i++) {
int v1, v2;
cin >> v1 >> v2;
v1 = locate(g, v1);
v2 = locate(g, v2);
arcnode* p1 = new arcnode;
p1->adjvex = v2 + 1;
p1->nextarc = g.vers[v1].firstarc;
g.vers[v1].firstarc=p1;
}
}
void Output(algraph g)
{
for (int i=0; i<g.vnum;i++)
{
cout <<g.vers[i].data<< ":";
arcnode * p=g.vers[i].firstarc;
while (p)
{
cout<<p->adjvex<<" ";
p=p->nextarc;
}
cout<<endl;
}
}
int visited[1000];
void Depth(algraph g,int v)
{
cout <<g.vers[v].data<< " ";
visited[v]=1;
arcnode* p=g.vers[v].firstarc;
while (p)
{
int w=locate(g, p->adjvex);
if (!visited[w])
Depth(g, w);
p=p->nextarc;
}
}
void Deptht(algraph g)
{
memset(visited, 0, sizeof(visited));
for (int i = 0; i < g.vnum; i++)
{
if (visited[i] == 0)
Depth(g,i);
}
}
int main()
{ algraph g;
creatDG(g);
Output(g);
Deptht(g);
return 0;
}
7040
#include<bits/stdc++.h>
using namespace std;
typedef struct arcnode {
int adjvex;
struct arcnode* next;
}arcnode;
typedef struct vnode {
int data;
int indegree,outdegree;
arcnode *firstarc;
}vnode, * adjlist;
typedef struct {
int vexnum, arcnum;
adjlist vers;
}algraph;
int Locate(algraph g, int index)
{
int flag = 0;
for (int i = 0; i < g.vexnum; i++)
{ if(g.vers[i].data==index)
{flag = 1;
return i;}
}
if (flag == 0)
return -1;
}
int CreateDG(algraph &g)
{
cin >>g.vexnum>>g.arcnum;
g.vers = new vnode[g.vexnum];
for (int i = 0; i < g.vexnum; i++) {
cin >> g.vers[i].data;
g.vers[i].firstarc=NULL;
g.vers[i].indegree=0;
g.vers[i].outdegree=0;
}
for (int i = 0; i < g.arcnum; i++) {
int v1, v2;
cin>>v1>>v2;
v1 = Locate(g, v1);
v2 = Locate(g, v2);
g.vers[v2].indegree++;
g.vers[v1].outdegree++;
arcnode* p1 = new arcnode;
p1->adjvex = v2 + 1;
p1->next = g.vers[v1].firstarc;
g.vers[v1].firstarc = p1;
}
return 1;
}
void outputdegree(algraph g)
{
for(int i=0;i<g.vexnum;i++)
{
cout<<g.vers[i].data<<":";
cout<<g.vers[i].indegree<<" ";
cout<<g.vers[i].outdegree<<" ";
cout<<g.vers[i].outdegree+g.vers[i].indegree<<"\n";
}
}
int main() {
algraph g;
CreateDG(g);
outputdegree(g);
return 0;
}
7042
要求非递归但是我用的递归,待会看看怎么非递归
#include<bits/stdc++.h>
using namespace std;
typedef struct arcnode {
int adjvex;
struct arcnode* next;
}arcnode; //边节点
typedef struct vnode {
int data;
arcnode* firstarc;
}vnode, * adjlist;
typedef struct {
int vexnum, arcnum;
adjlist vers;
}algraph;
int Locate(algraph g, int index)
{
int flag = 0;
for (int i = 0; i < g.vexnum; i++)
{
if(g.vers[i].data==index)
{flag = 1;
return i;}
}
if (flag == 0)
return -1;
}
int CreateUDG(algraph& g)
{
cin >> g.vexnum >> g.arcnum;
g.vers= new vnode[g.vexnum];
for (int i = 0; i < g.vexnum; i++)
{
cin >> g.vers[i].data;
g.vers[i].firstarc = NULL;
}
for (int i = 0; i < g.arcnum; i++) {
int v1, v2;
cin >> v1 >> v2;
v1 = Locate(g, v1);
v2 = Locate(g, v2);
arcnode* p1 = new arcnode;
arcnode* p2 = new arcnode;
p1->adjvex = v2 + 1;
p2->adjvex = v1 + 1;
p1->next = g.vers[v1].firstarc;
p2->next = g.vers[v2].firstarc;
g.vers[v1].firstarc = p1;
g.vers[v2].firstarc = p2;
}
return 1;
}
int visited[1000];
void Depth(algraph g, int v);
void DepthTraverse(algraph g)
{
memset(visited, 0, sizeof(visited));
for (int i = 0; i < g.vexnum; i++)
{
if (visited[i] == 0)
Depth(g, i);
}
}
void Depth(algraph g,int v)
{
cout <<"v"<< g.vers[v].data <<" ";
visited[v]=1;
arcnode* p = g.vers[v].firstarc;
while (p)
{
int w =Locate(g, p->adjvex);
if (!visited[w])
Depth(g, w);
p = p->next;
}
}
int main() {
algraph g;
CreateUDG(g);
DepthTraverse(g);
return 0;
}
拓扑排序!!!
7041
#include<bits/stdc++.h>
using namespace std;
typedef struct arcnode {
int adjvex;
struct arcnode* next;
}arcnode; //边节点
typedef struct vnode {
int data;
int indegree, outdegree;
arcnode* firstarc;
}vnode, * adjlist;
typedef struct {
int vexnum, arcnum;
adjlist vers;
}algraph;
int Locate(algraph g, int index)
{
int flag = 0;
for (int i = 0; i < g.vexnum; i++)
{
if (g.vers[i].data==index)
{
flag = 1;
return i;
}
}
if (flag == 0)
return -1;
}
int CreateDG(algraph & g)
{
cin >> g.vexnum >> g.arcnum;
g.vers = new vnode[g.vexnum];
for (int i=0; i<g.vexnum; i++)
{
cin >> g.vers[i].data;
g.vers[i].firstarc=NULL;
g.vers[i].indegree=0;
g.vers[i].outdegree=0;
}
for (int i=0; i < g.arcnum; i++) {
int v1,v2;
cin >>v1>>v2;
v1 = Locate(g, v1);
v2 = Locate(g, v2);
g.vers[v2].indegree++;
g.vers[v1].outdegree++;
arcnode* p1=new arcnode;
p1->adjvex=v2+1;
p1->next=g.vers[v1].firstarc;
g.vers[v1].firstarc = p1;
}
return 1;
}
int AOVsort(algraph g) {
stack<int> s;
for (int i = 0; i < g.vexnum;i++)
{
if (g.vers[i].indegree == 0)
s.push(i);
}
int num=0;//计数
queue<int> q;
while (!s.empty())
{
int temp=s.top();
s.pop();
q.push(temp + 1);
num++;
arcnode* p = g.vers[temp].firstarc;
while (p)
{
int a = p->adjvex;
g.vers[Locate(g,a)].indegree--;
if (g.vers[Locate(g, a)].indegree == 0)
s.push(Locate(g, a));
p = p->next;
}
}
if (num<g.vexnum)
return -1;
else {
while (!q.empty())
{
cout << "v" << q.front() << " ";
q.pop();
}
return 1;
}
}
int main() {
algraph g;
CreateDG(g);
AOVsort(g);
return 0;
}