Cod sursa(job #1642601)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 9 martie 2016 15:04:50
Problema Mesaj4 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.02 kb
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

class InputReader {
    public:
        InputReader() {}
        InputReader(const char *file_name) {
            input_file = fopen(file_name, "r");
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
        inline InputReader &operator >>(int &n) {
            while(buffer[cursor] < '0' || buffer[cursor] > '9') {
                advance();
            }
            n = 0;
            while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
                n = n * 10 + buffer[cursor] - '0';
                advance();
            }
            return *this;
        }
    private:
        FILE *input_file;
        static const int SIZE = 1 << 17;
        int cursor;
        char buffer[SIZE];
        inline void advance() {
            ++ cursor;
            if(cursor == SIZE) {
                cursor = 0;
                fread(buffer, SIZE, 1, input_file);
            }
        }
};

const int NMAX = 100005;

vector <int> graph[NMAX];
vector <pair <int, int> > sol;

bool vis[NMAX];
void dfs(int node) {
    vis[node] = true;
    for (auto it: graph[node])
        if (!vis[it]) {
            dfs(it);
            sol.push_back(make_pair(node, it));
        }
}

int main()
{
    InputReader cin("mesaj4.in");
    ofstream cout("mesaj4.out");

    int n, m;
    cin >> n >> m;

    int a, b;
    while (m --) {
        cin >> a >> b;

        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    dfs(1);

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

    cout << 2 * n - 2 << '\n';

    for (auto it: sol)
        cout << it.first << ' ' << it.second << '\n';

    reverse(sol.begin(), sol.end());
    for (auto it: sol)
        cout << it.second << ' ' << it.first << '\n';

    //cin.close();
    cout.close();
    return 0;
}