Pagini recente » Cod sursa (job #936750) | Cod sursa (job #2312305) | Cod sursa (job #1956759) | Cod sursa (job #845504) | Cod sursa (job #2268810)
#include <bits/stdc++.h>
#define NMAX 100001
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, d[NMAX], low[NMAX], step;
vector <int> v[NMAX];
vector < vector <int> > ans;
bool check[NMAX];
stack <int> st;
void DFS(int nod)
{
d[nod] = low[nod] = ++step;
st.push(nod);
check[nod] = true;
for(int i = 0; i < v[nod].size(); i++)
{
int nod_urm = v[nod][i];
if(d[nod_urm] == 0)
{
DFS(nod_urm);
low[nod] = min(low[nod], low[nod_urm]);
}
else
if(check[nod_urm])
{
low[nod] = min(low[nod], d[nod_urm]);
}
}
if(low[nod] == d[nod])
{
vector <int> p;
while(st.top()!= nod)
{
p.push_back(st.top());
check[st.top()] = false;
st.pop();
}
st.pop();
check[nod] = false;
p.push_back(nod);
ans.push_back(p);
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int a, b;
fin >> a >> b;
v[a].push_back(b);
}
DFS(1);
fout << ans.size() << "\n";
for(int i = 0; i < ans.size(); i++)
{
for(int j = 0; j < ans[i].size(); j++)
fout << ans[i][j] << " ";
fout << '\n';
}
return 0;
}