Pagini recente » Statistici Ionita Vlad (vlad_ionita) | Cod sursa (job #587400) | Cod sursa (job #2685064) | Clasament simulare_oji2012_clasa_a_9-a | Cod sursa (job #2012138)
#include <iostream>
#include <cstdio>
#include <bitset>
#include <vector>
#include <stack>
using namespace std;
const int MuchMax = 200005;
const int NodMax = 100005;
stack < int > st;
vector < int > G[MuchMax], Gt[MuchMax];
vector < int > comp[NodMax];
bitset < NodMax > viz;
int N, M, ctc;
void Read()
{
scanf("%d %d", &N, &M);
for(int i=1; i<=M; ++i)
{
int x, y;
scanf("%d %d", &x, &y);
G[x].push_back(y);
Gt[y].push_back(x);
}
}
void DFS(int nod)
{
viz[nod] = 1;
vector < int > ::iterator it;
for(it=G[nod].begin(); it!=G[nod].end(); ++it)
{
if(!viz[*it])
{
DFS(*it);
}
}
st.push(nod);
}
void DFSt(int nod)
{
viz[nod] = 1;
comp[ctc].push_back(nod);
vector < int > ::iterator it;
for(it=Gt[nod].begin(); it!=Gt[nod].end(); ++it)
{
if(!viz[*it])
{
DFSt(*it);
}
}
}
int main()
{
freopen("ctc.in", "r", stdin);
freopen("ctc.out", "w", stdout);
Read();
for(int i=1; i<=N; ++i)
{
if(!viz[i])
{
DFS(i);
}
}
viz.reset();
while(!st.empty())
{
int vf = st.top();
if(!viz[vf])
{
DFSt(vf);
++ctc;
}
st.pop();
}
cout << ctc << "\n";
for(int i=0; i<ctc; ++i)
{
vector < int > ::iterator it;
for(it=comp[i].begin(); it!=comp[i].end(); ++it)
cout << *it << " ";
cout<<"\n";
}
return 0;
}