Cod sursa(job #2331566)

Utilizator StefanZamfirStefan Zamfir StefanZamfir Data 29 ianuarie 2019 18:24:19
Problema Componente tare conexe Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.38 kb

/*


      _             _    ___     ___    _  __       __
   __| |   __ _    / |  / _ \   / _ \  (_)/ /    _  \ \
  / _` |  / _` |   | | | | | | | | | |   / /    (_)  | |
 | (_| | | (_| |   | | | |_| | | |_| |  / /_     _   | |
  \__,_|  \__,_|   |_|  \___/   \___/  /_/(_)   (_)  | |
                                                    /_/


 */

//#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <tuple>
#include <fstream>
#include <iomanip>
#include <utility>
#include <list>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
#include <unordered_map>

#pragma warning "da 100% din tine. :)"
#define nl '\n'
#define cnl cout << '\n';
#define pb(x) push_back(x)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ll long long
#define ull unsigned ll
#ifdef INFOARENA
#define ProblemName "ctc"
#endif
#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "a.in"
#define OuFile "a.out"
#endif


//using namespace __gnu_pbds;
using namespace std;
ifstream cin(InFile);
ofstream cout(OuFile);


//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update> pref_trie;

template<class v, class type>
void print(v Vector, type nr) {
    for_each(all(Vector), [](type x) {
        cout << x << ' ';
    });
}

bitset<100001> vis;

struct vertex {
    list<int> adjlist;
    list<int> invadjlist;
};

vertex v[100001];

inline void add(const int target, const int to_add_index) {
    v[target].adjlist.push_back(to_add_index);
    v[to_add_index].invadjlist.push_back(target);
}

stack<int> finish;

inline void dfs(const int start) {
    vis[start] = true;
    for (auto it = v[start].adjlist.begin(); it != v[start].adjlist.end(); ++it)
        if (!vis[*it])
            dfs(*it);
    finish.push(start);
}

int scc = -1;
vector<vector<int> > ans;

inline void invdfs(const int start) {
    vis[start] = true;
    for (auto it = v[start].invadjlist.begin(); it != v[start].invadjlist.end(); ++it) {
        if (!vis[*it])
            invdfs(*it);
    }
    ans[scc].push_back(start);
}

inline void findscc() {

    while (!finish.empty()) {
        if (!vis[finish.top()]) {
            ++scc;
            vector<int> temp;
            ans.push_back(temp);
            invdfs(finish.top());
        }
        finish.pop();
    }

}

#define pi pair<int,int>
#define F first
#define S second

int main() {
    ios_base::sync_with_stdio(false);
//    clock_t tStart = clock();
    cin.tie(nullptr);
    int x, y, weight;
    int n, m, s;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        cin >> x >> y;
        add(x, y);
    }
    for (int i = 1; i <= n; ++i) {
        if (!vis[i])
            dfs(i);
    }
    for (int i = 0; i <= n; ++i) {
        vis[i] = false;
    }
    findscc();
    cout << scc + 1 << nl;
    for (int i = 0; i <= scc; ++i, cout << nl) {
        for (auto it = ans[i].begin(); it != ans[i].end(); ++it) {
            cout << *it << ' ';
        }
    }

    cout << nl;
//    printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}