Pagini recente » Cod sursa (job #2474700) | Cod sursa (job #2121348) | Cod sursa (job #79859) | Cod sursa (job #2246900) | Cod sursa (job #2725375)
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
const int N=50005, M=100005;
vector<int> ctc[N],a[N],a_t[N];
stack<int> stiva;
int viz[N],viz_t[N],nc;
void dfs(int x)
{
viz[x]=1;
for (auto y: a[x])
{
if (!viz[y])
{
dfs(y);
}
}
stiva.push(x);
}
void dfs_t(int x)
{
viz_t[x]=1;
ctc[nc].push_back(x);
for (auto y:a_t[x])
{
if (!viz_t[y])
{
dfs_t(y);
}
}
}
int main()
{
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int n,m;
in>>n>>m;
for (int i=0;i<m;i++)
{
int x,y;
in>>x>>y;
a[x].push_back(y);
a_t[y].push_back(x);
}
for (int i=1;i<=n;i++)
{
if (viz[i]!=1) dfs(i);
}
while (!stiva.empty())
{
int x=stiva.top();
stiva.pop();
out<<x<<" ";
/*
if (!viz_t[x])
{
nc++;
dfs_t(x);
}
*/
}
in.close();
out.close();
return 0;
}