Pagini recente » Cod sursa (job #518649) | Istoria paginii runda/oji_xi/clasament | Cod sursa (job #2360388) | Cod sursa (job #764194) | Cod sursa (job #1641159)
// Infoarena DFS
#include <fstream>
#include <vector>
#include <stack>
std::vector<std::vector<int>> graph;
std::vector<bool> visited;
std::stack<int> topSort;
int vertices, edges;
void topologicalSort(int vertex)
{
visited[vertex] = true;
for (int i = 0; i < graph[vertex].size(); i++)
if (!visited[graph[vertex][i]])
topologicalSort(graph[vertex][i]);
topSort.push(vertex);
}
int main()
{
std::ifstream f("sortaret.in");
f >> vertices >> edges;
graph.resize(vertices + 1);
visited.resize(vertices + 1, false);
for (int i = 0; i < edges; i++)
{
int x, y;
f >> x >> y;
graph[x].push_back(y);
}
for (int i = 1; i < visited.size(); i++)
if (!visited[i])
topologicalSort(i);
std::ofstream g("sortaret.out");
while (!topSort.empty())
{
g << topSort.top() << " ";
topSort.pop();
}
return 0;
}