Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <numeric>
- #include <cmath>
- #include <climits>
- #include <stack>
- #include <iomanip>
- #include <queue>
- using namespace std;
- const int N = 1e5;
- vector<vector<int>> Adj;
- int x, y, n, m, Number[N+1], Count, componentcount, Low[N+1];
- bool Free[N+1];
- stack<int> Stack;
- void read()
- {
- cin >> n >> m;
- Adj.resize(n + 1);
- for (int i = 1; i <= m; ++ i)
- {
- cin >> x >> y;
- Adj[x].push_back(y);
- }
- Count = 0; componentcount = 0;
- fill (Free, Free + n + 1, true);
- fill(Number, Number + n + 1, 0);
- }
- void Visit(int u)
- {
- ++Count;
- Number[u] = Count;
- Low[u] = Number[u];
- Stack.push(u);
- for (int v : Adj[u])
- {
- if (Free[v])
- {
- if (Number[v] != 0)
- {
- Low[u] = min(Low[u], Number[v]);
- }
- else
- {
- Visit(v);
- Low[u] = min(Low[u], Low[v]);
- }
- }
- }
- if (Number[u] == Low[u])
- {
- int v;
- ++componentcount;
- cout << "Component " << componentcount << ": ";
- do
- {
- v = Stack.top();
- Stack.pop();
- cout << v << ' ';
- Free[v] = false;
- }
- while (v != u);
- cout << '\n';
- }
- }
- void solve()
- {
- for (int i = 1; i <= n; ++ i)
- {
- if (Number[i] == 0)
- {
- Visit(i);
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- freopen("TESTCODE.INP", "r", stdin);
- read();
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement