Pagini recente » Cod sursa (job #1760242) | Cod sursa (job #473790) | Cod sursa (job #2308489) | Cod sursa (job #2811042) | Cod sursa (job #1589912)
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 50005
using namespace std;
void read_data (int &n, vector <int> v[nmax]){
ifstream fin ("sortaret.in");
int m;
fin >> n >> m;
for (int i = 1; i <= m; i++){
int x, y;
fin >> x >> y;
v[x].push_back (y);
}
}
void dfs (int x, int n, bool seen[nmax], vector <int> v[nmax], vector <int> &sol){
seen[x] = true;
sol.push_back (x);
for (auto neigh : v[x])
if (!seen[neigh])
dfs (neigh, n, seen, v, sol);
}
void solve (bool seen[nmax], int n, vector <int> v[nmax]){
ofstream fout ("sortaret.out");
vector <int> sol;
for (int i = 1; i <= n; i++) seen[i] = 0;
for (int i = 1; i <= n; i++)
if (!seen[i])
dfs (i, n, seen, v, sol);
for (auto x : sol)
fout << x << " ";
}
int main(){
vector <int> v[nmax];
int n;
bool seen[nmax];
read_data (n, v);
solve (seen, n, v);
return 0;
}