Cod sursa(job #2836211)

Utilizator ciobyCiobanu Vlasie cioby Data 19 ianuarie 2022 22:16:25
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");

int n, m, x, y;

typedef struct graf
{
	int x;
	graf* a;
}*L;

L a[100005];
L at[100005];
int f[100005];
int f2[100005];
int stiva[100005];
int vf = 0;
int ans = 0;

vector< int >rez[100005];

void add(L& DEST, int val)
{
	L p = new graf;
	p->x = val;
	p->a = DEST;
	DEST = p;
}

void citire()
{
	fin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		fin >> x >> y; 
		add(a[x], y);
		add(at[y], x);
	}
}

void DFS1(int nod)
{
	f[nod] = 1;
	for (L p = a[nod]; p != NULL; p = p->a)
	{
		if (!f[p->x])
			DFS1(p->x);
	}
	stiva[++vf] = nod;
}

void DFS_STRONG(int nod,int index)
{
	f2[nod] = 1;
	rez[index].push_back(nod);
	for (L p = at[nod]; p != NULL; p = p->a)
	{
		if (!f2[p->x])
			DFS_STRONG(p->x,index);
	}
}

void rezolvare()
{
	for (int i = 1; i <= n; i++) {
		if (!f[i]) DFS1(i);
	}
	while (vf)
	{
		int k = stiva[vf--];
		if (!f2[k])
		{
			ans++;
			DFS_STRONG(k,ans);
		}
	}
	fout << ans << '\n';
	for (int i = 1; i <= ans; i++)
	{
		for (int j = 0; j < rez[i].size(); j++)
			fout << rez[i][j] << ' ';
		fout << '\n';
	}
}



int main()
{
	citire();
	rezolvare();
}