Cod sursa(job #1851776)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 20 ianuarie 2017 07:18:19
Problema Mesaj4 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>
using namespace std;

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

static constexpr int maxn = 1e5 + 20;

int n, m;
vector<int> vec[maxn];
bool viz[maxn] = {};
vector<pair<int, int>> up, down;

void dfs(const int cur){
    for(const int next : vec[cur]){
        if(viz[next]) continue;
        viz[next] = true;
        down.emplace_back(cur, next);
        dfs(next);
        up.emplace_back(next, cur); } }

int main(){
    f >> n >> m;
    for(int x, y; m; --m){
        f >> x >> y;
        --x, --y;
        vec[x].push_back(y);
        vec[y].push_back(x); }

    viz[0] = true;
    dfs(0);

    if(up.size() + down.size() != 2*n-2) g << -1 << endl; 
    else{
        g << 2*n - 2 << '\n';
        for(const auto p : up  ) g << p.first+1 << ' ' << p.second+1 << '\n';
        for(const auto p : down) g << p.first+1 << ' ' << p.second+1 << '\n'; }

    return 0; }