category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Course Schedule,

Approach(Kahn’s algo)

Code sample (Kahn's algo) ```cpp int n, m; std::cin >> n >> m; std::vector graph[n + 1], revGraph[n + 1], inEdge(n + 1); for (int i = 0; i < m; i++) { int a, b; std::cin >> a >> b; graph[a].push_back(b); revGraph[b].push_back(a); inEdge[b]++; } std::queue qu; for (int i = 1; i <= n; i++) { if (inEdge[i] == 0) { qu.push(i); } } std::vector order, dp(n + 1, 0); while (!qu.empty()) { auto u = qu.front(); qu.pop(); order.push_back(u); for (const auto &v : graph[u]) { if (--inEdge[v] == 0) { qu.push(v); } } } dp[0] = dp[1] = 1; for (int i = 1; i < n; i++) { int u = order[i]; for (const auto &v : revGraph[u]) { dp[u] += dp[v]; dp[u] %= mod; } } std::cout << dp[n] << '\n'; int main() { int n, m; cin >> n >> m; vector<vector> graph(n + 1); vector in_degree(n + 1); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; graph[a].push_back(b); in_degree[b]++; } queue qu; for (int i = 1; i <= n; i++) if (in_degree[i] == 0) qu.push(i); vector order; while (!qu.empty()) { auto u = qu.front(); qu.pop(); order.push_back(u); for (const auto &v : graph[u]) { if (--in_degree[v] == 0) { qu.push(v); } } } if (int(order.size()) != n) { cout << "IMPOSSIBLE\n"; } else { for (const auto &i : order) { cout << i << ' '; } ``` </details> #### Approach(DFS type) _... to be updated._