voidadd(int u, int v, int w){ E.push_back({ u, v, w }); } boolfindedge(int u0, int v0){ for (auto [u,v,w] : E) if (u == u0 && v == v0) return1; return0; } voiddfs(int u){ if (vis[u]) return; vis[u] = 1; for (auto e : E) if (e.u == u) dfs(e.v); }
bool E[N][N]; // Adjacency Matrix voidadd(int u, int v, int w){ E[u][v] = w; } boolfindedge(int u, int v){ return E[u][v]; } voiddfs(int u){ if (vis[u]) return; vis[u] = 1; for (int v = 1; v <= n; ++v) if (E[u][v]) dfs(v); }
// Chain Forward Star structEdge { int nxt, to, w; }; vector<Edge> E; int head[N]; inlinevoidadd(int u, int v, int w){ E.push_back({ head[u], v, w }); head[u] = E.size() - 1; } boolfindedge(int u, int v){ for (int i = head[u]; i >= 1; i = E[i].nxt) if (E[i].to == v) return1; return0; } voiddfs(int u){ if (vis[u]) return; vis[u] = true; for (int i = head[u]; i >= 1; i = E[i].nxt) dfs(E[i].to); } voideachT(){ E.push_back({ -1,0,0 }); for(;;) { add(u, v, w) }; }