Cod sursa(job #2949035)

Utilizator pifaDumitru Andrei Denis pifa Data 29 noiembrie 2022 14:49:55
Problema Ciclu Eulerian Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");

int n, m;

const int N = 1e5;

vector <int> edges[N + 1];

int fre[N + 1];

vector <int> v;

void dfs(int k)
{
    fre[k] = 1;
    for(auto x:edges[k])
    {
        if(!fre[x])
        {
            fre[x] = 1;
            dfs(x);
            v.push_back(x);
        }
    }
}

int main()
{
    in >> n >> m;
    map <int, int> mp;
    for(int i = 1; i <= m; i++)
    {
        int x, y;
        in >> x >> y;
        mp[x]++;
        mp[y]++;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }
    int ok = 0;
    for(auto x:mp)
    {
        if(x.second % 2)
            ok = 1;
    }
    dfs(1);

    if(v.size() + 1 != n || ok)
        out << -1;
    return 0;
}