Cod sursa(job #2462677)

Utilizator TheNextGenerationAyy LMAO TheNextGeneration Data 27 septembrie 2019 18:48:45
Problema Mesaj4 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <bits/stdc++.h>
using namespace std;
//ifstream cin("mesaj4.in");
//ofstream cout("mesaj4.out");
const int N = 1e5+5;
bool viz[N];
vector<int> v[N];
vector< pair<int,int> > ans;
void dfs(int x)
{
    viz[x] = 1;
    for (auto it: v[x])
        if (!viz[it])
        {
            dfs(it);
            ans.push_back({it,x});
        }
}
int main()
{
    int n,m;
    cin >> n >> m;
    for (int i = 1; i<=m; i++)
    {
        int x,y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1);
    if (ans.size()!=n-1)
    {
        cout << -1;
        return 0;
    }
    cout << 2*(n-1) << "\n";
    for (auto it: ans)
        cout << it.first << " " << it.second << "\n";
    reverse(ans.begin(),ans.end());
    for (auto it: ans)
        cout << it.second << " " << it.first << "\n";
}