Pagini recente » Cod sursa (job #1138264) | Cod sursa (job #2703433) | Cod sursa (job #976630) | Cod sursa (job #1897328) | Cod sursa (job #2056322)
#include <bits/stdc++.h>
#define NMAX 50005
using namespace std;
ifstream fi("sortaret.in"); void Input();
ofstream fo("sortaret.out"); void Output();
int N, M;
int indexor;
vector<int> graph[NMAX];
bool visited[NMAX];
int topo[NMAX];
void DFS(int node)
{
visited[node] = true;
int y;
for(auto y:graph[node])
{
if(!visited[y]);
DFS(y);
}
topo[++indexor] = node;
}
int main()
{
Input();
for(int i = 1; i <= N; ++i)
{
if(!visited[i])
{
DFS(i);
topo[++indexor] = i;
}
}
Output();
}
void Input()
{
fi>>N>>M;
int x, y;
for(int i = 1; i <= M; ++i)
{
fi>>x>>y;
graph[x].push_back(y);
}
}
void Output()
{
for(auto i = N; i >= 1; --i)
{
fo<<topo[i]<<' ';
}
}