Cod sursa(job #2011710)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 16 august 2017 23:10:51
Problema Mesaj4 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define oo 1000000000
#define PII pair <int, int>
  
using namespace std;
int n, m;
vector < int > V[100100];
bool v[100100];
vector < PII > R;
void dfs(int x){
	v[x] = 1;
	for (auto it : V[x])
		if (!v[it]) R.pb({x, it}), dfs(it);
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    ifstream cin("mesaj4.in");
    ofstream cout("mesaj4.out");
    cin >> n >> m;
    for (int i = 1, x, y; i <= m; i++) cin >> x >> y, V[x].pb(y), V[y].pb(x);
    dfs(1);
    int l = R.size();
    if (l != n - 1) return cout << -1, 0;
    cout << 2 * l << "\n";
    for (auto it : R) cout << it.x << " " << it.y << "\n";
    reverse(R.begin(), R.end());
    for (auto it : R) cout << it.y << " " << it.x << "\n";
}