Cod sursa(job #2195499)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 16 aprilie 2018 15:55:28
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n,m,i,j,x,y,ntc;
bool viz1[100002];
bool viz2[100002];
vector <int> G[100002];
vector <int> sol[100002];
vector <int> A[100002];
vector <int> d;
void df1 (int nod)
{
  viz1[nod] = true;
  vector <int> :: iterator it;
  for (it = G[nod].begin(); it != G[nod].end(); it++)
  {
    if (!viz1[*it])
    {
      df1(*it);
    }
  }
  d.push_back(nod);
}
void df2 (int nod)
{
  viz2[nod] = true;
  vector <int> :: iterator it;
  for (it = A[nod].begin(); it != A[nod].end(); it++)
  {
    if (!viz2[*it])
    {
      df2(*it);
    }
  }
  sol[ntc].push_back(nod);
}
int main()
{
    fin >> n >> m;
    for (i = 1; i <= m; i++)
    {
      fin >> x >> y;
      G[x].push_back(y);
      A[y].push_back(x);
    }
    for (i = 1; i <= n; i++)
    {
      if (!viz1[i])
      {
        df1(i);
      }
    }
    reverse (d.begin(),d.end());
    for (i = 0; i < n; i++)
    {
      if (viz2[d[i]] == false)
      {
        ntc ++;
        df2(d[i]);
      }
    }
    fout << ntc << '\n';
    for (i = 1; i <= ntc; i++)
    {
      vector <int> :: iterator it;
      for (it = sol[i].begin(); it != sol[i].end(); it++)
        fout<< *it << " ";
      fout << '\n';
    }
    return 0;
}