Pagini recente » Cod sursa (job #3365487) | Cod sursa (job #3365494) | Cod sursa (job #3366044) | Cod sursa (job #3366033) | Cod sursa (job #3364716)
#include <iostream>
#include <fstream>
using namespace std;
const int NMAX = 100001;
struct nod
{
int x;
nod *next;
};
int N, M,
post[NMAX], nr,
nrc;
bool viz[NMAX];
nod *G[NMAX], *GT[NMAX],
*CTC[NMAX];
ifstream f("ctc.in");
ofstream g("ctc.out");
void add(nod *&cap_lst, int nod_ad)
{
nod *p = new nod;
p->x = nod_ad;
p->next = cap_lst;
cap_lst = p;
}
void citiregraf()
{
int x, y;
f >> N >> M;
while(M--)
{
f >> x >> y;
add(G[x], y);
add(GT[y], x);
}
}
void DFS(int vf)
{
viz[vf] = 1;
for(nod *p = G[vf]; p != NULL; p = p->next)
if(!viz[p->x])
DFS(p->x);
post[++nr] = vf;
}
void DFSt(int vf)
{
viz[vf] = 0;
add(CTC[nrc], vf);
for(nod *p = GT[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])
DFS(i);
for(i = N; i >= 1; i--)
if(viz[post[i]])
{
nrc++;
DFSt(post[i]);
}
}
void afisare()
{
g << nrc << '\n';
for(int i = 1; i <= nrc; i++)
{
for(nod *p = CTC[i]; p != NULL; p = p->next)
g << p->x << ' ';
g << '\n';
}
}
int main()
{
citiregraf();
componente();
afisare();
f.close();
g.close();
return 0;
}