Pagini recente » Cod sursa (job #673742) | Istoria paginii runda/hk/clasament | Cod sursa (job #1645856) | Istoria paginii runda/simulareoji10/clasament | Cod sursa (job #2277940)
#include <bits/stdc++.h>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,i,x,y;
vector<pair<int,int> > v[100010];
bitset<500010> used,arbor;
bitset<100010> viz;
vector<int> ans;
void dfs(int poz)
{
viz[poz]=1;
for(auto it:v[poz])
if(!viz[it.first])
{
arbor[it.second]=1;
dfs(it.first);
}
return ;
}
int main()
{
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y;
v[x].push_back({y,i});
v[y].push_back({x,i});
}
dfs(1);
for(i=1;i<=n;i++)
if(!viz[i])
{
g<<-1;
return 0;
}
// for(i=1;i<=n;i++)
// {
// g<<i<<":\n";
// for(auto it:v[i])
// g<<it.first<<' '<<it.second<<' '<<arbor[it.second]<<'\n';
// }
ans.push_back(1);
for(int poz=1;ans.size()<m+1;)
{
bool ok=0;
for(auto it:v[poz])
if(!used[it.second])
if(!arbor[it.second])
{
ok=1;
used[it.second]=1;
poz=it.first;
ans.push_back(poz);
break;
}
if(ok)continue;
for(auto it:v[poz])
if(!used[it.second])
{
ok=1;
used[it.second]=1;
poz=it.first;
ans.push_back(poz);
break;
}
if(!ok)
{
g<<-1;
return 0;
}
}
for(auto it:ans)
g<<it<<' ';
return 0;
}