Pagini recente » Cod sursa (job #451615) | Cod sursa (job #2888947)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream f("in.txt");
ofstream g("out.txt");
const int N=100001;
vector <int> succesori[N+1],predecesori[N+1];
vector <int> ctc[N+1];
vector <int> stiva;
bitset <N+1> viz;
int n,m,nc;
void DFS(int x)
{
viz[x]=1;
for(auto y: succesori[x])
{
if(viz[y]==0)
DFS(y);
}
stiva.push_back(x);
}
void DFS_Tare(int x)
{
ctc[nc].push_back(x);
viz[x]=1;
for(auto y:predecesori[x])
if(viz[y]==0)
DFS_Tare(y);
}
int main()
{
int x,y;
f>>n>>m;
//se construiesc graful si graful transpus
for(int i=1; i<=m; i++)
{
f>>x>>y;
succesori[x].push_back(y);
predecesori[y].push_back(x);
}
for(int i=1; i<=n; i++)
if(viz[i]==0) //
DFS(i);
viz.reset();
for(int i=(int)stiva.size()-1; i>=0; i--)
if(viz[stiva[i]]==0) //
{
nc++;
DFS_Tare(stiva[i]);
}
g<<nc<<endl;
//se afiseaza comp conexe
for(int i=1; i<=nc; i++)
{
for(auto x: ctc[i])
g<<x<<" ";
g<<endl;
}
f.close();
g.close();
return 0;
}