Cod sursa(job #2680589)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 3 decembrie 2020 19:10:01
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>
#include <vector>
#include <algorithm>
#define f in
#define g out

using namespace std;
ifstream in ( "plimbare.in" );
ofstream out( "plimbare.out" );
vector<int> L[1100], s, sol[1100];
int n, x, y, i, nivel, ctc, k, ok;
int mch[1100][1100], niv[1100], low[1100], fr[1100];

void dfs ( int nod ){
    fr[nod] = 1;
    niv[nod] = low[nod] = ++nivel;
    s.push_back(nod);
    for ( auto vecin : L[nod] )
        if ( !niv[vecin] ){
            dfs(vecin);
            low[nod] = min ( low[nod], low[vecin] );
        } else if ( fr[vecin] )
                    low[nod] = min ( low[nod], low[vecin] );
    if ( low[nod] == niv[nod] ){
        ctc++;
        x = 0;
        while ( x != nod ) {
            x = s.back();
            s.pop_back();
            fr[x] = 0;
            sol[ctc].push_back(x);
        }
    }
}

bool cmp ( int x, int y ){
    return mch[x][y];
}

int main() {
    
    f>>n;
    while (f>>x>>y) {
        L[x].push_back(y);
        mch[x][y] = 1;
    }
    for ( i=1; i <= n; i++ )
        if ( !niv[i] )
            dfs(i);
    
    int maxi = 0, poz;
    for ( i=1; i <= ctc; i++ )
        if ( maxi < sol[i].size() ){
            maxi = sol[i].size();
            poz = i;
        }
    sort(sol[poz].begin(), sol[poz].end(), cmp);
    g<<maxi<<"\n";
    for ( auto x : sol[poz] )
        g<<x<<" ";
    return 0;
}