Cod sursa(job #2013882)

Utilizator workwork work work Data 22 august 2017 15:51:00
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

#define f first
#define s second

using namespace std;

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

int n, m, x, y, grd[100005], lng[100005], L, st[500005], top;
vector<pair<int, int> > a[100005];
bitset<500005> u;

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)
    {
        x = st[top];
        L = a[x].size();
        while(lng[x] < L)
        {
            if(!u[a[x][lng[x]].s])
            {
                u[a[x][lng[x]].s] = 1;
                st[++top] = (a[x][lng[x]].f);
                break;
            }
            lng[x]++;
        }
        if(lng[x] == L)
        {
            if(top > 0)
            {
                fprintf(G, "%d ", x);

                top--;
            }
        }
    }
    return 0;
}