Pagini recente » Cod sursa (job #8593) | Cod sursa (job #2278930) | Cod sursa (job #3131869) | Cod sursa (job #2263713) | Cod sursa (job #320537)
Cod sursa(job #320537)
#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]){
p->edge = 1;
for (nod *q = L[p->inf]; q != NULL && q->inf != x; q = q->urm)
if (q->inf == x) q->edge = 1;
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;
};
};
};