Pagini recente » Cod sursa (job #502746) | Cod sursa (job #1966843) | Cod sursa (job #2025868) | Cod sursa (job #1484989) | Cod sursa (job #607746)
Cod sursa(job #607746)
#include <fstream>
#include <vector>
using namespace std;
struct Node
{
vector<unsigned long int> m_Vertices;
bool m_Visited;
};
Node Graph[50000];
unsigned long int n, m, x, y, b;
vector<unsigned long int> Print;
void visit(unsigned long int t)
{
if (Graph[t].m_Visited == false)
{
Graph[t].m_Visited = true;
Print.push_back(t);
for (unsigned long int i=0; i<Graph[t].m_Vertices.size(); i++)
{
visit(Graph[t].m_Vertices[i]);
}
}
}
int main()
{
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
fin >> n >> m;
fin >> x >> y;
Graph[x].m_Vertices.push_back(y);
b = x;
for (unsigned long int i=1; i<m; i++)
{
fin >> x >> y;
Graph[x].m_Vertices.push_back(y);
}
visit(b);
for (unsigned long int i=0; i<Print.size(); i++)
fout << Print[i] << " ";
for (unsigned long int i=0; i<n; i++)
if (Graph[i].m_Visited == false)
fout << i << " ";
fin.close();
fout.close();
return 0;
}