Cod sursa(job #2533774)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 29 ianuarie 2020 18:14:02
Problema Componente biconexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("pctart.in");
ofstream g("pctart.out");

constexpr int NMAX = 100003;

stack <pair<int, int>> nodes;
vector<int> G[NMAX];
bool sel[NMAX], C[NMAX];
int nivel[NMAX], T[NMAX], Low[NMAX], rad, nr, N, M, x, y;
stringstream buffer;

void afis(int u, int v) {

   while(1)
   {
       int x = nodes.top().first;
       int y = nodes.top().second;

       nodes.pop();

       buffer << x << ' ';
       buffer << y << ' ';

       if(x == u || y == v)
        break;
   }

   return;
}

void df(int nod) {
    sel[nod] = true; Low[nod] = nivel[nod];
    for(auto fiu : G[nod])
    if(!sel[fiu]) {
        nodes.push({nod, fiu});
        T[fiu] = nod;
        nivel[fiu] = nivel[nod] + 1;
        if(nod == rad) nr++;
        df(fiu);
        if(Low[fiu] < Low[nod]) Low[nod] = Low[fiu];
        if(Low[fiu] >= nivel[nod] && nod != rad)
            C[nod] = true, afis(nod, fiu);
    } else if(T[nod] != fiu && Low[nod] > nivel[fiu]) {
        Low[nod] = nivel[fiu];
    }
}
int main()
{
    f >> N >> M;
    for(int i = 1; i <= M; i++)
    {
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    for(int i = 1; i <= N; i++)
        if(!sel[i])
        {
            nivel[i] = 1;
            nr = 0;
            rad = i;
            df(i);
            if(nr > 1) C[i] = true;
        }
    g << buffer.str() << "\n";
    return 0;
}