Cod sursa(job #2667836)

Utilizator raresmateiuMateiu Rares-Ioan raresmateiu Data 3 noiembrie 2020 22:27:04
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>
#define NMAX 100009


using namespace std;

ifstream fin("biconex.in");
ofstream fout("biconex.out");

vector <int> g[NMAX], sol[NMAX];
stack <pair <int, int>> st;
int n, m, nr;
int level[NMAX], low[NMAX];
bool use[NMAX];

void DFS(int node, int father)
{
    use[node] = 1;
    level[node] = low[node] = level[father] + 1;
    for (auto other : g[node])
    {
        if (other == father)
            continue;
        if (!use[other])
        {
            st.push({node, other});
            DFS(other, node);
            if (low[other] >= level[node])
            {
                bool ok = 1;
                ++nr;
                while (ok)
                {
                    int node1 = st.top().first, node2 = st.top().second;
                    st.pop();
                    if (node1 == node && node2 == other)
                        ok = 0;
                    sol[nr].push_back(node2);
                }
                sol[nr].push_back(node);
            }
            low[node] = min(low[node], low[other]);
        }
        else
            low[node] = min(low[node], level[other]);
    }
}

int main()
{
    int x, y;
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    DFS(1, 0);
    fout << nr << '\n';
    for (int i = 1; i <= nr; i++)
    {
        for (auto j : sol[i])
            fout << j << ' ';
        fout << '\n';
    }
return 0;}