Cod sursa(job #2605504)

Utilizator CezarNCezar Negoescu CezarN Data 25 aprilie 2020 12:07:03
Problema Ciclu Eulerian Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.88 kb
//#include <bits/stdc++.h)
#include <iostream>
#include <fstream>
#include <vector>
const int NMAX = 100000, MMAX = 500000;
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
struct edge {
    int node1;  //if negative then the edge is visited
    int node2;
};
edge ei;
int sol[1+MMAX];              //solution
int sol_idx=0;
edge e[1+MMAX];               //edges
vector<int>  g[1 + NMAX];     //graph nodes - vector of adjacent lists
int main()
{
    int n, m, n1, n2;
    in >> n >> m;
    ei.node1 = 0; ei.node1 = 0;
    e[0] = ei; //e.push_back(ei);     //make the edges vector 1-based, so put something at position ZERO
    for (int i = 0; i < m; i++) {
        in >> n1 >> n2;  //n1 and n2 are the ends of the edge
        ei.node1 = n1;
        ei.node2 = n2;
        e[i + 1] = ei;   //e.push_back(ei);  //store the edge in edge vector
        if (g[n1].empty()) g[n1].push_back(0);  //put number of edges at position 0 of adjacent list for src node
        if (g[n2].empty()) g[n2].push_back(0);  //put number of edges at position 0 of adjacent list for dest node
        g[n1][0]++;  //increase the soure node degree
        g[n2][0]++;  //increase the destination node degree
        g[n1].push_back(i + 1);  //store the edge number in the adjacent list of source node(1-based index)
        g[n2].push_back(i + 1);  //store the edge number in the adjacent list of destnation node
    }
    //check whether g is eulerian graph
    for (int i = 0; i < n; i++) {
        if (g[i + 1][0] & 1) {  //odd number of edges, reject it!
            out << -1 << endl;
            return 0;
        }
    }
    //search the graph for an eulerian cycle
    vector<int> stk;   //stack
    stk.push_back(1);
    while (!stk.empty()) {
        int nod = stk.back();
        if (g[nod][0] > 0) {  //there are more unprocessed edges adjacent to this node
            int next_e = g[nod][g[nod][0]];   //next edge from nod's adjacency list
            g[nod][0]--;                      //delete this edge from nod's adjacency list
            if (e[next_e].node1 > 0) {        //if this edge is unvisited ...
                int next_nod = nod == e[next_e].node1 ? e[next_e].node2 : e[next_e].node1;  //get the other end of the edge ...
                e[next_e].node1 = -1;         //... mark the edge as visited
                stk.push_back(next_nod);      //... and put it on the stack
            }
        }  //no more unvisited edges for this node  
        else {
            sol[sol_idx++] = nod;  //put the node into the solution cycle
            stk.pop_back();        //delete the node from the stack
        }
    }
    //print the solution.The last node is the start node so do not print it
    for (int i = 0; i < sol_idx-1; i++) out << sol[i] << ' ';
    in.close();    //close input filestream
    out.close();   //close the output stream
}