In the past few days, I have encountered some questions that have high requirements for edge building. However, it is difficult for a vector to build edges, so we learned the adjacent table .. The following are my views on the adjacent table. The storage method of the adjacent table is a linked list of each node and a linked list created by the header insertion method. here we first use an array for simulation .. First [u] and next [e] represent the number of the first edge of node u and the number of the next edge of the e edge respectively .. The implementation code is: n. in the past few days, I have encountered some problems with high requirements on edge creation. However, it is difficult for a vector to build edges, so we learned the adjacent table .. The following are my views on the adjacent table. Storage method of the adjacent table
An adjacent table is a linked list of each node and a linked list created by the header insertion method. here we first use an array for simulation .. First [u] and next [e] represent the number of the first edge of node u and the number of the next edge of the e edge respectively .. The implementation code is:
next[e]=head[u[e]];head[u[e]]=e;
Then it will be very useful if it is used together with the Struct ..
struct Edge{ int to,val,next;}edge[maxn];int head[maxn];void addedge(int x,int y,int val){ edge[++cnt].to=y; edge[cnt].val=val; edge[cnt].next=head[x]; head[x]=cnt;}The traversal method of the corresponding adjacent table is also available.
for(int i=head[x],i!=-1;i=next[i])
The second traversal method is:
for(int i=head[x],i!=-1;i=edge[i].next)
While vector can achieve the same effect as the adjacent table, the vector container is actually a dynamic array, but unlike the adjacent table, it is stored in sequence, so the storage and traversal are very simple.
Traverse for (int I = 0; I
< pre>
The representation is exactly the same as the array ..
Correct your understanding...
<>