Cod sursa(job #2476333)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 18 octombrie 2019 17:46:47
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

ifstream in("ctc.in");
ofstream out("ctc.out");

int n, m, nr;
bool viz[100001], viz2[100001];
vector <int> g[100001], g2[100001], sol[100001];
stack <int> s;

inline void DFS(int nod)
{
	viz[nod] = 1;
	for (int i = 0; i < g[nod].size(); ++i)
	{
		if (!viz[g[nod][i]])
			DFS(g[nod][i]);
	}
	s.push(nod);
}

inline void DFS2(int nod)
{
	viz2[nod] = 1;
	sol[nr].push_back(nod);
	for (int i = 0; i < g2[nod].size(); ++i)
	{
		if (!viz2[g2[nod][i]])
			DFS2(g2[nod][i]);
	}
}

inline void citire()
{
	int x, y;
	in >> n >> m;
	for (int i = 1; i <= m; ++i)
	{
		in >> x >> y;
		g[x].push_back(y);
		g2[y].push_back(x);
	}
}

int main()
{
	citire();
	for (int i = 1; i <= n; ++i)
	{
		if (!viz[i])
			DFS(i);
	}
	while (!s.empty())
	{
		if (!viz2[s.top()])
		{
			++nr;
			DFS2(s.top());
		}
		s.pop();
	}
	out << nr << '\n';
	for (int i = 1; i <= nr; ++i)
	{
		for (int j = 0; j < sol[i].size(); ++j)
			out << sol[i][j] << " ";
		out << '\n';
	}
	return 0;
}