# Cod sursa(job #284062)

Utilizator Data 20 martie 2009 22:23:59 Componente biconexe 100 cpp done Arhiva educationala 2.06 kb
``````#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

#define FIN "biconex.in"
#define FOUT "biconex.out"
#define MAX_N 100015
#define MAX_M 200015
#define PB push_back
#define MP make_pair

typedef pair<int, int> PII;

int N, M, S, C;
vector<int> G[MAX_N];
PII stack[MAX_M];
bool viz[MAX_N];
int lvl[MAX_N], mlvl[MAX_N], tata[MAX_N];
vector<int> comp[MAX_N];
int v[MAX_N];

scanf("%d %d", &N, &M);
for (int i = 1; i <= M; ++i) {
int a, b;
scanf("%d %d", &a, &b);
G[a].PB(b);
G[b].PB(a);
}
}

void DFS(int nod) {
viz[nod] = true;
mlvl[nod] = lvl[nod];

for (vector<int>::iterator it = G[nod].begin(); it != G[nod].end(); ++it)
if (*it != tata[nod])
if (!viz[*it]) {
lvl[*it] = lvl[nod] + 1;
stack[++S] = MP(nod, *it);
tata[*it] = nod;
DFS(*it);
if (mlvl[*it] >= lvl[nod]) {
++C;
do {
if (v[stack[S].first] != C) {
comp[C].PB(stack[S].first);
v[stack[S].first] = C;
}
if (v[stack[S].second] != C) {
comp[C].PB(stack[S].second);
v[stack[S].second] = C;
}
} while (stack[S--] != MP(nod, *it));
}
mlvl[nod] = min(mlvl[nod], mlvl[*it]);

} else {
mlvl[nod] = min(mlvl[nod], lvl[*it]);
}
}

void solve() {
for (int i = 1; i <= N; ++i)
if (!viz[i])
DFS(i);
printf("%d\n", C);
for (int i = 1; i <= C; ++i) {
for (int j = 0; j < (int)comp[i].size(); ++j)
printf("%d ", comp[i][j]);
printf("\n");
}
}

int main() {
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);