Cod sursa(job #2796823)

Utilizator realmeabefirhuja petru realmeabefir Data 8 noiembrie 2021 20:46:04
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.61 kb
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

class graph
{
public:
    void bfs();
    ///////////////////////////////////////////////////////////
    void dfs();
    void _dfs_rec(int s, int viz[], vector<int> la[]);
    ///////////////////////////////////////////////////////////
    void ctc();
    void topo_dfs(int s, vector<int>& topo, int viz[], vector<int> la[]);
    void dfs_lat(int el, int viz[], vector<int> lat[], vector<vector<int>>& cc);
};

void graph::bfs()
{
    ifstream f("bfs.in");
    ofstream g("bfs.out");
    int n,m,s;
    vector<int>* la = new vector<int>[100005];
    int* dist = new int[100005];

    f >> n >> m >> s;
    memset(dist, -1, sizeof(int) * (n+1));
    for (int i = 1; i <= m; i++)
    {
        int x,y;
        f >> x >> y;
        la[x].push_back(y);
    }

    int curr_dist = 1;
    queue<int> q;
    q.push(s);
    dist[s] = 0;

    while (q.size())
    {
        for (auto& v: la[q.front()])
        {
            if (dist[v] == -1)
            {
                dist[v] = dist[q.front()] + 1;
                q.push(v);
            }
        }
        q.pop();
    }

    for (int i = 1; i <= n; i++)
        g << dist[i] << ' ';

    delete[] dist;
    delete[] la;
}

void graph::_dfs_rec(int s, int viz[], vector<int> la[])
{
    viz[s] = 1;
    for (auto& el: la[s])
    {
        if (viz[el] == 0)
            _dfs_rec(el, viz, la);
    }
}

void graph::dfs()
{
    ifstream f("dfs.in");
    ofstream g("dfs.out");
    int n,m;
    vector<int>* la = new vector<int>[100005];
    int* viz = new int[100005];

    f >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x,y;
        f >> x >> y;
        la[x].push_back(y);
        la[y].push_back(x);
    }

    int cc = 0;
    for (int i = 1; i <= n; i++)
    {
        if (viz[i] == 0)
        {
            _dfs_rec(i, viz, la);
            cc++;
        }
    }

    g << cc;

    delete[] viz;
    delete[] la;
}

void graph::topo_dfs(int s, vector<int>& topo, int viz[], vector<int> la[])
{
    viz[s] = 1;
    for (auto& el: la[s])
    {
        if (viz[el] == 0)
            topo_dfs(el, topo, viz, la);
    }
    topo.push_back(s);
}

void graph::dfs_lat(int s, int viz[], vector<int> lat[], vector<vector<int>>& cc)
{
    viz[s] = 1;
    cc[cc.size() - 1].push_back(s);

    for (auto& el: lat[s])
    {
        if (viz[el] == 0)
            dfs_lat(el, viz, lat, cc);
    }
}

void graph::ctc()
{
    ifstream f("ctc.in");
    ofstream g("ctc.out");
    int n,m;
    vector<int>* la = new vector<int>[100005];
    vector<int>* lat = new vector<int>[100005];
    int* viz = new int[100005];

    f >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x,y;
        f >> x >> y;
        la[x].push_back(y);
        lat[y].push_back(x);
    }

    vector<int> topo;
    for (int i = 1; i <= n; i++)
    {
        if (viz[i] == 0)
        {
            topo_dfs(i, topo, viz, la);
        }
    }
    // reverse topo
    reverse(topo.begin(), topo.end());
    // reset viz
    memset(viz, 0, sizeof(int) * (n+1));

    vector<vector<int>> cc;

    for (auto el: topo)
    {
        if (!viz[el])
        {
            cc.push_back(vector<int>());
            dfs_lat(el, viz, lat, cc);
        }
    }

    g << cc.size() << '\n';
    for (auto& row: cc)
    {
        for (auto& el: row)
            g << el << ' ';
        g << '\n';
    }

    delete[] la;
    delete[] lat;
    delete[] viz;
}

int main()
{
    graph g;
    g.ctc();

    return 0;
}