Pagini recente » Cod sursa (job #1372498) | Cod sursa (job #2175671) | Cod sursa (job #1000938) | Cod sursa (job #1613995) | Cod sursa (job #2031069)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
int N, M, post[100001],nr,nrc;
bool viz[100001];
struct nod
{
int x;
nod *next;
};
nod *v1[100001],*v2[100001],*cc[100001];
void add(nod *&cap_lst, int nod_ad)
{
nod *p;
p = new nod;
p -> x = nod_ad;
p -> next = cap_lst;
cap_lst = p;
}
void citiregraf()
{
f >> N >> M;
while(M--)
{
int x, y;
f >> x >> y;
add(v1[x], y);
add(v2[y], x);
}
}
void DFS(int vf)
{
nod *p;
viz[vf] = 1;
for(p = v1[vf]; p != NULL; p = p -> next)
if(!viz[p -> x])
DFS(p -> x);
post[++nr]=vf;
}
void DFSt(int vf)
{
nod *p;
viz[vf] = 0;
add(cc[nrc],vf);
for(p = v2[vf]; p != NULL; p = p -> next)
if(viz[p -> x])
DFSt(p -> x);
}
void componente()
{
int i;
for(i = 1; i <= N; i++)
if(viz[i] == 0)
DFS(i);
for(i=N;i>0;i--)
if(viz[post[i]]==1){
nrc++;
DFSt(post[i]);
}
}
void afis(){
g << nrc << '\n';
for(int i = 1; i <= nrc; i++)
{
for(nod *p = cc[i]; p != NULL; p = p->next)
g << p->x << ' ';
g << '\n';
}
}
int main()
{
citiregraf();
componente();
afis();
return 0;
}