C++邻接表,逆邻接表

Powered by:NEFU AB_IN

AdjacencyList

邻接表逆邻接表的创建与打印操作。

邻接表:更注重出度的统计。
逆邻接表:更注重入度的统计。

以下面一个有向图为例

输入:
4
1 2 3
1 3 4
2 1 2
3 4 1
0 0 0

在这里插入图片描述

/*
 * @Description: https://blog.csdn.net/qq_45859188
 * @Author: NEFU AB_IN
 * @version: 1.0
 * @Date: 2021-03-19 21:05:17
 * @LastEditors: NEFU AB_IN
 * @LastEditTime: 2021-03-20 16:09:01
 */
#include<bits/stdc++.h>
using namespace std;
#define ll                          long long
#define ull                         unsigned long long
#define ld                          long double
#define db                          double
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define MP                          make_pair
#define PB                          emplace_back
#define SZ(X)                       ((int)(X).size())   
#define mset(s, _)                  memset(s, _, sizeof(s))
#define IOS                         ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl                        "\n"
#define forn(i, l, r)                for (int i = l; i <= r; i++)
typedef pair<int, int>               pii;
typedef pair<ll, ll>                 pll;
const int INF = 0x3f3f3f3f;


namespace AdjacencyListDef{
    const int N = 1e3 + 10;
    typedef struct Node{ //表结点
        int adjvex; //指向的结点下标
        int w; //边权
        struct Node *next;
    }ANode;

    typedef struct VNode{ //头节点
        int data; // 顶点信息
        ANode *firstA;
    }VNode;
    void CreateAdjList(VNode G[], int &n, int &m){
        m = 0;
        cin >> n;
        forn(i, 1, n){ //init
            G[i].data = i;
            G[i].firstA = NULL;
        }
        int a, b, w;
        cin >> a >> b >> w;
        while(a && b){ //尾插法
            ANode *p;
            p = (ANode*)malloc(sizeof(ANode));
            p->adjvex = b;
            p->w = w;
            p->next = G[a].firstA;
            G[a].firstA = p;
            m ++;
            cin >> a >> b >> w;
        }
    }

    void CreateReverseAdjList(VNode G[], int &n, int &m){
        m = 0;
        cin >> n;
        forn(i, 1, n){ //init
            G[i].data = i;
            G[i].firstA = NULL;
        }
        int a, b, w;
        cin >> a >> b >> w;
        while(a && b){ //尾插法
            ANode *p;
            p = (ANode*)malloc(sizeof(ANode));
            p->adjvex = a;
            p->w = w;
            p->next = G[b].firstA;
            G[b].firstA = p;
            m ++;
            cin >> a >> b >> w;
        }
    }
}
using namespace AdjacencyListDef;

void CreatePositive(){
    VNode G[N];
    int n, m; // 结点数、边数
    CreateAdjList(G, n, m);
    ANode *p;
    p = (ANode*)malloc(sizeof(ANode));
    cout << n << " " << m << endl;
    forn(i, 1, n){
        int cnt = 0;
        p = G[i].firstA;
        while(p){
            cnt ++;
            cout << i << " " << p->adjvex << " " << p->w << endl;
            p = p->next;
        }
        cout << i << "->OutDegree:" << cnt << endl; 
    }
}
void CreateReverse(){
    VNode RG[N];
    int n, m;
    CreateReverseAdjList(RG, n, m);
    ANode *p;
    p = (ANode*)malloc(sizeof(ANode));
    cout << n << " " << m << endl;
    forn(i, 1, n){
        int cnt = 0;
        p = RG[i].firstA;
        while(p){
            cnt ++;
            cout << i << " " << p->adjvex << " " << p->w << endl;
            p = p->next;
        }
        cout << i << "->InDegree:" << cnt << endl; 
    }
}

void solve(){
    CreatePositive();
    /*
    4 4
    1 3 4
    1 2 3
    1->OutDegree:2
    2 1 2
    2->OutDegree:1
    3 4 1
    3->OutDegree:1
    4->OutDegree:0
    */
    CreateReverse();
    /*
    4 4
    1 2 2
    1->InDegree:1
    2 1 3
    2->InDegree:1
    3 1 4
    3->InDegree:1
    4 3 1
    4->InDegree:1
    */
}

int main()
{
    IOS;
    solve();
    return 0;
}

完结。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

NEFU AB-IN

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

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

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

打赏作者

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

抵扣说明:

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

余额充值