Cod sursa(job #2888970)

Utilizator MariusANDMarius-Ionut Andreiasi MariusAND Data 12 aprilie 2022 00:20:14
Problema Componente tare conexe Scor 60
Compilator cpp-32 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

const int N=1e5;
vector <int> graf[N+1],graf_t[N+1];
vector <int> ctc[N+1];
vector <int> st;
bitset <N+1> viz;
int n,m,nc;


void DFS(int x)
{
	viz[x]=1;
	for(auto y: graf[x])
	{
		if(!viz[y])
		{
		   	DFS(y);
		}
	}
	st.push_back(x);
}


void DFS_C(int x)
{
	ctc[nc].push_back(x);
	viz[x]=1;
	for(auto y:graf_t[x])
	{
		if(!viz[y])	
		{
			DFS_C(y);
		}
	}
}


int main()
{
	ifstream f("ctc.in");
	ofstream g("ctc.out");
	f>>n>>m;		
	for(int i=1; i<=m; i++)
	{
		int x,y;
		f>>x>>y;
		graf[x].push_back(y);
		graf_t[y].push_back(x);
	}
	f.close();
	for(int i=1; i<=n; i++)
		if(!viz[i])	
		{
			DFS(i);
		}
	viz.reset();
	for(int i=(int)st.size()-1; i>=0; i--)
		if(!viz[st[i]])  
		{
			nc++;
			DFS_C(st[i]);
		}
	g<<nc<<endl;
	for(int i=1; i<=nc; i++)
	{
		for(auto x: ctc[i])
		{
			g<<x<<" ";
		}
		g<<endl;
	}
	g.close();
	return 0;
}