Pagini recente » Istoria paginii runda/fns_fanpage | Cod sursa (job #430640) | Cod sursa (job #1595791) | Cod sursa (job #2078485) | Cod sursa (job #1243742)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f ("ctc.in");
ofstream g ("ctc.out");
const int NMAX = 100000 + 1;
int n, m, ind, comp;
vector <int> graf[NMAX];
int index[NMAX], lowlink[NMAX];
bool inStack[NMAX];
stack <int> st;
vector < vector <int> > sol;
void citeste() {
int a, b;
f >> n >> m;
for (int i = 1; i <= m; i++) {
f >> a >> b;
graf[a].push_back(b);
}
}
void tarjan(int nod) {
int l, fiu, top;
index[nod] = lowlink[nod] = ++ind;
st.push(nod);inStack[nod] = true;
l = graf[nod].size();
for (int i = 0; i < l; i++) {
fiu = graf[nod][i];
if (index[fiu] == 0) tarjan(fiu);
if (inStack[fiu]) lowlink[nod] = min(lowlink[nod], lowlink[fiu]);
}
if (index[nod] <= lowlink[nod]) {
comp++;
vector <int> solcrt;
do {
top = st.top();
inStack[top] = false;
st.pop();
solcrt.push_back(top);
} while(top != nod);
sol.push_back(solcrt);
}
}
void scrie() {
int l;
g << comp << '\n';
for (int i = 0; i < comp; i++) {
l = sol[i].size();
for (int j = 0; j < l; j++) g << sol[i][j] << ' ';
g << '\n';
}
}
int main() {
citeste();
for (int i = 1; i <= n; i++)
if (!index[i]) tarjan(i);
scrie();
return 0;
}