zoukankan      html  css  js  c++  java
  • 数据结构-邻接表学习

    转载请注明来源https://www.cnblogs.com/TheSilverMoon/p/9309491.html

    在学会邻接矩阵之后,我等渣渣算是了解了一种图的储存方式,但是邻接矩阵有着一个缺点,那就是不适合存稀疏图(时空复杂度均为n^2),否则会爆Memory,然后你就会收到一个长得真不怎么好看的MLE。这个时候邻接表就来了,简单理解的话就是用单链表来表示图。

     有向无权图

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pb push_back
    #define fio ios::sync_with_stdio(false);cin.tie(0);
    #define pii pair<int,int>
    #define vi vector<int>
    #define vc vector<char>
    #define pi 3.1415926
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    const int INF=0x3f3f3f3f;
    const int N=2e5+5;
    
    typedef long long ll;
    typedef double db;
    typedef unsigned long long ull;
    using namespace std;
    
    vi Graph[N];
    
    int main()
    {
        fio;
        int n,m;//n个点,m组数据
        cin>>n>>m;
        int start,to;
        for (int i=1;i<=m;i++)
        {
            cin>>start>>to;
            Graph[start].pb(to);//有向无权图
        }
    }

    无向无权图

    int main()
    {
        fio;
        int n,m;//n个点,m组数据
        cin>>n>>m;
        int start,to;
        for (int i=1;i<=m;i++)
        {
            cin>>start>>to;
            Graph[start].pb(to);
            Graph[to].pb(start);
        }
    }

    有向有权图

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pb push_back
    #define fio ios::sync_with_stdio(false);cin.tie(0);
    #define pii pair<int,int>
    #define vi vector<int>
    #define vc vector<char>
    #define pi 3.1415926
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    const int INF=0x3f3f3f3f;
    const int N=2e5+5;
    
    typedef long long ll;
    typedef double db;
    typedef unsigned long long ull;
    using namespace std;
    
    struct node
    {
        int to;
        int value;
    };
    
    vector<node> Graph[N];
    int main()
    {
        fio;
        int n,m;//n个点,m组数据
        cin>>n>>m;
        int start,to;
        for (int i=1;i<=m;i++)
        {
            node temp;
            cin>>start>>temp.to>>temp.value;
            Graph[start].pb(temp);
        }
    }

    有向无权图

    struct node
    {
        int to;
        int value;
    };
    
    vector<node> Graph[N];
    int main()
    {
        fio;
        int n,m;//n个点,m组数据
        cin>>n>>m;
        int start,to;
        for (int i=1;i<=m;i++)
        {
            node temp;
            cin>>start>>temp.to>>temp.value;
            Graph[start].pb(temp);
            swap(start,temp.to);
            Graph[start].pb(temp);
        }
    }
  • 相关阅读:
    Java8系列之重新认识HashMap(转)
    kafka 消费者和生产者测试类
    zookeeper配置
    JNLP应用程序无法打开的解决办法
    Functional Programming 资料收集
    Python的问题解决: IOError: [Errno 32] Broken pipe
    python的subprocess的简单使用和注意事项
    Python tricks(7) -- new-style class的__slots__属性
    Python tricks(6) -- python代码执行的效率
    Python tricks(5) -- string和integer的comparison操作
  • 原文地址:https://www.cnblogs.com/TheSilverMoon/p/9309491.html
Copyright © 2011-2022 走看看