Pagini recente » Monitorul de evaluare | Cod sursa (job #3364541) | Cod sursa (job #3364761) | Cod sursa (job #3365276) | Cod sursa (job #3364762)
#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],c[nmax+1];
int timer,d[nmax+1],low[nmax+1],comp;
bool on_stack[nmax+1];
stack<int>st;
void get_scc(int nod){
int t;
comp++;
do{
t=st.top();
st.pop();
on_stack[t]=0;
c[comp].pb(t);
}while(t!=nod);
}
void tarjan(int nod){
d[nod]=low[nod]=++timer;
st.push(nod);
on_stack[nod]=1;
for(int i:v[nod]){
if(d[i])
low[nod]=min(low[nod],d[i]);
else{
tarjan(i);
low[nod]=min(low[nod],low[i]);
}
}
if(low[nod]==d[nod])
get_scc(nod);
}
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);
}
for(int i=1;i<=n;i++)
if(!d[i])
tarjan(i);
fout<<comp<<'\n';
for(int i=1;i<=comp;i++){
for(int j:c[i])
fout<<j<<' ';
fout<<'\n';
}
return 0;
}