Pagini recente » Borderou de evaluare (job #3366761) | Borderou de evaluare (job #3364763) | Monitorul de evaluare | Cod sursa (job #3364541) | Cod sursa (job #3364761)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
const int nmax = 1e5;
vector<int>v[nmax + 1], vr[nmax + 1], c[nmax + 1];
int comp;
bool vap[nmax + 1];
stack<int>st;
void dfs (int nod) {
vap[nod] = 1;
for (int i : v[nod])
if (!vap[i])
dfs (i);
st.push (nod);
}
void dfsr (int nod) {
c[comp].pb(nod);
vap[nod] = 0;
for (int i : vr[nod])
if (vap[i])
dfsr (i);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
fin>>n>>m;
while(m--){
int st,dr;
fin>>st>>dr;
v[st].pb(dr);
vr[dr].pb(st);
}
for(int i=1;i<=n;i++)
if(!vap[i])
dfs(i);
while(st.size()){
int t=st.top();
st.pop();
if(vap[t]){
comp++;
dfsr(t);
}
}
fout<<comp<<'\n';
for(int i=1;i<=comp;i++){
for(int j:c[i])
fout<<j<<' ';
fout<<'\n';
}
return 0;
}