Cod sursa(job #3351174)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 17 aprilie 2026 13:32:48
Problema Componente tare conexe Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int MAXN = 1e5 + 5;
vector < int > G[MAXN], GT[MAXN], St;
int nc = 0, N, M, x, y;
bool sel[MAXN];
ifstream f("ctc.in");
ofstream g("ctc.out");

void DF(int x){
  sel[x]=true;
  for( auto i:G[x])
    if (!sel[i]) DF(i);
  St.pb(x);
}

void DFT(int x, bool k){
  sel[x]=true;
  if (k) cout << x << ' ';
  for(auto i: GT[x])
   if (!sel[i])
       DFT(i, k);
}

int main(){

    f >> N >> M;
    for (int i = 1; i <= M; ++i){
      f >> x >> y;
      G[x].pb(y);GT[y].pb(x);
    }
    memset(sel, false,sizeof(sel));
    for(int i = 1; i <= N; i++)
        if (!sel[i]) DF(i);
    memset(sel, false,sizeof(sel));
    for(int i = St.size() - 1; i >= 0; --i)
        if (!sel[St[i]]) {
            DFT(St[i], false);
            nc++;
        }
    g << nc << '\n';
    memset(sel, false,sizeof(sel));
    for(int i = St.size() - 1; i >= 0; --i)
        if (!sel[St[i]]) {
            DFT(St[i], true);
            g << '\n';
        }
    return 0;
}