Pagini recente » Cod sursa (job #831212) | Cod sursa (job #1498894) | Cod sursa (job #1701759) | Cod sursa (job #2189070) | Cod sursa (job #3198807)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector<vector<int>> G, GT;
int n, m, contor;
vector<bool> V;
vector<int> S;
vector<vector<int>> CTC;
void read()
{
fin >> n >> m;
G = GT = vector<vector<int>>(n + 1);
for (int i = 1; i <= m; i++)
{
int a, b;
fin >> a >> b;
G[a].push_back(b);
GT[b].push_back(a);
}
}
void dfs(int k)
{
V[k] = true;
for (auto x : G[k])
if (!V[x])
dfs(x);
S.push_back(k);
}
void dfsGT(int k, vector<int> &componenta)
{
V[k] = true;
componenta.push_back(k);
for (auto x : GT[k])
if (!V[x])
dfsGT(x, componenta);
}
int main()
{
read();
V = vector<bool>(n + 1, false);
for (int i = 1; i <= n; i++)
if (!V[i])
dfs(i);
V = vector<bool>(n + 1, false);
for (vector<int>::reverse_iterator it = S.rbegin(); it != S.rend(); it++)
if (!V[*it])
{
contor++;
vector<int> componenta;
dfsGT(*it, componenta);
CTC.push_back(componenta);
}
fout << contor << "\n";
for (const auto &componenta : CTC)
{
for (const auto &nod : componenta)
fout << nod << " ";
fout << "\n";
}
return 0;
}