Pagini recente » Cod sursa (job #2596416) | Cod sursa (job #2329478) | Cod sursa (job #2067682) | Cod sursa (job #345207) | Cod sursa (job #3230222)
#include <bits/stdc++.h>
using namespace std;
#define NMAX 100000
vector<int> graph[NMAX + 2];
int discoveryTime[NMAX + 2];
int lowTime[NMAX + 2];
int nodeStack[NMAX + 2];
int stackTop;
int comp_cnt;
int timer;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
void rst(int n)
{
for (int i = 1; i <= n; ++i) {
lowTime[i] = discoveryTime[i] = 0;
}
timer = 0;
stackTop = 0;
}
void dfs(int node, int parent, bool shouldPrint) {
discoveryTime[node] = lowTime[node] = ++timer;
nodeStack[++stackTop] = node;
bool art_point = false;
for (int adjNode : graph[node]) {
if (adjNode != parent) {
if (!discoveryTime[adjNode]) {
dfs(adjNode, node, shouldPrint);
lowTime[node] = min(lowTime[node], lowTime[adjNode]);
if (lowTime[adjNode] >= discoveryTime[node]) {
art_point = true;
comp_cnt++;
if (shouldPrint) {
fout << "Component " << comp_cnt << ": ";
while (stackTop > 0 && nodeStack[stackTop] != adjNode) {
fout << nodeStack[stackTop] << " ";
stackTop--;
}
fout << adjNode << " " << node << "\n";
stackTop--;
}
}
} else {
lowTime[node] = min(lowTime[node], discoveryTime[adjNode]);
}
}
}
if (!parent && art_point) {
fout << "Root component: ";
while (stackTop > 0) {
fout << nodeStack[stackTop] << " ";
--stackTop;
}
fout << "\n";
}
}
int main() {
int n, m;
fin >> n >> m;
for (int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs(1, 0, false);
fout << "Number of components: " << comp_cnt << "\n";
rst(n);
comp_cnt = 0;
dfs(1, 0, true);
fin.close();
fout.close();
return 0;
}