Pagini recente » Cod sursa (job #2408313) | Cod sursa (job #78997) | Cod sursa (job #68353) | Cod sursa (job #2824668) | Cod sursa (job #3030847)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector <int> g[N];
int id[N], low[N], t;
bool inst[N];
stack <int> st;
vector <vector <int>> ctc;
void dfs(int u) {
id[u] = low[u] = ++t;
inst[u] = true; st.push(u);
for(int v : g[u]) {
if(!id[v]) {
dfs(v);
low[u] = min(low[u], low[v]);
} else if(inst[v]) low[u] = min(low[u], id[v]);
}
if(low[u] == id[u]) {
ctc.push_back({u});
while(st.top() != u) {
inst[st.top()] = false;
ctc.back().push_back(st.top());
st.pop();
}
inst[u] = false;
st.pop();
}
}
int main()
{
#ifndef HOME
ifstream cin("ctc.in");
ofstream cout("ctc.out");
#else
ios_base :: sync_with_stdio(false); cin.tie(0);
#endif
int n, m;
cin >> n >> m;
for(int i = 0, u, v; i < m; i++)
cin >> u >> v,
g[u].push_back(v);
for(int i = 1; i <= n; i++) if(!id[i])
dfs(i);
for(auto& c : ctc) {
for(int x : c)
cout << x << " ";
cout << "\n";
}
return 0;
}