Cod sursa(job #1677629)

Utilizator vladrochianVlad Rochian vladrochian Data 6 aprilie 2016 18:10:43
Problema Mesaj4 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

const int N_MAX = 1e5;

ifstream fin("mesaj4.in");
ofstream fout("mesaj4.out");

int N, M;
vector<int> G[N_MAX + 5];

bool use[N_MAX + 5];
vector<pair<int, int>> sol;

void Dfs(int node) {
   use[node] = true;
   for (int son : G[node])
      if (!use[son]) {
         Dfs(son);
         sol.emplace_back(son, node);
      }
}

int main() {
   fin >> N >> M;
   while (M--) {
      int x, y;
      fin >> x >> y;
      G[x].push_back(y);
      G[y].push_back(x);
   }

   Dfs(1);

   for (int i = 1; i <= N; ++i)
      if (!use[i]) {
         fout << "-1\n";
         return 0;
      }

   fout << 2 * (N - 1) << "\n";

   for (const auto &it : sol)
      fout << it.first << " " << it.second;

   reverse(sol.begin(), sol.end());

   for (const auto &it : sol)
      fout << it.second << " " << it.first;

   return 0;
}