Pagini recente » Cod sursa (job #2396714) | Clasament pebarosaneala | Cod sursa (job #2074306) | Cod sursa (job #2207583) | Cod sursa (job #2863751)
#include <bits/stdc++.h>
#define cal 50005
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, m, x, y, cnt;
vector <int> g[cal], gt[cal];
set <int> r[cal];
vector <bool> v;
vector <int> s;
void dfs(int x)
{
v[x]=true;
for(auto h : g[x])
{
if(v[h]==0)
{
dfs(h);
}
}
s.push_back(x);
}
void dfst(int x)
{
v[x]=1;
r[cnt].insert(x);
for(auto h : gt[x])
{
if(!v[h])
{
dfst(h);
}
}
}
int main()
{
fin >> n >> m;
for(int i=1; i<=m; i++)
{
fin >> x >> y;
g[x].push_back(y);
gt[y].push_back(x);
}
v=vector <bool> (n+1, false);
for(int i=1; i<=n; i++)
{
if(v[i]==0)
{
dfs(i);
}
}
v=vector <bool> (n+1, false);
reverse(s.begin(), s.end());
for(auto h : s)
{
if(v[h]==0)
{
cnt++;
dfst(h);
}
}
fout << cnt << "\n";
for(int i=1; i<=cnt; i++)
{
for(auto h : r[i])
{
fout << h << " ";
}
fout << "\n";
}
return 0;
}