Cod sursa(job #2576266)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 6 martie 2020 18:11:44
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.72 kb
#include <bits/stdc++.h>
#define FILE_NAME "biconex"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 100000 + 100
using namespace std;

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> llp;
typedef pair<ld,ld> pct;

const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;


void add(ll &a , ll b)
{
    a += b;
    a %= mod;
}

void sub(ll &a, ll b)
{
    a = (a - b + mod) % mod;
}

int n, m, cate, niv[NMAX], low[NMAX];
vector <int> G[NMAX], sol[NMAX];
bitset <NMAX> viz;
deque <int> Q;
void DFS(int nod, int dad)
{
    viz[nod] = 1;
    Q.push_back(nod);
    low[nod] = niv[nod];
    for(auto it : G[nod])
    {
        if(it == dad) continue;

        if(viz[it])
        {
            low[nod] = min(low[nod], niv[it]);
            continue;
        }

        niv[it] = niv[nod] + 1;
        DFS(it, nod);
        low[nod] = min(low[nod], low[it]);
        if(low[it] >= niv[nod])
        {
            cate++;
            int u;
            do
            {
                u = Q.back();
                Q.pop_back();
                sol[cate].push_back(u);
            }while(!Q.empty() && u != it);
            sol[cate].push_back(nod);
        }
    }
}
int main()
{
    f >> n >> m;
    for(int x,y, i = 1; i <= m; ++i)
    {
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFS(1,0);
    g << cate << '\n';
    for(int i = 1; i <= cate; ++i, g << '\n')
        for(auto it : sol[i])
            g << it << ' ';
    f.close();
    g.close();
    return 0;
}