Cod sursa(job #476592)

Utilizator coditzaDiana Kelerman coditza Data 11 august 2010 18:11:21
Problema Componente tare conexe Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 3.58 kb
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <deque>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <functional>
 
using namespace std;

template<typename T>
void printV(const vector<T> &v) {
    for (size_t i = 0; i < v.size(); ++i) {
        cout << v[i] << " ";
    }
    cout << endl;
}

class ctc {
private:
    typedef vector<int> VI;
    typedef vector<int>::iterator VIIt;

    vector<VI> graf;
    VI d;
    VI f;
    VI v;
    int t;

    void computeTimes() {
        VI(graf.size(), 0).swap(d);
        VI(graf.size(), 0).swap(f);
        VI(graf.size(), 0).swap(v);
        t = 0;

        for (size_t i = 0; i < graf.size(); ++i) {
            if (v[i] == 0) {
                computeTimes(i);
            }
        }
    }

    void computeTimes(size_t u) {
        v[u] = 1;

        ++t;
        d[u] = t;

        for (size_t i = 0; i < graf[u].size(); ++i) {
            int z = graf[u][i];
            if (v[z] == 0) {
                computeTimes(z);
            }
        }

        ++t;
        f[u] = t;
    }

    void swapGraf() {
        vector<VI> ngraf(graf.size(), VI());

        for (size_t i = 0; i < graf.size(); ++i) {
            for (size_t j = 0; j < graf[i].size(); ++j) {
                ngraf[graf[i][j]].push_back(i);
            }
        }

        graf.swap(ngraf);
    }

    class TimeComp : public binary_function<bool, int, int> {
        private:
        ctc *_c;
        public:
        TimeComp(ctc *c) : _c(c) {}
        bool operator()(const int &u, const int &v) const {
            if (_c->f[u] < _c->f[v]) return true;
            return false;
        }
    };

    void computeCC() {
        VI(graf.size(), 0).swap(v);

        TimeComp tc(this);

        for (size_t i = 0; i < graf.size(); ++i) {
           // cout << i << ": ";
           // printV<int>(graf[i]);

            sort(graf[i].rbegin(), graf[i].rend(), tc);
    //        printV<int>(graf[i]);
        }

        VI order(graf.size(), 0);
        for (size_t i = 0; i < graf.size(); ++i) {
            order[i] = i;
        }
        sort(order.rbegin(), order.rend(), tc);

        int cno = 1;
        for (size_t i = 0; i < order.size(); ++i) {
            if (v[order[i]] == 0) {
                computeCC(order[i], cno);
                ++cno;
            }
        }
    }

    void computeCC(size_t u, int cno) {
        v[u] = cno;

        for (size_t i = 0; i < graf[u].size(); ++i) {
            int z = graf[u][i];
            if (v[z] == 0) {
                computeCC(z, cno);
            }
        }
    }


public:
    void func_ctc(ifstream &in, ofstream &out) {

        int n, m;
        in >> n >> m;

        vector<VI>(n, VI()).swap(graf);

        for (int i = 0; i < m; ++i) {
            int x, y;
            in >> x >> y;
            graf[x - 1].push_back(y - 1);
        }

        computeTimes();

//        printV<int>(d);
  //      printV<int>(f);

        swapGraf();

        computeCC();

        VIIt max = max_element(v.begin(), v.end());
        out << *max << endl;

        for (size_t i = 0; i < v.size(); ++i) {
            if (v[i] != 0) {
                out << i + 1;
                for (size_t j = i + 1; j < v.size(); ++j) {
                    if (v[i] == v[j]) {
                        out << " " << j + 1;
                        v[j] = 0;
                    }
                }
                v[i] = 0;
                out << endl;
            }
        }
    }
};

int main() {
    ifstream in("ctc.in");
    ofstream out("ctc.out");

    ctc x;
    x.func_ctc(in, out);
}