Pagini recente » Cod sursa (job #3328198) | Cod sursa (job #3317756) | Cod sursa (job #3356254) | Cod sursa (job #3335856) | Cod sursa (job #3343618)
#include <bits/stdc++.h>
using namespace std;
ifstream f("sortaret.in");
ofstream g("sortaret.out");
int n,m,x,y,nr,vizitat[100200];
vector <int> edges[100200], tedges[100200], rez;
stack <int> st;
void dfs(int x)
{
vizitat[x]=1;
for(auto y:edges[x])
if(vizitat[y]==0)
dfs(y);
st.push(x);
}
void dfs2(int x)
{
vizitat[x]=1;
rez.push_back(x);
for(auto y:tedges[x])
if(vizitat[y]==0)
dfs2(y);
}
int32_t main()
{
f>>n>>m;
for(int i=1; i<=m; i++)
f>>x>>y, edges[x].push_back(y), tedges[y].push_back(x);
for(int i=1; i<=n; i++)
if(vizitat[i]==0)
dfs(i);
memset(vizitat,0,sizeof(vizitat));
while(!st.empty())
{
int x=st.top();
if(vizitat[x]==0)
nr++, dfs2(x), rez.push_back(0);
st.pop();
}
g<<nr<<'\n';
for(auto it:rez)
if(it==0)
g<<'\n';
else
g<<it<<' ';
return 0;
}