Pagini recente » Monitorul de evaluare | Cod sursa (job #2146209) | Cod sursa (job #1113847) | Cod sursa (job #1304335) | Cod sursa (job #2255742)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
//ifstream in("tst.in");
ifstream in("ctc.in");
ofstream out("ctc.out");
int n, m, x, y, vf, cnt, st[100100];
vector <int> v[100100], vv[100100], s[100100];
bool viz[100100];
void dfs(int x){
viz[x] = 1;
for(auto y : v[x])
if(!viz[y])
dfs(y);
st[++vf] = x;
}
void dfs2(int x){
viz[x] = 0;
s[cnt].push_back(x);
for(auto y : vv[x])
if(viz[y])
dfs2(y);
}
int main(){
in >> n >> m;
while(m--){
in >> x >> y;
v[x].push_back(y);
vv[y].push_back(x);
}
for(int i = 1; i <= n; i++)
if(!viz[i])
dfs(i);
for(; vf; vf--)
if(viz[st[vf]]){
cnt++;
dfs2(st[vf]);
}
out << cnt;
for(int i = 1; i <= cnt; i++){
out << '\n';
for(auto j : s[i])
out << j << ' ';
}
return 0;
}