Cod sursa(job #2569095)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 4 martie 2020 11:04:44
Problema Mesaj4 Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <bits/stdc++.h>
#define FILE_NAME "mesaj4"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 100100
using namespace std;

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

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ld,ld> pct;

const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;


void add(ll &a , ll b)
{
    a += b;
    a %= mod;
}

void sub(ll &a, ll b)
{
    a = (a - b + mod) % mod;
}

int n, m;
vector <int> G[NMAX];
bitset <NMAX> viz;
vector <pi> sol;

void DFS(int nod)
{
    viz[nod] = 1;
    for(auto it : G[nod])
        if(!viz[it])
        {
            DFS(it);
            sol.push_back({it,nod});
        }
}
int main()
{
    f >> n >> m;
    for(int x,y, i = 1; i <= m; ++i)
    {
        f >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFS(1);
    if(sol.size() != (n - 1))
        {g << -1; return 0;}
    g << 2 * n - 2 << '\n';
    for(int i = 0; i < sol.size(); ++i)
        g << sol[i].first << ' ' << sol[i].second << '\n';
    for(int i = sol.size() - 1; i >= 0; --i)
        g << sol[i].second << ' ' << sol[i].first << '\n';
    f.close();
    g.close();
    return 0;
}