數組模擬實現鄰接表

之間都是用vector模擬鄰接表,但是後面發現vector時間複雜度有點高,所以寫了份用數組模擬鏈表的方法實現鄰接表


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
/******************/
#define LL long long
const int maxn=100010;

template<int N,int M>
struct Graph
{
    int top;
    struct Vertex
    {
        int head;
    }V[N];
    struct Edge
    {
        int v,next;
    }E[M];
    void init()
    {
        memset(V,-1,sizeof V);
        top=0;
    }
    void add_edge(int u,int v,int w)
    {
        E[top].v=v;
        E[top].next=V[u].head;
        V[u].head=top++;
    }
};

Graph<maxn,maxn> g;

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        g.init();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章