Cod sursa(job #2815839)

Utilizator oporanu.alexAlex Oporanu oporanu.alex Data 10 decembrie 2021 14:58:01
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int nmax = 105;
vector<int> cycle;
vector<int> adj[nmax];
bool used[nmax];
int L[nmax];
int R[nmax];

int main()
{
    int V, E;
    fin >> V >> E;
    for(int i = 1; i <= E; ++i){
        int src, dst;
        fin >> src >> dst;
        adj[src].push_back(i);
        adj[dst].push_back(i);
        L[i] = src;
        R[i] = dst;
    }

    for(int i = 1; i <= V; ++i){
        if(adj[i].size() % 2)
            cout << "-1\n";
        return 0;
    }

    vector<int> cycle;
    stack<int> st;
    st.push(1);

    while(st.size()) {
        int crt = st.top();
        if(adj[crt].empty()){
            st.pop();
            cycle.push_back(crt);
        }
        else {
            int edgeCrt = adj[crt].back();
            adj[crt].pop_back();
            if(used[edgeCrt] == false){
                used[edgeCrt] = true;
                int to = L[edgeCrt] ^ R[edgeCrt] ^ crt;
                st.push(to);
            }
        }
    }
    for(auto el: cycle)
        cout << el << ' ';

    cout << "asdpnfpasdnfpoansdpfnasdpfn\n";
    return 0;
}