Cod sursa(job #3169118)

Utilizator paaull69Ion Paul paaull69 Data 14 noiembrie 2023 11:28:51
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.93 kb
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
#define MOD 1000000007

ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");

int dist(int x1,int y1,int x2,int y2)
{
    return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}
int det(int x1,int y1,int x2,int y2,int x3,int y3)
{
    return (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);
}

struct point
{
    int x,y;
};

ll arr(point l,point r)
{
    return (long long)(r.x-l.x)*(long long)(r.y-l.y);
}
ll per(point l,point r)
{
    return (long long)(r.x-l.x)+(long long)(r.y-l.y);
}
ll areaofrect(point l1,point r1,point l2,point r2)
{
    long long dx=min(r1.x,r2.x)-max(l1.x,l2.x);
    long long dy=min(r1.y,r2.y)-max(l1.y,l2.y);
    long long ar=dx*dy >= 0 ? dx*dy : 0;
    return ar;
}
ll perofrect(point l1,point r1,point l2,point r2)
{
    ll dx=min(r1.x,r2.x)-max(l1.x,l2.x);
    ll dy=min(r1.y,r2.y)-max(l1.y,l2.y);
    if(dx < 0 || dy < 0)return 0;
    return dx+dy;
}

int from[500001],to[500001];

vector<int> g[100001];
vector<int> res;

bool usedEdge[500001];
int n,m;

int main()
{

    fin >> n >> m;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        fin >> x >> y;
        g[x].push_back(i);
        g[y].push_back(i);
        from[i]=x;
        to[i]=y;
    }
    bool eulerian=1;
    for(int i=1;i<=n;i++)
    {
        if(g[i].size() %2)eulerian=0;
    }
    if(!eulerian)
    {
        fout << -1;
        return 0;
    }

    stack<int> S;
    S.push(1);
    while(!S.empty())
    {
        int x=S.top();
        if(!g[x].empty())
        {
            int e = g[x].back();
            g[x].pop_back();
            if(usedEdge[e])continue;
            usedEdge[e]=1;
            int w = to[e] ^ from[e] ^ x;
            S.push(w);
        }
        else
        {
            S.pop();
            res.push_back(x);
        }
    }
    for(int i=0;i<res.size()-1;i++)fout << res[i] << " ";
}