Pagini recente » Cod sursa (job #1236190) | Cod sursa (job #22720) | Cod sursa (job #2572757) | Cod sursa (job #620524) | Cod sursa (job #3238659)
#include <bits/stdc++.h>
std :: ifstream in ("ctc.in");
std :: ofstream out ("ctc.out");
const int NMAX = 1e5 + 5;
int n;
int m;
int x;
int y;
int cnt;
std :: vector <int> v[NMAX];
std :: vector <int> tout;
std :: bitset <NMAX> visited;
std :: vector <int> t[NMAX];
std :: vector <int> comp;
std :: stack <std :: vector <int>> s;
void dfs(int nod)
{
for(int i : v[nod])
{
if(visited[i] == false)
{
visited[i] = true;
dfs(i);
}
}
tout.push_back(nod);
}
void dfs1(int nod)
{
comp.push_back(nod);
for(int i : t[nod])
{
if(visited[i] == false)
{
visited[i] = true;
dfs1(i);
}
}
}
int main()
{
in >> n >> m;
for(int i = 1; i <= m; i ++)
{
in >> x >> y;
v[x].push_back(y);
t[y].push_back(x);
}
for(int i = 1; i <= n; i ++)
{
if(visited[i] == false)
{
visited[i] = true;
dfs(i);
}
}
std :: reverse(tout.begin(), tout.end());
visited &= 0;
for(auto i : tout)
{
if(visited[i] == false)
{
cnt ++;
visited[i] = true;
dfs1(i);
s.push(comp);
comp.clear();
}
}
out << cnt << '\n';
while(!s.empty())
{
for(int i : s.top())
{
out << i << " ";
}
out << '\n';
s.pop();
}
return 0;
}