Cod sursa(job #2233386)

Utilizator AlexandruRudiAlexandru Rudi AlexandruRudi Data 23 august 2018 09:34:43
Problema Componente tare conexe Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define ld long double
#define pld pair<ld,ld>
#define lg length()
#define sz size()
#define pb push_back
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005
#define x1 xdddddddddddddddddd
#define y1 ydddddddddddddddddd

int n,m,x,y,v[200005],k,w[200005];

vector <int> g[200005];

vector <vector<int>> ans;

stack <int> s;

void DFS(int nod){
    if(w[nod]) return;
    k++;
    w[nod]=v[nod]=k;
    s.push(nod);
    for(int i : g[nod]){
        DFS(i);
        if(v[i]>0) v[nod]=min(v[nod],v[i]);
    }
    if(v[nod]==w[nod]){
        int t; ans.pb({});
        do{
            t=s.top();
            s.pop();
            v[t]*=-1;
            ans[ans.sz-1].pb(t);
        }while(t!=nod);
    }
}

int32_t main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
    ifstream cin("ctc.in");
    ofstream cout("ctc.out");
    cin >> n >> m;
    for(int i=1;i<=m;i++){
        cin >> x >> y;
        g[x].pb(y);
    }
    for(int i=1;i<=n;i++){
        if(!v[i]) DFS(i);
    }
    cout << ans.sz << '\n';
    for(vector <int> i : ans){
        for(int j : i) cout << j << ' ';
        cout << '\n';
    }
}