Pagini recente » Cod sursa (job #744399) | Cod sursa (job #2601283) | Cod sursa (job #3183483) | Cod sursa (job #1575775) | Cod sursa (job #2651683)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
vector<int> disc;
vector<int> low;
stack<pair<int, int>> s;
int comp = 0;
void dfs(int u,vector<int>* g, vector<pair<int,int>> *res,int p) {
static int time = 0;
int children = 0;
disc[u] = low[u] = ++time;
for (int v : g[u])
if (disc[v] == -1) {
++children;
s.push({ u,v });
dfs(v, g, res, u);
low[u] = min(low[u], low[v]);
if (p==-1 && children>=2 || p != -1 && low[v] >= disc[u]) {
++comp;
pair<int, int> e;
do{
e = s.top();
s.pop();
res[comp].push_back(e);
} while (e.first != u || e.second != v);
}
}
else if (v != p) {
low[u] = min(low[u], disc[v]);
if (disc[v] < disc[u])
s.push({ u,v });
}
if (p == -1 && !s.empty()) {
++comp;
while (!s.empty()) {
res[comp].push_back(s.top());
s.pop();
}
}
}
void biconnectedComponents(vector<int>* g,int n, vector<pair<int,int>> *res) {
for (int i = 1;i <= n;++i)
if (disc[i] == -1) {
dfs(1, g, res, -1);
}
}
int main()
{
ifstream fin("biconvex.in");
ofstream fout("biconex.out");
vector<int> *g;
int n, m;
fin >> n >> m;
g = new vector<int>[n + 1];
disc.resize(n + 1, -1);
low.resize(n + 1);
while (m--) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
vector<pair<int, int>> res[100];
biconnectedComponents(g, n, res);
fout << comp << '\n';
for (int i = 1;i <= comp;++i) {
vector<bool> v(n + 1, false);
for (auto e : res[i]) {
if (v[e.first] == false) {
fout << e.first << ' ';
v[e.first] = true;
}
if (v[e.second] == false) {
fout << e.second << ' ';
v[e.second] = true;
}
}
fout << '\n';
}
return 0;
}