Cod sursa(job #3285238)

Utilizator rakanDijkstra rakan Data 12 martie 2025 17:13:40
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <queue>
#include <fstream>
#include <stdio.h>
#include <string.h>
#include <iomanip>
#include <stack>
#include <climits>
#include <unordered_map>
#include <map>
#include <set>
using namespace std;

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

int n, m, v[100002], len, ctc;
vector<int> G[100002], Gt[100002], sol[100002];
bitset<100002> viz;

void DFS(int x)
{
	viz[x] = true;
	for (int w : G[x])
		if (!viz[w]) DFS(w);
	v[++len] = x;
}

void DFS_GT(int x)
{
	viz[x] = true;
	sol[ctc].push_back(x);
	for (int w : Gt[x])
		if (!viz[w]) DFS_GT(w);
}

void SortTop()
{
	for (int i = 1; i <= n; i++)
		if (!viz[i]) DFS(i);
}

int main()
{
	int i, j;
	fin >> n >> m;
	while (m--)
	{
		fin >> i >> j;
		G[i].push_back(j);
		Gt[j].push_back(i);
	}
	SortTop();
	viz.reset();
	for (i = n; i >= 1; i--)
	{
		int k = v[i];
		if (!viz[k])
		{
			ctc++;
			DFS_GT(k);
		}
	}
	int cnt = 0;
	fout << ctc;
	for (auto e : sol)
	{
		if (e.size() == 0) continue;
		fout << "\n";
		for (int w : e)
			fout << w << " ";
	}
	return 0;
}