Pagini recente » Cod sursa (job #1329499) | Cod sursa (job #394364) | Cod sursa (job #378633) | Rating Aelx Paraschiv (222darkdark) | Cod sursa (job #1641149)
// 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);
visited.resize(vertices, false);
for (int i = 0; i < edges; i++)
{
int x, y;
f >> x >> y;
x--;
y--;
graph[x].push_back(y);
}
for (int i = 0; i < visited.size(); i++)
if (!visited[i])
topologicalSort(i);
std::ofstream g("sortaret.out");
while (!topSort.empty())
{
g << topSort.top() << " ";
topSort.pop();
}
return 0;
}