Pagini recente » Cod sursa (job #1376099) | Cod sursa (job #2402269) | Cod sursa (job #2119866) | Cod sursa (job #154513) | Cod sursa (job #320239)
Cod sursa(job #320239)
#include <stdio.h>
#define MaxN 100001
int n, m;
struct nod {
int inf, edge;
nod *urm;
};
nod *L[MaxN];
bool viz[MaxN];
int grad[MaxN];
void add(int ex1, int ex2){
nod *p, *q;
p = new nod;
p->inf = ex2;
p->urm = L[ex1];
L[ex1] = p;
q = new nod;
q->inf = ex1;
q->urm = L[ex2];
L[ex2] = q;
};
void DFS(int x){
viz[x] = true;
for (nod *p = L[x]; p != NULL; p = p->urm)
if (!viz[p->inf]){
DFS(p->inf);
};
};
int main(){
int ex1, ex2;
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d %d",&n, &m);
for (int i = 1; i <= m; i++){
scanf("%d %d", &ex1, &ex2);
add(ex1,ex2);
grad[ex1]++;
grad[ex2]++;
};
DFS(1);
for (int i = 1; i <= n; i++){
if (grad[i] % 2) {
printf("-1");
return 0;
};
if (viz[ i ] != true ) {
printf("-1");
return 0;
};
};
};