Cod sursa(job #1758978)

Utilizator fanache99Constantin-Buliga Stefan fanache99 Data 18 septembrie 2016 12:07:51
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream cin("mesaj4.in");
ofstream cout("mesaj4.out");

const int MAXN = 100000;

vector<int> g[1 + MAXN];
vector<pair<int, int> > solution;
bool seen[1 + MAXN];

void DFS(int node) {
    seen[node] = true;
    for (auto &son : g[node])
        if (!seen[son]) {
            DFS(son);
            solution.push_back(make_pair(son, node));
        }
}

int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    DFS(1);
    for (int i = 1; i <= n; i++)
        if (!seen[i]) {
            cout << "-1\n";
            return 0;
        }
    cout << solution.size() * 2 << "\n";
    for (int i = 0; i < solution.size(); i++)
        cout << solution[i].first << " " << solution[i].second << "\n";
    for (int i = solution.size() - 1; i >= 0; i--)
        cout << solution[i].second << " " << solution[i].first << "\n";
    return 0;
}