Cod sursa(job #2003752)

Utilizator infomaxInfomax infomax Data 23 iulie 2017 21:03:13
Problema Ciclu Eulerian Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

#define f first
#define s second

using namespace std;

FILE *F=fopen("ciclueuler.in", "r"), *G=fopen("ciclueuler.out", "w");

int n, m, x, y, v[500003], st[500003], top, k, w[100003], lng, nod, ind[100003], grd[100003];
vector<pair<int, int> > a[100003];


int main()
{
    fscanf(F, "%d %d ", &n, &m);
    for(int i = 1; i <= m; ++ i)
    {
        fscanf(F, "%d %d ", &x, &y);
        a[x].push_back({y, i});
        a[y].push_back({x, i});
        grd[x] ++;
        grd[y] ++;
    }
    for(int i = 1; i <= n; ++ i)
    {
        if(!grd[i] || grd[i] % 2)
        {
            fprintf(G, "%d", -1);
            return 0;
        }
    }
    st[++ top] = 1;
    while(top>0)
    {
        nod = st[top];
        lng = a[nod].size();
        while(ind[nod] < lng)
        {
            x = a[nod][ind[nod]].f;
            y = a[nod][ind[nod]].s;
            if(!w[y])
            {
                w[y] = 1;
                st[++ top] = x;
                break;
            }
            ind[nod] ++;
        }
        if(ind[nod] == lng)
        {
            if(top > 0)
            {
                fprintf(G, "%d ", nod);
                top--;
            }
        }
    }
    return 0;
}