Cod sursa(job #2195714)

Utilizator flibiaVisanu Cristian flibia Data 17 aprilie 2018 11:03:02
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;

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

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

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(; cnt; cnt--){
		out << '\n';
		for(auto i : sol[cnt])
			out << i << ' ';
	}
	return 0;
}