Pagini recente » Cod sursa (job #419923) | Cod sursa (job #76373) | Cod sursa (job #1271219) | Cod sursa (job #3145266) | Cod sursa (job #1529225)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
const int nmax = 100005;
vector <int> g[nmax], ctc[nmax];
stack <int> st;
int low[nmax], lev[nmax], nr;
bool viz[nmax];
void tarjan(int dad)
{
int i, son, nod;
low[dad]=lev[dad];
viz[dad]=true;
st.push(dad);
for(i=0; i<g[dad].size(); i++)
{
son=g[dad][i];
if(lev[son]==0)
{
lev[son]=lev[dad]+1;
tarjan(son);
low[dad]=min(low[dad], low[son]);
}
else if(viz[son]==true)
low[dad]=min(low[dad], low[son]);
}
if(low[dad]==lev[dad])
{
nr++;
do{
nod=st.top();
st.pop();
ctc[nr].push_back(nod);
viz[nod]=false;
}while(nod!=dad);
}
}
int main()
{
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
ios_base::sync_with_stdio(false);
int n, m, x, y, i, j;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> x >> y;
g[x].push_back(y);
}
for(i=1; i<=n; i++)
if(lev[i]==0)
{
lev[i]=1;
tarjan(i);
}
fout << nr << "\n";
for(i=1; i<=nr; i++)
{
for(j=0; j<ctc[i].size(); j++)
fout << ctc[i][j] << " ";
fout << "\n";
}
return 0;
}