Pagini recente » Istoria paginii runda/carnedecal | Cod sursa (job #1010749) | Cod sursa (job #1490544) | Istoria paginii runda/clasa_a_19 | Cod sursa (job #2783589)
#include <fstream>
#include <vector>
using namespace std;
vector<int> graph[50005];
int vis[50005], v[50005], cnt;
void dfs(int node)
{
vis[node] = true;
for (int i = 0; i < graph[node].size(); i++)
{
int next = graph[node][i];
if (!vis[next])
dfs(next);
}
v[++cnt] = node;
}
int main()
{
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
int N, M, x, y;
fin >> N >> M;
for (int i = 1; i <= M; i++)
{
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for (int i = 1; i <= N; i++)
if (!vis[i])
dfs(i);
for (int i = cnt; i >= 1; i--)
fout << v[i] << " ";
return 0;
}