Before the chain better on-time performance and memory performance to the star than the vector adjacent table.
Former star chain usage to see the following template code:
#include <bits / STDC ++ H.> the using namespace STD; const int MAXN = 1E3 + 10 ; // Maximum Points const int MAXM = 1e6 + 10 ; // maximum number of edges struct Node { int to, W, Next;} Edge [MAXM ]; // set of edges, to the end, w right side, next one on the same side of the starting point numbers int n, m, head [MAXN]; // input n points, m edges struct Chain_Forward_Star { // chain forward star int CNT; void the init () { // initialization CNT = 0 ; Memset (head, -. 1 , the sizeof (head)); // initializes -1 } void the Add ( int U, int V, int W) { // bordered, u starting, v end, w right side Edge [CNT] .to = V ; edge [CNT] .W = W; edge [CNT] .next = head [u]; // likewise u starting on one side of the numbering head [u] = CNT ++; // updated to as a starting point u on one side of the numbering } } CFS; int main () { int U, V, W; CIN >> >> n- m; CFS.init (); for ( int I =. 1 ; I <= m; I ++ ) { CIN >> >> U V W >>; // U starting point, v end, w right side CFS.add (U, v, w); // CFS.add (V , U, W); } for ( int I = . 1 ; I <= n-; I ++ ) { COUT << I << endl; for ( int J = head [I]; ~ J; J = Edge [J]. Next) // reverse traversal starting from edge i, ~ j = i.e. J -!. 1 COUT << i << ' ' << edge [J] .to << ' ' << edge [J] .W < < endl; COUT << endl; } return 0; }