【问题描述】已知一个有向图,如下图所示,试编写一个算法,以邻接表作为存储结构,计算各顶点的入度和出度。
【输入形式】
顶点数,边数
顶点序列
边序列
【输出形式】顶点及入度和出度
【样例输入】
6 9
012345
0122
102
0216
214
149
258
537
5412
4315
【样例输出】
0 1 2
1 2 2
2 1 2
3 2 0
4 2 1
5 1 2
#include<iostream>
//#include<string>
using namespace std;
# define Maxsize 20
struct arcnode {
int adjvex;
struct arcnode* next;
};
struct vnode {
int data;
struct acrnode* first;
};
struct graph {
vnode vertices;
int vexnum, arcnum;
};
struct linjiebiao {
int biao[Maxsize][3];
int side;
};
void create(linjiebiao& biao)
{
for (int i = 0; i < biao.side; i++)
{
for (int j = 0; j < 3; j++)
{
int num;
cin >> num;
biao.biao[i][j] = num;
}
}
}
int chudu(linjiebiao biao, int vertex)
{
int count = 0;
for (int i = 0; i < biao.side; i++)
{
if (biao.biao[i][0] == vertex)
count++;
}
return count;
}
int rudu(linjiebiao& biao, int vertex)
{
int count = 0;
for (int i = 0; i < biao.side; i++)
{
if (biao.biao[i][1] == vertex)
count++;
}
return count;
}
int main()
{
int dian;
cin >> dian;
int side;
cin >> side;
int vertexlist[Maxsize];
for (int i = 0; i < dian; i++)
{
int num;
cin >> num;
vertexlist[i] = num;
}
linjiebiao biao;
biao.side = side;
create(biao);
for (int j = 0; j < dian; j++)
{
cout << j << " " << rudu(biao, j) << " " << chudu(biao, j) << endl;
}
return 0;
}