Pagini recente » Cod sursa (job #2599615) | Cod sursa (job #2939159) | Cod sursa (job #3146134) | Cod sursa (job #2689140) | Cod sursa (job #3215915)
#include <bits/stdc++.h>
#define NMAX 100002
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, m, viz[NMAX], conex_components;
vector<int> G[NMAX];
vector<int> GT[NMAX];
vector<int> rez[NMAX];
stack<int> s;
void read_data()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
GT[y].push_back(x);
}
}
void dfs(int nod)
{
viz[nod] = 1;
for (auto vec : G[nod])
if (!viz[vec]) dfs(vec);
s.push(nod);
}
void dfs_t(int nod)
{
viz[nod] = 2;
rez[conex_components].push_back(nod);
for (auto vec : GT[nod])
if (viz[vec] == 1) dfs_t(vec);
}
int main()
{
read_data();
for (int i = 1; i <= n; i++)
if (!viz[i]) dfs(i);
while (!s.empty())
{
if (viz[s.top()] == 1)
{
conex_components++;
dfs_t(s.top());
}
s.pop();
}
fout << conex_components << "\n";
for (int i = 1; i <= conex_components; i++)
{
for (auto nod : rez[i])
fout << nod << " ";
fout << "\n";
}
return 0;
}