Cod sursa(job #2981446)

Utilizator samyro14Samy Dragos samyro14 Data 17 februarie 2023 22:47:39
Problema Componente biconexe Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.95 kb
#include <bits/stdc++.h>
using namespace std;

#define ll unsigned long long
#define fast_read ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define INF 0x3f3f3f3f3f3f

const int maxn = 1e5;
const int maxm = 2e5;
const int mod = 666013;
ifstream fin("biconex.in");
ofstream fout("biconex.out");

int n, p, m, timp;
vector<int> a[maxn + 2];
int t_in[maxn + 2], t_min[maxn + 2];
stack<int> s;
vector<pair<int, int>> punte;
vector<vector<int>> cbc;
bool artpoint[maxn + 2];
void read(){
    fin >> n >> m;
    for(int i = 1; i <= m; ++i){
        int x, y; fin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }
}
void add_cbc(int x, int y, vector<int> & c){
    while(s.top() != y){
        c.push_back(s.top());
        s.pop();
    }
    c.push_back(y);
    c.push_back(x);
    s.pop();
}
void dfs(int x, int t){
    t_in[x] = t_min[x] = ++timp;
    int nrfii = 0;
    s.push(x);
    for(auto y : a[x]){
        if(y == t)
            continue;
        if(t_in[y] == 0){ // y este fiu in arborele DFS
            dfs(y, x);
            nrfii++;
            t_min[x] = min(t_min[x], t_min[y]);
            if(t_min[y] >= t_in[x]){
                vector<int> c;
                add_cbc(x, y, c);
                cbc.push_back(c);
                if(t_min[y] > t_in[x]){
                    punte.push_back({x, y});
                }
                if(t != 0)
                    artpoint[x] = 1;
            }
        }
        else{ // am muchie de intoarcere
            t_min[x] = min(t_min[x], t_in[y]);
        }
    }
    if(nrfii > 1 && t == 0)  artpoint[x] = 1;

}
int main(){
    read();
    for(int i = 1; i <= n; ++i)
        if(!t_in[i])
            dfs(i, 0);

        fout << cbc.size() << "\n";
        for(auto x : cbc){
            fout << x.size() << " ";
            for(auto y : x) fout << y << " ";
            fout << '\n';
        }
    return 0;
}
/*
task:
*/