数组模拟实现邻接表

来源:互联网 发布:云计算 大事件 编辑:程序博客网 时间:2024/06/05 00:47

之间都是用vector模拟邻接表,但是后面发现vector时间复杂度有点高,所以写了份用数组模拟链表的方法实现邻接表


#include<iostream>#include<cstdio>#include<cstring>using namespace std;/******************/#define LL long longconst 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;}


0 0
原创粉丝点击