Cod sursa(job #1987165)

Utilizator DianaVelciovVelciov Diana DianaVelciov Data 29 mai 2017 21:04:25
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int NMAX = 100000;
int N, M, e;
vector<int> G[NMAX + 5];
bool use[NMAX + 5];
pair<int,int> P[2 * NMAX + 5];

void DFS(int node){
    use[node]=true;
    for (int vecin : G[node])
        if (!use[vecin]){
            DFS(vecin);
            P[++e]=make_pair(vecin,node);
        }
}

int main()
{
    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);
    int ok=1;
    for (int i = 1; i <= N; ++i){
        if (!use[i])
            out << "-1\n",ok=0;
    }
    if (ok){
        out << 2 * (N - 1) << '\n';
        for(int i = 1; i <= e; ++i)
            out << P[i].first << " " << P[i].second << '\n';
        for(int i = e; i >= 1; --i)
            out << P[i].second << " " << P[i].first << '\n';
    }
    in.close();
    out.close();
    return 0;
}