Cod sursa(job #2796852)

Utilizator realmeabefirhuja petru realmeabefir Data 8 noiembrie 2021 21:28:45
Problema Componente biconexe Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 9.03 kb
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

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 sortaret();
    ///////////////////////////////////////////////////////////
    void mcritice();
    void dfs_critice(int s, int p, int viz[], vector<int> la[], int lowLink[], int ids[], int& id, vector<pair<int,int>>& critice);
    ///////////////////////////////////////////////////////////
    void biconex();
    void dfs_biconexe(int s, int viz[], vector<int> la[], unordered_map<pair<int,int>, int, hash_pair>& map_critice, vector<vector<int>>& biconexe);
};

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;
}

void graph::sortaret()
{
    ifstream f("sortaret.in");
    ofstream g("sortaret.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);
    }

    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());

    for (auto& el: topo)
        g << el << ' ';

    delete[] la;
    delete[] viz;
}

void graph::dfs_critice(int s, int p, int viz[],vector<int> la[], int lowLink[], int ids[], int& id, vector<pair<int,int>>& critice)
{
    lowLink[s] = id;
    ids[s] = id;
    id ++;
    viz[s] = 1;

    for (auto& el: la[s])
    {
        if (!viz[el])
            dfs_critice(el, s, viz, la, lowLink, ids, id, critice);
        if (el != p)
            lowLink[s] = min(lowLink[s], lowLink[el]);
    }

    if (lowLink[s] == ids[s] && p != -1)
    {
        critice.push_back({s, p});
    }
}

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

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

    vector<pair<int,int>> critice;
    // in problema e graf conex, but anyway
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
            dfs_critice(i, -1, viz, la, lowLink, ids, id, critice);
    }

    for (auto& per: critice)
        g << per.first << ' ' << per.second << '\n';

    delete[] viz;
    delete[] la;
    delete[] lowLink;
    delete[] ids;
}


void graph::dfs_biconexe(int s, int viz[], vector<int> la[], unordered_map<pair<int,int>, int, hash_pair>& map_critice, vector<vector<int>>& biconexe)
{
    viz[s] = 1;
    biconexe[biconexe.size() - 1].push_back(s);
    for (auto& el: la[s])
    {
        // daca e in map_critice nu e bn
        if (map_critice.find({s, el}) != map_critice.end() || map_critice.find({el ,s}) != map_critice.end())
            continue;
        if (!viz[el])
            dfs_biconexe(el, viz, la, map_critice, biconexe);
    }
}

void graph::biconex()
{
    // mai intai caut muchile critice
    // fiecare muchie critica va fi o componente biconexa
    // elimin muhcile din lista de adiacenta si fac restul de componente conexe, care vor fi si ele biconexe (in afara de cele cu un singur nod)
    ifstream f("biconex.in");
    ofstream g("biconex.out");
    int n,m;
    vector<int>* la = new vector<int>[100005];
    int* viz = new int[100005];
    int* lowLink = new int[100005];
    int* ids = new int[100005];
    int id = 0;

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

    vector<pair<int,int>> critice;
    // in problema e graf conex, but anyway
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
            dfs_critice(i, -1, viz, la, lowLink, ids, id, critice);
    }

    // inserez in biconexe toate muchile critice
    vector<vector<int>> biconexe;
    for (auto& per: critice)
    {
        biconexe.push_back(vector<int>());
        biconexe[biconexe.size()-1].push_back(per.first);
        biconexe[biconexe.size()-1].push_back(per.second);
    }

    // pastrez aici muchile care sunt critice pt a nu le folosi in dfs final
    unordered_map<pair<int,int>, int, hash_pair> map_critice;
    for (auto& per: critice)
    {
        map_critice[{per.first, per.second}] = 1;
        map_critice[{per.second, per.first}] = 1;
    }

    int nrBic = critice.size();
    memset(viz, 0, sizeof(int) * (n+1));
    for (int i = 1; i <= n; i++)
    {
        if (!viz[i])
        {
            biconexe.push_back(vector<int>());
            dfs_biconexe(i, viz, la, map_critice, biconexe);
            if (biconexe[biconexe.size()-1].size() >= 2)
                nrBic ++;
        }
    }

    g << nrBic << '\n';
    for (auto& row: biconexe)
    {
        if (row.size() < 2)
            continue;
        for (auto& el: row)
            g << el << ' ';
        g << '\n';
    }

    delete[] viz;
    delete[] la;
    delete[] lowLink;
    delete[] ids;
}

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

    return 0;
}