Cod sursa(job #1436113)

Utilizator irimiecIrimie Catalin irimiec Data 15 mai 2015 09:24:49
Problema Componente tare conexe Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.85 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#ifndef ONLINE_JUDGE
FILE *fin = freopen("ctc.in", "r", stdin);
FILE *fout = freopen("ctc.out", "w", stdout);
#endif

const int MAXN = 100;

int n, m, cnt;
vector<int> v[MAXN], vp[MAXN], vm[MAXN];
vector<int> discovered, used, where;

void read() {
	int x, y;

	scanf("%d%d", &n, &m);
    for(int i = 0; i < m; ++i) {
        scanf("%d%d", &x, &y);
        vp[x].pub(y);
        vm[y].pub(x);
    }

}

void dfp(int nod) {
    used[nod] = 1;
    for(auto &it: vp[nod])
        if(!used[it])
            dfp(it);
    discovered.pub(nod);
}

void dfm(int nod, int which) {
    where[nod] = which;
    for(auto &it: vm[nod])
        if(where[it] == -1)
            dfm(it, which);
}

int main() {
	read();

    used.resize(n + 1, 0);
    for(int i = 1; i <= n; ++i)
        if(!used[i])
            dfp(i);

    where.resize(n + 1, -1);
    cnt = 0;
    for(; !discovered.empty(); discovered.pop_back()) {
        if(where[discovered.back()] == -1)
            dfm(discovered.back(), cnt), cnt++;
    }

    for(int i = 1; i <= n; ++i)
        v[where[i]].pub(i);

    printf("%d\n", cnt);
    for(int i = 0; i < n; ++i) {
        for(auto &it: v[i])
            printf("%d ", it);
        printf("\n");
    }
    return 0;
}