TOJ3649欧拉回路

欧拉回路 分享至QQ空间 去爱问答提问或回答

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 35            Accepted: 20

Description

欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路。现给定一个图,问是否存在欧拉回路?

Input

测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是节点数N ( 1 < N < 1000 )和边数M;随后的M行对应M条边,每行给出一对正整数,分别是该条边直接连通的两个节点的编号(节点从1到N编号)。当N为0时输入结
束。

Output

每个测试用例的输出占一行,若欧拉回路存在则输出1,否则输出0。
 

Sample Input

3 3
1 2
1 3
2 3
3 2
1 2
2 3
0

Sample Output

1
0

#include <iostream>
#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 1100;

int cnt[maxn];
int gn, gm;

int main()
{
    int i;
    int from, to;
    while(scanf("%d%d", &gn, &gm) != EOF && gn) {
        memset(cnt, 0, sizeof(cnt));
        for(i = 0; i < gm; i++) {
            scanf("%d%d", &from, &to);
            cnt[from]++;
            cnt[to]++;
        }
        for(i = 1; i <= gn; i++) {
            if(cnt[i]%2==0 && cnt[i] != 0) continue;
            else {
                printf("0\n");
                break;
            }
        }
        if(i==gn+1) {
            printf("1\n");
        }
    }
    return 0;
}

你可能感兴趣的:(TOJ3649欧拉回路)