Cod sursa(job #2792503)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 1 noiembrie 2021 19:51:15
Problema Mesaj4 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.85 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << '\n'
#define debugsp(x) cerr << #x << " " << x << ' '

using namespace std;

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

const int INF = 2e9;
const int N = 1e5;

vector <int> g[1 + N];
vector <pair <int, int>> ans;
bool viz[1 + N];

void DFS (int node) {
    viz[node] = true;

    for (int to : g[node])
        if (!viz[to]) {
            ans.push_back(make_pair(to, node));
            DFS(to);
        }
}

int main() {
    int n, m;
    in >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        in >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    DFS(1);
    for (int i = 1; i <= n; i++)
        if (!viz[i]) {
            out << "-1\n";
            return 0;
        }
    
    out << 2 * (n - 1) << '\n';
    for (int i = ans.size() - 1; i >= 0; i--)
        out << ans[i].first << ' ' << ans[i].second << '\n';
    for (int i = 0; i < ans.size(); i++)
        out << ans[i].second << ' ' << ans[i].first << '\n';
    in.close();
    out.close();
    return 0;
}