Cod sursa(job #2095543)

Utilizator georgerapeanuRapeanu George georgerapeanu Data 27 decembrie 2017 17:46:08
Problema Ciclu Eulerian Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
FILE *f = fopen("ciclueuler.in","r");
FILE *g = fopen("ciclueuler.out","w");
int N,M;
vector<pair<int,int> >  G[100005];
bool U[500005];
int st[500005];
int len;
int main()
{
    fscanf(f,"%d %d",&N,&M);
    for(int i = 1;i <= M;i++){
        int x,y;
        fscanf(f,"%d %d",&x,&y);
        G[x].push_back({y,i});
        G[y].push_back({x,i});
    }
    st[++len] = 1;
    while(len){
        int nod = st[len];
        while(!G[nod].empty() && U[G[nod].back().second]){
            G[nod].pop_back();
        }
        if(!G[nod].empty()){
            int v = G[nod].back().first;
            U[G[nod].back().second] = 1;
            G[nod].pop_back();
            st[++len] = v;
        }
        else{
            if(len != 1){
                fprintf(g,"%d ",nod);
            }
            len--;
        }
    }
    fclose(f);
    fclose(g);
    return 0;
}