Pagini recente » Cod sursa (job #2666953) | Cod sursa (job #1373439) | Cod sursa (job #2294457) | Cod sursa (job #736333) | Cod sursa (job #1024761)
#include<fstream>
#include<vector>
using namespace std;
ifstream f("mesaj4.in"); ofstream g("mesaj4.out");
int n,m,viz[100001];
vector <int> L[100001];
vector < pair <int, int> > sol;
void dfs(int nod)
{ viz[nod]=1;
vector<int>::iterator it=L[nod].begin(), sf=L[nod].end();
for(; it!=sf; ++it)
if(!viz[*it])
{ sol.push_back(make_pair(nod,*it));
dfs(*it);
}
}
int main()
{ f>>n>>m;
for(int x,y,i=1; i<=m; ++i) f>>x>>y, L[x].push_back(y), L[y].push_back(x);
dfs(1);
if(sol.size()!=n-1) g<<"-1\n";
else
{ g<<2*sol.size()<<'\n';
vector < pair<int,int> >::reverse_iterator itr = sol.rbegin(), sfr=sol.rend();
for(; itr!=sfr; ++itr) g<<(*itr).second<<' '<<(*itr).first<<'\n';
vector <pair <int, int> >::iterator it=sol.begin(), sf=sol.end();
for(; it!=sf; ++it) g<<(*it).first<<' '<<(*it).second<<'\n';
}
g.close(); return 0;
}