Pagini recente » Cod sursa (job #529830) | Cod sursa (job #192481) | Cod sursa (job #1617629) | Cod sursa (job #2561030) | Cod sursa (job #3227980)
#include <iostream>
#include <vector>
#include <stack>
#include <fstream>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
#define Nmax =10005;
stack <int> s;
vector <int> Mad1[100001],Mad2[100001],cc[100001];
int n,e,nr,a,b,beenthere[100001],p;
void dfs(int nodS)
{int j;
beenthere[nodS]=1;
for(int i=0;i<Mad1[nodS].size();++i)
{
j=Mad1[nodS][i];
if(!beenthere[j])
dfs[j];
}
s.push(nodS);
}
void dfst(int nodS)
{int j;
cc[nr].push_back(nodS);
beenthere[nodS]=2;
for(int i=0;i<Mad2[nodS].size();++i)
{
j=Mad2[nodS][i];
if(beenthere[j]==1)
dfst[i];
}
}
int main()
{f>>n>>e;
for(int i=1;i<=e;++i)
{
f>>a>>b;
Mad1[a].push_back(b);
Mad2[b].push_back(a);
}
for(int i=1;i<=n;++i)
{
if(!beenthere[i])
dfs[i];
}
while(!s.empty())
{
p=s.top();
if(beenthere[p]==1)
{
nr++;
dfst[p];
}
s.pop();
}
g<<nr<<endl;
for(int i=1;i<=nr;++i)
{
for(int j=0;j<cc[i].size();++j)
g<<cc[i][j]<<" ";
g<<endl;
}
return 0;
}