Pagini recente » Cod sursa (job #2848559) | Cod sursa (job #1498498) | Cod sursa (job #1216591) | Cod sursa (job #2527019) | Cod sursa (job #2045815)
#include <bits/stdc++.h>
using namespace std;
vector <int> ctc[100001];
vector <int> graf[100001];
vector <int> igraf[100001];
vector <int> v;
void dfs1 (int node)
{
viz[node] = 1;
for (auto x:graf[node])
if (viz[x] == 0)
dfs1(x);
v.push_back(node);
}
void dfs2 (int node, int where)
{
ctc[where].push_back(node);
viz[node] = 0;
for (auto x:igraf[node])
if (viz[x])
dfs2(x, where);
}
int main(int argc, char const *argv[])
{
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, m;
fin >> n >> m;
for (int i = 1; i<=m; ++i)
{
int x, y;
fin >> x >> y;
graf[x].push_back(y);
igraf[y].push_back(x);
}
for (int i = 1; i<=n; ++i)
if (viz[i] == 0)
dfs1(i);
int cate = 0;
reverse(v.begin(), v.end());
for (auto x:v)
if (viz[x])
{
++cate;
dfs2(x, cate);
}
cout << cate << '\n';
for (int i = 1; i<=cate; ++i)
{
for (auto x:ctc[i])
cout << x << ' ';
cout << '\n';
}
return 0;
}