Cod sursa(job #2064347)

Utilizator SenibelanMales Sebastian Senibelan Data 12 noiembrie 2017 11:08:18
Problema Mesaj4 Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 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> > sol;
vector <bool> use;
enum State {collect, spread} state;

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]){
            if(state == spread)
                sol.push_back(make_pair(node, neighbour));
            DFS(neighbour, node);
        }
    }
    if(state == collect && father != 0)
        sol.push_back(make_pair(node, father));
}

void Solve(){
    DFS(1, 0);
    state = spread;
    fill(use.begin(), use.end(), false);
    DFS(1, 0); 
}

void Print(){
    out << sol.size() << "\n";
    for(int i = 0; i < sol.size(); ++i)
        out << sol[i].first << " " << sol[i].second << "\n";
}

int main(){
    Read();
    Solve();
    Print();
    return 0;
}