Pagini recente » Cod sursa (job #1827672) | Cod sursa (job #264758) | Cod sursa (job #1670281) | Cod sursa (job #2141078) | Cod sursa (job #2808017)
#include <iostream>
#include <list>
#include <queue>
#include <vector>
#include <list>
#include <fstream>
const int nMax = 100005;
using namespace std;
class Graf {
private:
int m_n, m_m;
vector<int> m_listAd[nMax];
// DFS
bool m_dfsViz[nMax] = {};
// BFS
int m_bfsDist[nMax] = {};
queue<int> m_bfsQueue;
// CTC
int m_ctcId[nMax] = {}, m_ctcLow[nMax] = {}, m_ctcUltId = 0;
bool m_ctcPeStiva[nMax] = {};
list<list<int>> m_ctc;
stack<int> m_ctcStack;
// ---------------- Functii private ----------------
void orientatCtcDFS(int x) {
m_ctcStack.push(x);
m_ctcPeStiva[x] = true;
m_ctcId[x] = m_ctcLow[x] = ++m_ctcUltId;
for (auto y: m_listAd[x]) {
// Nu am explorat nodul pana acum (neavand vreun id)
if (m_ctcId[y] == 0) {
orientatCtcDFS(y);
}
// Am intalnit un nod care inca nu a fost atribuit unei componente conexe.
// Poate nodul curent face parte din viitoarea componenta conexa, a carei (posibila) sursa
// a fost gasita de y.
if (m_ctcPeStiva[y]) {
m_ctcLow[x] = min(m_ctcLow[x], m_ctcLow[y]);
}
}
// Am ajuns la nodul de start al ctc-ului explorat in prezent
if (m_ctcId[x] == m_ctcLow[x]) {
list<int> compCurr;
while (true) {
auto y = m_ctcStack.top();
m_ctcStack.pop();
m_ctcPeStiva[y] = false;
compCurr.push_back(y);
if (y == x) break;
}
m_ctc.push_back(compCurr);
}
}
public:
// ---------------- Interfata publica ----------------
explicit Graf(int n = 0, int m = 0) : m_n(n), m_m(m) {}
/*************** Algoritmi generali ***************/
void DFS(int k) {
m_dfsViz[k] = true;
for (auto x: m_listAd[k]) {
if (!m_dfsViz[x]) {
DFS(x);
}
}
}
const auto &BFS(int start) {
m_bfsQueue.push(start);
m_bfsDist[start] = 1;
while (!m_bfsQueue.empty()) {
int curr = m_bfsQueue.front();
m_bfsQueue.pop();
for (auto i: m_listAd[curr]) {
if (m_bfsDist[i] == 0) {
m_bfsDist[i] = m_bfsDist[curr] + 1;
m_bfsQueue.push(i);
}
}
}
return m_bfsDist;
}
/*************** Grafuri neorientate ***************/
void neorientatCitesteListaAdiacenta(ifstream &in) {
for (int i = 0; i < m_m; i++) {
int x, y;
in >> x >> y;
m_listAd[x].push_back(y);
m_listAd[y].push_back(x);
}
}
int neorientatNrCompConexe() {
int nrComp = 0;
for (int i = 1; i <= m_n; i++) {
if (!m_dfsViz[i]) {
nrComp++;
DFS(i);
}
}
return nrComp;
}
/*************** Grafuri orientate ***************/
void orientatCitesteListaAdiacenta(ifstream &in) {
for (int i = 0; i < m_m; i++) {
int x, y;
in >> x >> y;
m_listAd[x].push_back(y);
}
}
const auto &orientatCtc() {
// Algoritmul lui Tarjan
for (int i = 1; i <= m_n; i++) {
// Nu am explorat nodul pana acum (neavand vreun id)
if (m_ctcId[i] == 0) {
orientatCtcDFS(i);
}
}
return m_ctc;
}
};
int main() {
// Input rapid
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// I/O
ifstream in("ctc.in");
ofstream out("ctc.out");
int n, m;
in >> n >> m;
Graf g(n, m);
g.orientatCitesteListaAdiacenta(in);
in.close();
const auto &ctc = g.orientatCtc();
out << ctc.size() << "\n";
for (auto &ct: ctc) {
for (auto x: ct) {
out << x << " ";
}
out << "\n";
}
out.close();
return 0;
}