Pagini recente » Cod sursa (job #2754026) | Cod sursa (job #2221751) | Cod sursa (job #1019754) | Cod sursa (job #531808) | Cod sursa (job #2790317)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("ctc.in");
ofstream g ("ctc.out");
int const N = 1e5 + 3;
int stop [N];
vector <int> v [N] , v2 [N] , comp;
vector <vector <int> > sol;
bool viz [N];
void dfs (int node){
viz [node] = true;
for(auto y : v [node])
if (! viz [y])
dfs (y);
stop [++ stop [0]] = node;
}
void dfs2 (int node){
viz [node] = true;
comp.push_back (node);
for(auto y : v2 [node])
if (! viz [y])
dfs2 (y);
}
int main()
{
int n , m;
f >> n >> m;
for(int i = 1 ; i <= m ; ++ i){
int a , b;
f >> a >> b;
v [a].push_back (b);
v2 [b].push_back (a);
}
for(int i = 1 ; i <= n ; ++ i)
if (! viz [i])
dfs (i);
fill (viz , viz + n + 1 , false);
for(int i = stop [0] ; i ; -- i)
if (! viz [stop [i]]){
comp.clear ();
dfs2 (stop [i]);
sol.push_back (comp);
}
g << sol.size () << '\n';
for(auto y : sol){
for(auto y2 : y)
g << y2 << ' ';
g << '\n';
}
return 0;
}