邻接表

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int tot=0;
int head[maxn],nxt[maxn],ver[maxn],edge[maxn];// head nxt中存tot的下标
void add(int u,int v,int w) // 加入有向边(u,v)权值为w 
{
    ver[++tot]=v;
    edge[tot]=w;
    
    nxt[tot]=head[u];
    head[u]=tot;
}
int main()
{
    add(1,2,1);
    add(1,3,2);
    add(1,2,3);
    int x=1;
    for(int i=head[x];i;i=nxt[i]) // 找从x出发的所有有向边 
    {
        int y=ver[i];
        int z=edge[i];
        printf("%d %d %d
",x,y,z);
    }
}
原文地址:https://www.cnblogs.com/dongdong25800/p/10632042.html