Cod sursa(job #2064354)

Utilizator SenibelanMales Sebastian Senibelan Data 12 noiembrie 2017 11:25:35
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int NMAX = 100005;

int N, M;
vector <int> G[NMAX];
vector < pair<int,int> > sol1;
vector < pair<int,int> > sol2;
vector <bool> use;

void Read(){
    in >> N >> M;
    use.resize(N + 5, false);
    for(int i = 1; i <= M; ++i){
        int x, y;
        in >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x); 
    }
}

void DFS(int node, int father){
    use[node] = true;
    for(int i = 0; i < G[node].size(); ++i){
        int neighbour = G[node][i];
        if(!use[neighbour]){
            sol2.push_back(make_pair(node, neighbour));
            DFS(neighbour, node);
        }
    }
    if(father != 0)
        sol1.push_back(make_pair(node, father));
}


void Print(){
    if(sol1.size() != N - 1)
      out << -1 << "\n";
    else{
      out << sol1.size() + sol2.size() << "\n";
      for(int i = 0; i < sol1.size(); ++i)
          out << sol1[i].first << " " << sol1[i].second << "\n";
      for(int i = 0; i < sol2.size(); ++i)
          out << sol2[i].first << " " << sol2[i].second << "\n";
    }
}

int main(){
    Read();
    DFS(1, 0);
    Print();
    return 0;
}