Cod sursa(job #2183386)

Utilizator flibiaVisanu Cristian flibia Data 23 martie 2018 09:29:35
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>
#define N 100100

using namespace std;

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

int n, m, x, y, vf, st[N], cnt;
vector <int> v[N], vv[N], sol[N];
bool viz[N];

void predfs(int from){
	viz[from] = 1;
	for(auto to : v[from])
		if(!viz[to])
			predfs(to);
	st[++vf] = from;
}

void dfs(int from){
	viz[from] = 0;
	sol[cnt].push_back(from);
	for(auto to : vv[from])
		if(viz[to])
			dfs(to);
}

int main(){
	in >> n >> m;
	while(m--){
		in >> x >> y;
		v[x].push_back(y);
		vv[y].push_back(x);
	}
	for(int i = 1; i <= n; i++)
		if(!viz[i])
			predfs(i);
	for(; vf; vf--)
		if(viz[st[vf]]){
			++cnt;
			dfs(st[vf]);
		}
	out << cnt;
	for(int i = 1; i <= cnt; i++){
		out << '\n';
		for(auto j : sol[i])
			out << j << ' ';
	}
	return 0;
}