Cod sursa(job #2284359)

Utilizator ShouldTryAdam Robert Mihai ShouldTry Data 17 noiembrie 2018 10:42:42
Problema Componente tare conexe Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n , a[10001][10001], ctc[105] , s[105], p[105], nrc;
void df1(int x)
{
	s[x] = 1;
	for(int i =1 ; i <= n ; i ++)
		if(s[i] == 0 && a[x][i] == 1)
			df1(i);
}
void df2(int x)
{
	p[x] = 1;
	for(int i =1 ; i <= n ; i ++)
		if(p[i] == 0 && a[i][x] == 1)
			df2(i);
}
int main()
{
    int i , j , m, ok=0;
    fin >> n >> m;
    while( m )
    {
    	fin >> i >> j;
    	a[i][j] = 1;
    	m --;
    }
	for(int i = 1 ; i <= n ; ++i)
		if(ctc[i] == 0)
		{
			for(int j = 1; j <= n ; ++j)
				s[j] = p[j] = 0;
			nrc ++;
			df1(i);
			df2(i);
			for(int j = 1; j <= n ; ++j)
				if(s[j] == 1 && p[j] == 1)
					ctc[j] = nrc;
		}
	fout << nrc ;
	int a=0;

	while (ok==0)
	{a++;
	 ok=1;
	 fout << endl;
    for (int i=1;i<=n;i++)
         if(ctc[i]==a){fout << i << " ";
                       ok=0;
                       }

     }
    return 0;
}