邻接表这种高科技的小儿科,你会就是小儿科 ,不会啦就是高科技。
如下图所示输入边为
1 2
1 3
1 4
2 4
3 4
0 | 1 | 2 | 3 | 4 | 5 | 6 | |
v | 2 | 3 | 4 | 4 | 4 | ||
next | -1 | 0 | 1 | -1 | 3 |
head
0 -1
1 -1 0 1 2 head【i】保存的是与i相连的e数组的下标;
2 -1 3 next保存的是与v相连的那个 和其他相连的下标 若next=-1表示没有其他与i相连的啦
3 -1 4
4 -1
5 -1
int head[maxn] int cnt=0; struct node { int v,next; }e[maxn]; void add(int u,int v) { e[cnt].v=v; e[cnt].next=head[u]; head[u]=cnt++; }