Cod sursa(job #3331354)

Utilizator GliggyGligor Andrei Gliggy Data 26 decembrie 2025 22:00:28
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
// Copilot Rules:
// RULE: There shouldnt be any spaces in the code, except after commas.
// RULE: All variables in main should be declared globally, and can be used before they are defined.
// RULE: All open curly braces should be placed on the same line as the function or control structure they belong to, not on the line after.
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ciclueuler.in");   //strudel
ofstream fout("ciclueuler.out"); //ciclueuler
struct aaa{
    int nod,id;
};
int n,m,i,x,y,ok;
vector<aaa> v[100010];
stack<int> s;
stack<int> c;
int viz[500010];
int ptr[100010];
int main()
{
    fin>>n>>m;
    for(i=1;i<=m;i++){
        fin>>x>>y;
        v[x].push_back({y,i});
        v[y].push_back({x,i});
    }
    for(i=1;i<=n;i++){
        if(v[i].size()%2){
            fout<<"-1";
            return 0;
        }
    }
    s.push(1);
    while(!s.empty()){
        int nod=s.top();
        ok=1;
        for(;ptr[nod]<v[nod].size();ptr[nod]++){
            if(viz[v[nod][ptr[nod]].id]==0){
                viz[v[nod][ptr[nod]].id]=1;
                s.push(v[nod][ptr[nod]].nod);
                ok=0;
                ptr[nod]++;
                break;
            }
        }
        if(ok==1){
            c.push(nod);
            s.pop();
        }
    }
    while(!c.empty()){
        x=c.top();
        c.pop();
        if(!c.empty())
            fout<<x<<" ";
    }
    return 0;
}