Pagini recente » Cod sursa (job #2720247) | Cod sursa (job #1410164) | Cod sursa (job #1581271) | Cod sursa (job #1807638) | Cod sursa (job #1714618)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream in("mesaj4.in");
ofstream out("mesaj4.out");
const int maxn = 100005;
pair <int, int> rec[maxn];
vector <int> g[maxn];
bitset <maxn> viz;
int cnt;
void dfs(int x)
{
viz[x] = 1;
for(auto it : g[x])
{
if(!viz[it])
{
dfs(it);
cnt++;
rec[cnt] = make_pair(x, it);
}
}
}
int main()
{
int n, m;
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
if(cnt < n - 1)
{
out << -1 << "\n";
return 0;
}
out << cnt * 2 << "\n";
for(int i = 1; i <= cnt; i++)
out << rec[i].second << " " << rec[i].first << "\n";
for(int i = cnt; i >= 1; i--)
out << rec[i].first << " " << rec[i].second << "\n";
return 0;
}