#include #include namespace nachia{ struct AdjacencyList{ public: struct AdjacencyListRange{ using iterator = typename std::vector::const_iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)std::distance(begi, endi); } const int& operator[](int i) const { return begi[i]; } }; private: int mn; std::vector E; std::vector I; public: AdjacencyList(int n, std::vector> edges, bool rev){ mn = n; std::vector buf(n+1, 0); for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; } for(int i=1; i<=n; i++) buf[i] += buf[i-1]; E.resize(buf[n]); for(int i=(int)edges.size()-1; i>=0; i--){ auto [u,v] = edges[i]; E[--buf[u]] = v; if(rev) E[--buf[v]] = u; } I = std::move(buf); } AdjacencyList(const std::vector>& edges = {}){ int n = mn = edges.size(); std::vector buf(n+1, 0); for(int i=0; i targets, std::vector bounds){ AdjacencyList res; res.mn = bounds.size() - 1; res.E = std::move(targets); res.I = std::move(bounds); return res; } AdjacencyListRange operator[](int u) const { return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] }; } int num_vertices() const { return mn; } int num_edges() const { return E.size(); } AdjacencyList reversed_edges() const { AdjacencyList res; int n = res.mn = mn; std::vector buf(n+1, 0); for(int v : E) ++buf[v]; for(int i=1; i<=n; i++) buf[i] += buf[i-1]; res.E.resize(buf[n]); for(int u=0; u::const_iterator; iterator begi, endi; iterator begin() const { return begi; } iterator end() const { return endi; } int size() const { return (int)std::distance(begi, endi); } const Edge& operator[](int i) const { return begi[i]; } }; private: int mn; std::vector E; std::vector I; public: AdjacencyListEdgeIndexed(int n, const std::vector>& edges, bool rev){ mn = n; std::vector buf(n+1, 0); for(auto [u,v] : edges){ ++buf[u]; if(rev) ++buf[v]; } for(int i=1; i<=n; i++) buf[i] += buf[i-1]; E.resize(buf[n]); for(int i=(int)edges.size()-1; i>=0; i--){ auto [u,v] = edges[i]; E[--buf[u]] = { v, i }; if(rev) E[--buf[v]] = { u, i }; } I = std::move(buf); } AdjacencyListEdgeIndexed() : AdjacencyListEdgeIndexed(0, {}, false) {} AdjacencyListRange operator[](int u) const { return AdjacencyListRange{ E.begin() + I[u], E.begin() + I[u+1] }; } int num_vertices() const { return mn; } int num_edges() const { return E.size(); } AdjacencyListEdgeIndexed reversed_edges() const { AdjacencyListEdgeIndexed res; int n = res.mn = mn; std::vector buf(n+1, 0); for(auto [v,i] : E) ++buf[v]; for(int i=1; i<=n; i++) buf[i] += buf[i-1]; res.E.resize(buf[n]); for(int u=0; u #include namespace nachia{ class BiconnectedComponents{ private: int mn; int mm; int mnum_bcs; std::vector> medges; std::vector edgeidx_to_bcidx; public: BiconnectedComponents(int n, std::vector> edges){ std::vector dfsi_to_vtx; std::vector vtx_to_dfsi; std::vector linked_over; std::vector dfs_parent; mn = n; int m = edges.size(); medges = std::move(edges); nachia::AdjacencyListEdgeIndexed adj(n, medges, true); dfsi_to_vtx.resize(n); vtx_to_dfsi.resize(n); dfs_parent.assign(n, -1); linked_over.assign(n, -1); int dfsi = 0; auto dfs1 = [&](int p, auto self)->int { vtx_to_dfsi[p] = dfsi; dfsi_to_vtx[dfsi] = p; int backedge = dfsi; dfsi++; for(auto [nx,i] : adj[p]){ if(dfs_parent[nx] != -1) backedge = std::min(backedge, vtx_to_dfsi[nx]); else{ dfs_parent[nx] = i; int link = self(nx, self); backedge = std::min(backedge, link); linked_over[nx] = (link < vtx_to_dfsi[p]) ? 1 : 0; } } return backedge; }; for(int i=0; i res(m); auto dfs2 = [&](int p, int bcid, int& maxbcid, auto self)-> void { if(dfs_parent[p] < 0){ for(auto [nx,i] : adj[p]) if(dfs_parent[nx] == i){ bcid = maxbcid++; self(nx, bcid, maxbcid, self); } return; } for(auto [nx,i] : adj[p]) if(dfs_parent[nx] != i) res[i] = bcid; for(auto [nx,i] : adj[p]) if(dfs_parent[nx] == i){ int nx_bcid = bcid; if(!linked_over[nx]) nx_bcid = maxbcid++; self(nx, nx_bcid, maxbcid, self); } }; int bcid = 0; for(int i=0; i> get_bcs() const { std::vector> res(mnum_bcs); for(int i=0; i buf(mnum_bcs+1); for(int bci : edgeidx_to_bcidx) ++buf[bci]; for(int i=1; i<=mnum_bcs; i++) buf[i] += buf[i-1]; std::vector E(buf.back()); for(int i=0; i> res(bct_n - 1); int resi = 0; std::vector visited(mn); for(int bci=0; bci namespace nachia{ struct HeavyLightDecomposition{ private: int N; std::vector P; std::vector PP; std::vector PD; std::vector D; std::vector I; std::vector rangeL; std::vector rangeR; public: HeavyLightDecomposition(const AdjacencyList& E = AdjacencyList(1, {}, false)){ N = E.num_vertices(); P.assign(N, -1); I = {0}; I.reserve(N); for(int i=0; i<(int)I.size(); i++){ int p = I[i]; for(int e : E[p]) if(P[p] != e){ I.push_back(e); P[e] = p; } } std::vector Z(N, 1); std::vector nx(N, -1); PP.resize(N); for(int i=0; i=1; i--){ int p = I[i]; Z[P[p]] += Z[p]; if(nx[P[p]] == -1) nx[P[p]] = p; if(Z[nx[P[p]]] < Z[p]) nx[P[p]] = p; } for(int p : I) if(nx[p] != -1) PP[nx[p]] = p; PD.assign(N,N); PD[0] = 0; D.assign(N,0); for(int p : I) if(p != 0){ PP[p] = PP[PP[p]]; PD[p] = std::min(PD[PP[p]], PD[P[p]]+1); D[p] = D[P[p]]+1; } rangeL.assign(N,0); rangeR.assign(N,0); std::vector dfs; dfs.push_back(0); while(dfs.size()){ int p = dfs.back(); rangeR[p] = rangeL[p] + Z[p]; int ir = rangeR[p]; dfs.pop_back(); for(int e : E[p]) if(P[p] != e) if(e != nx[p]){ rangeL[e] = (ir -= Z[e]); dfs.push_back(e); } if(nx[p] != -1){ rangeL[nx[p]] = rangeL[p] + 1; dfs.push_back(nx[p]); } } I.resize(N); for(int i=0; i PD[v]) u = P[PP[u]]; while(PP[u] != PP[v]){ u = P[PP[u]]; v = P[PP[v]]; } return (D[u] > D[v]) ? v : u; } int dist(int u, int v) const { return depth(u) + depth(v) - depth(lca(u,v)) * 2; } std::vector> path(int r, int c, bool include_root = true, bool reverse_path = false) const { if(PD[c] < PD[r]) return {}; std::vector> res(PD[c]-PD[r]+1); for(int i=0; i<(int)res.size()-1; i++){ res[i] = std::make_pair(rangeL[PP[c]], rangeL[c]+1); c = P[PP[c]]; } if(PP[r] != PP[c] || D[r] > D[c]) return {}; res.back() = std::make_pair(rangeL[r]+(include_root?0:1), rangeL[c]+1); if(res.back().first == res.back().second) res.pop_back(); if(!reverse_path) std::reverse(res.begin(),res.end()); else for(auto& a : res) a = std::make_pair(N - a.second, N - a.first); return move(res); } std::pair subtree(int p){ return std::make_pair(rangeL[p], rangeR[p]); } int to_seq(int vertex) const { return rangeL[vertex]; } int to_vtx(int seqidx) const { return I[seqidx]; } int median(int x, int y, int z) const { return lca(x,y) ^ lca(y,z) ^ lca(x,z); } int la(int from, int to, int d) const { if(d < 0) return -1; int g = lca(from,to); int dist0 = D[from] - D[g] * 2 + D[to]; if(dist0 < d) return -1; int p = from; if(D[from] - D[g] < d){ p = to; d = dist0 - d; } while(D[p] - D[PP[p]] < d){ d -= D[p] - D[PP[p]] + 1; p = P[PP[p]]; } return I[rangeL[p] - d]; } }; } // namespace nachia #include /* int main() { int n; scanf("%d", &n); int m; scanf("%d", &m); std::vector> edges(m); for(auto& [u,v] : edges) scanf("%d%d", &u, &v); auto bcs = nachia::BiconnectedComponents(n, edges).get_bcs(); printf("%d\n", (int)bcs.size()); for(auto& bc : bcs){ printf("%d", (int)bc.size()); for(auto v : bc) printf(" %d", v); printf("\n"); } return 0; } */ int main() { int n; scanf("%d", &n); int m; scanf("%d", &m); std::vector> edges(m); for(auto& [u,v] : edges){ scanf("%d%d", &u, &v); u--; v--; } auto hld_bct = nachia::HeavyLightDecomposition(nachia::BiconnectedComponents(n, edges).get_bct()); int q; scanf("%d", &q); for(int queryi=0; queryi