Pagini recente » Cod sursa (job #1534665) | Cod sursa (job #912559) | Rating Pojar Mihai (Pojar_Mihai_Corneliu_322CB) | Cod sursa (job #1949015) | Cod sursa (job #2662987)
#include <fstream>
#include <vector>
#include <stack>
#include <iostream>
#define UNDEFINED -1
const int NMAX = 1e5 + 1;
std :: ifstream f("ctc.in");
std :: ofstream g("ctc.out");
std :: vector <int> graf[NMAX];
std :: vector <std :: vector <int> > sol;
int n, m;
void tarjanAlg();
void DFS(int i, std :: vector <int> & disc, std :: vector <int>& low, std :: stack <int> & stk
, std :: vector <bool> & inStack);
int main() {
f >> n >> m;
for (int i = 0; i < m; i++ ){
int a, b;
f >> a >> b;
graf[a - 1].push_back(b - 1);
}
tarjanAlg();
g << sol.size() << "\n";
for (auto linie : sol ) {
for (auto elem : linie) {
g << elem << " ";
}
g << "\n";
}
return 0;
}
void tarjanAlg(){
std :: vector <int> disc(n + 1,-1); // discovery_time
std :: vector <int> low(n + 1, -1);
std :: vector <bool> inStack(n,false);
std :: stack <int> stk;
for (int i = 0; i < n; i++)
if (disc[i] == UNDEFINED)
DFS(i,disc,low,stk,inStack);
}
void DFS(int u, std :: vector <int> & disc, std :: vector <int>& low, std :: stack <int> & stk
, std :: vector <bool> & inStack){
static int time = 0;
disc[u] = low[u] = time;
time += 1;
stk.push(u);
inStack[u] = true;
for (int v : graf[u]){
if (disc[v] == UNDEFINED){
DFS(v, disc, low, stk, inStack );
low[u] = std :: min (low[u], low[v]);
}
else
if (inStack[u])
low[u] = std :: min (low[u], disc[v]);
}
std :: vector <int> aux;
if (disc[u] == low[u]){
while (stk.top() != u){
aux.push_back(stk.top()+1);
//g << stk.top() + 1 << " ";
inStack[stk.top()] = false;
stk.pop();
}
aux.push_back(stk.top()+1);
//g << stk.top() + 1 << "\n";
sol.push_back(aux);
inStack[u] = false;
stk.pop();
}
}