Pagini recente » Cod sursa (job #1353305) | Cod sursa (job #2110265) | Cod sursa (job #2926940) | Cod sursa (job #1370232) | Cod sursa (job #3247076)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
const int nmax = 1005;
int n, m, fr[nmax], fr2[nmax], k;
vector<int> v[nmax], v2[nmax], ans[nmax];
stack<int> St;
void dfs(int nod)
{
fr[nod] = 1;
for(auto x : v[nod])
if(!fr[x])
dfs(x);
St.push(nod);
}
void dfs2(int nod)
{
ans[k].push_back(nod);
fr2[nod] = 1;
for(auto x : v2[nod])
if(!fr2[x])
dfs2(x);
}
int main()
{
f >> n >> m;
for(int i = 1; i <= m; i ++)
{
int x, y;
f >> x >> y;
v[x].push_back(y);
v2[y].push_back(x);
}
for(int i = 1; i <= n; i ++)
if(!fr[i])
dfs(i);
while(!St.empty())
{
if(!fr2[St.top()])
k ++, dfs2(St.top());
St.pop();
}
g << k << '\n';
for(int i = 1; i <= k; i ++, g << '\n')
for(auto x : ans[i])
g << x << " ";
return 0;
}