Cod sursa(job #1927124)

Utilizator valentin50517Vozian Valentin valentin50517 Data 14 martie 2017 22:28:01
Problema Ciclu Eulerian Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
#define maxm 500100
#define maxn 100100
using namespace std;

int N,M,Sz[maxn],St[maxn],st;
bool B[maxn];
map<int,int> G[maxn];
void add(int x,int y){G[x][y]++,G[y][x]++,Sz[x]++,Sz[y]++;}
void del(int x,int y){if(G[x][y] > 0) G[x][y]--,G[y][x]--; if(G[x][y]==0) G[x].erase(y),G[y].erase(x);}

void dfs(int nod){
    if(B[nod]) return; B[nod]=1;
    for(auto it : G[nod]) dfs(it.first);
}

void euler(int nod){
    St[++st] = nod;
    while(st){
        nod = St[st];
        if(G[nod].empty()){
            --st;
            cout << nod << ' ';
        }else{
            int son = G[nod].begin()->first;
            del(nod,son);
            St[++st] = son;
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);
    ifstream cin("euler.in");
    ofstream cout("euler.out");
    cin >> N >> M;
    for(int x,y;M--;){
        cin >> x >> y;
        add(x,y);
    }
    dfs(1);
    for(int i = 1;i<=N;i++) if(Sz[i]%2 || !B[i]) return cout << G[i].size(),0;
    euler(1);
}