这个东西其实和vector差不多,但是确实比vector快,参考c++图论vector邻接表与链式前向星性能差异
逻辑是这样的,有一个head,他标记了上一个边是谁,最开始当然是-1喽,每新add一条边,当前边的to更新为到哪个点,next更新为上一条边的序号,head更新为当前序号,这样就实现了从后往前遍历.
(据说可以实现从小到大排序,我似乎没看到...)
https://blog.csdn.net/acdreamers/article/details/16902023讲的很详细的一片博客\
核心操作:
int head[maxn];
struct Edge
{
int to,next;//w(weight)
} edges[maxm]; //边从0保存
int tot; //计数边总数
/*
head 初始化-1,tot初始化0
*/
void add_edge(int from,int to)
{
edges[tot]=(Edge){to,head[from]};
head[from]=tot++;
in[to]++;
}
遍历的时候:
for(int e = head[u];e!=-1;e = edges[e].next)
{
操作
}