Cod sursa(job #1090769)

Utilizator IeewIordache Bogdan Ieew Data 23 ianuarie 2014 02:36:52
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.37 kb
#include <fstream>
#include <stdlib.h>

using namespace std;

#define DEBUG false
#define MAXN 100001
#define MAXM 500000

#if DEBUG
#include <iostream>
#define INFILE "/Users/biordach/Documents/Xcode Projects/training/training/ciclueuler.in"
#define __OUT cout
#else
#define INFILE "ciclueuler.in"
#define OUTFILE "ciclueuler.out"
// #define INFILE "/Users/biordach/Documents/Xcode Projects/training/training/ciclueuler.in"
// #define OUTFILE "/Users/biordach/Documents/Xcode Projects/training/training/ciclueuler.out"


ofstream __OUT(OUTFILE);
#endif

struct node {
    int dest;
    node *next, *correspondent;
}*first[MAXN], *last[MAXN];
int grades[MAXN];
int n, m;

void readInput();
void solve();
void printOutput();

int main(int argc, const char * argv[])
{
    readInput();
    solve();
    printOutput();

#if DEBUG == false
    __OUT.close();
#endif
    
    return 0;
}

void readInput(){
    ifstream in(INFILE);
    in>>n>>m;

    for(int i=0; i < m; i++){
        int x, y;
        in >>x >> y;
        node *q = new node;
        node *w = new node;
        q->next = NULL;
        w->next = NULL;
        
        q->correspondent = w;
        w->correspondent = q;
        
        q->dest = y;
        w->dest = x;
        
        if(!first[x]) {
            first[x] = last[x] =q;
        } else {
            last[x] -> next = q;
            last[x] = q;
        }
        
        if(!first[y]) {
            first[y] = last[y] = w;
        } else {
            last[y] -> next = w;
            last[y] = w;
        }
        
        grades[x] ++;
        grades[y] ++;
        
    }
    in.close();
}

void solve(){
#if DEBUG
    for(int i = 1; i<=n; i++){
        __OUT<<i<<": ";
        for(node* w = first[i];w;w=w->next)
            __OUT<<w->dest<<' ';
        __OUT<<'\n';
    }
#endif
    
    for (int i = 0; i <= n; i++)
        if(grades[i] % 2){
            __OUT<<-1;
            return;
        }
    int current = 1;
    node* q = first[current];
    
    while (q) {
        __OUT<<current<<' ';

        while (first[current] && !first[current]->dest){
            q = first[current] -> next;
            delete first[current];
            first[current] = q;
        }
        q = first[current];

        if(!q)
            continue;
        
        node *w = q->correspondent;
        current = q->dest;
        q->dest = w->dest = 0;
    }
}

void printOutput(){
    __OUT<<'\n';
}