Pagini recente » Cod sursa (job #2350724) | Cod sursa (job #1828536) | Cod sursa (job #3005259) | Cod sursa (job #2280277) | Cod sursa (job #745104)
Cod sursa(job #745104)
//Include
#include <fstream>
#include <vector>
using namespace std;
//Constante
const int MAX_SIZE = (int)5e4 + 1;
//Functii
void dfs(int node);
//Variabile
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int nodes, edges;
int visited[MAX_SIZE];
vector<int> before[MAX_SIZE], after[MAX_SIZE];
//Main
int main()
{
in >> nodes >> edges;
int from, to;
while(edges--)
{
in >> from >> to;
after[from].push_back(to);
before[to].push_back(from);
}
for(int i=1 ; i<=nodes ; ++i)
dfs(i);
in.close();
out.close();
return 0;
}
void dfs(int node)
{
if(visited[node])
return ;
vector<int>::iterator it=before[node].begin(), end = before[node].end();
for(; it!=end ; ++it)
if(!visited[*it])
return ;
visited[node] = true;
out << node << ' ';
it=after[node].begin(), end = after[node].end();
for(; it!=end ; ++it)
dfs(*it);
}