This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_C" #include <bits/stdc++.h> using namespace std; #include "../DfsTree/StronglyConnected.h" int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); while (m--) { int u, v; cin >> u >> v; g[u].push_back(v); } DirectedDfs tree(g); int q; cin >> q; while (q--) { int u, v; cin >> u >> v; cout << (tree.comp_ids[u] == tree.comp_ids[v]) << '\n'; } }
#line 1 "Graph/tests/aizu_grl_3_c_strongly_connected.test.cpp" #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_3_C" #include <bits/stdc++.h> using namespace std; #line 1 "Graph/DfsTree/StronglyConnected.h" // Index from 0 // Usage: // DirectedDfs tree; // Now you can use tree.scc // // Note: reverse(tree.scc) is topo sorted // // Tested: // - (requires scc to be topo sorted) https://judge.yosupo.jp/problem/scc // - https://cses.fi/problemset/task/1686/ // - (edges have costs) https://oj.vnoi.info/problem/bedao_g16_b struct DirectedDfs { vector<vector<int>> g; int n; vector<int> num, low, current, S; int counter; vector<int> comp_ids; vector< vector<int> > scc; DirectedDfs(const vector<vector<int>>& _g) : g(_g), n(g.size()), num(n, -1), low(n, 0), current(n, 0), counter(0), comp_ids(n, -1) { for (int i = 0; i < n; i++) { if (num[i] == -1) dfs(i); } } void dfs(int u) { low[u] = num[u] = counter++; S.push_back(u); current[u] = 1; for (auto v : g[u]) { if (num[v] == -1) dfs(v); if (current[v]) low[u] = min(low[u], low[v]); } if (low[u] == num[u]) { scc.push_back(vector<int>()); while (1) { int v = S.back(); S.pop_back(); current[v] = 0; scc.back().push_back(v); comp_ids[v] = ((int) scc.size()) - 1; if (u == v) break; } } } // build DAG of strongly connected components // Returns: adjacency list of DAG std::vector<std::vector<int>> build_scc_dag() { std::vector<std::vector<int>> dag(scc.size()); for (int u = 0; u < n; u++) { int x = comp_ids[u]; for (int v : g[u]) { int y = comp_ids[v]; if (x != y) { dag[x].push_back(y); } } } return dag; } }; #line 7 "Graph/tests/aizu_grl_3_c_strongly_connected.test.cpp" int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); while (m--) { int u, v; cin >> u >> v; g[u].push_back(v); } DirectedDfs tree(g); int q; cin >> q; while (q--) { int u, v; cin >> u >> v; cout << (tree.comp_ids[u] == tree.comp_ids[v]) << '\n'; } }