Cod sursa(job #1738894)

Utilizator tudorgalatanRoman Tudor tudorgalatan Data 7 august 2016 22:45:05
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;

vector <int> G[100001], H[100001];
deque <int> Q;
int degree[100005];
bool seen[500005];
int node, u, v, N, M, x, y, i;

int main ()
{
    ifstream fin ("ciclueuler.in");
    fin >> N >> M;
    for (i=1; i<=M; i++)
    {
        fin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
        H[u].push_back(i);
        H[v].push_back(i);
        degree[u]++;
        degree[v]++;
    }
    ofstream fout ("ciclueuler.out");
    for (i=1; i<=N; i++)
        if (degree[i]%2 == 1)
        {
            fout << -1;
            return 0;
        }
    Q.push_back(1);
    while (!Q.empty())
    {
        node = Q.back();
        if (degree[node] == 0)
        {
            fout << node << ' ';
            Q.pop_back();
        }
        else
        {
            x = G[node].back();
            y = H[node].back();
            G[node].pop_back();
            H[node].pop_back();
            if (seen[y] == 0)
            {
                seen[y] = 1;
                Q.push_back(x);
                degree[x]--;
                degree[node]--;
            }
        }
    }
    return 0;
}