求各入度(邻接表)

Problem Description

设有一有向图,其顶点值为字符型并假设各值互不相等,采用邻接表表示法存储表示。设计一个算法,求该图中所有顶点的入度值(要求按顶点的存储顺序输出)。

 Input

有多组测试数据,每组数据的第一行表示图的顶点数n和图的边数e(0<n<30);第二行表示各顶点的值,按输入顺序进行存储;接下来有e行,每一行表示每条边所依附的顶点的存储下标i和j,两个下标之间用空格隔开。

 Output

每组输出占一行,按顶点的存储顺序输出各顶点的入度值,要求每两个入度值之间有一空格。

 Sample Input

4 4
ABCD
0 1
0 3
1 2
1 3

 Sample Output

0 1 1 2
#include <iostream>
#include <stdlib.h>
#define maxvex 20
#include<stdio.h>
using namespace std;

typedef struct {
    int adjvex;

} ArcCell,ArcCells[maxvex][maxvex];

typedef struct {
    char vertex[maxvex];
    ArcCells arcs;
    int arcnum,vexnum;
} MatrixGraph;


struct ArcNode {
    int adj;
    ArcNode* next;
};

typedef struct {
    ArcNode* firstedge;
    char vex;
} Edge,Edges[maxvex];

typedef struct {
    Edges edges;
    int arcnum,vertexnum;
    int deg[maxvex];
} Graph;

void CreatDG(Graph&G,int n,int e) {
    G.arcnum=e;
    G.vertexnum=n;
    for(int i=0; i<n; i++) {
        cin>>G.edges[i].vex;
        G.edges[i].firstedge=NULL;
        G.deg[i]=0;
    }
    for(int i=0; i<e; i++) {
        int a,b;
        cin>>a>>b;
        ArcNode* p=new ArcNode;
        p->adj=b;
        p->next=G.edges[a].firstedge;
        G.edges[a].firstedge=p;
    }
}

void Deg(Graph& G) {
    for(int i=0; i<G.vertexnum; i++) {
        ArcNode* p=G.edges[i].firstedge;
        while(p) {
            G.deg[p->adj]++;
            p=p->next;
        }
    }

}

void Print(Graph G) {

    for(int i=0; i<G.vertexnum; i++) {

        ArcNode* p=G.edges[i].firstedge;
        while(p) {
            cout<<p->adj<<" ";
            p=p->next;
        }
        cout<<endl;
    }
}


void CreatUDM(MatrixGraph &G,int n,int e) {

    G.arcnum=e;
    G.vexnum=n;
    for(int i=0; i<G.vexnum; i++) {
        cin>>G.vertex[i];
        for(int j=0; j<G.vexnum; j++) {
            G.arcs[i][j].adjvex=0;

        }
    }



    for(int i=0; i<G.arcnum; i++) {
        int a,b;
        cin>>a>>b;
        G.arcs[a][b].adjvex=1;
        G.arcs[b][a].adjvex=1;
    }

}








bool vistited[maxvex];

void InitVistied() {

    for(int i=0; i<maxvex; i++) {
        vistited[i]=false;
    }
}



void MatrixDFSTraverse(MatrixGraph G,int v) {
    vistited[v]=true;
    cout<<G.vertex[v]<<":";
    for(int j=0; j<G.vexnum; j++) {
        if(G.arcs[v][j].adjvex!=0) {
            cout<<G.vertex[j];
        }
    }
    cout<<endl;
    for(int i=0; i<G.vexnum; i++) {
        if(!vistited[i]&&G.arcs[v][i].adjvex!=0) {
            MatrixDFSTraverse(G,i);
        }
    }


}

int main() {
   
    int n,e;

    while(cin>>n>>e) {
        Graph g;
        CreatDG(g,n,e);
        Deg(g);
        for(int i=0; i<g.vertexnum-1; i++) {
            cout<<g.deg[i]<<" ";
        }
        cout<<g.deg[g.vertexnum-1]<<endl;

    }
    return 0;
}




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haibianyoushark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值