hdu 1878水题

判断图是否连通,连通的话如果每个点的度为偶数就存在欧拉回路,否则就不存在。

/*
 * hdu1878/win.cpp
 * Created on: 2012-9-7
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXN = 1010;
bool graph[MAXN][MAXN];
int du[MAXN], myset[MAXN];
int N, M;

void initset() {
    for(int i = 0; i <= N; i++) {
        myset[i] = i;
    }
}

void mymerge(int a, int b) {
    if(a > b) {a ^= b;a ^= b;a ^= b;}
    for(int i = 0; i < MAXN; i++) {
        if(myset[i] == b) {
            myset[i] = a;
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int a, b, i;
    while(scanf("%d", &N) == 1 && N > 0) {
        memset(graph, 0, sizeof(graph));
        memset(du, 0, sizeof(du));
        initset();
        scanf("%d", &M);
        for(i = 0; i < M; i++) {
            scanf("%d%d", &a, &b);
            if(!graph[a - 1][b - 1] && a != b) {
                graph[a - 1][b - 1] = true;
                du[a - 1]++;
                du[b - 1]++;
                if(myset[a - 1] != myset[b - 1]) {
                    mymerge(myset[a - 1], myset[b - 1]);
                }
            }
        }
        for(i = 0; i < N; i++) {
            if(du[i] % 2 != 0) {
                puts("0");
                break;
            }
        }
        if(i == N) {
            if(count(myset, myset + N, 0) == N) {
                puts("1");
            }else {
                puts("0");
            }
        }
    }
    return 0;
}
posted @ 2012-10-31 22:55  moonbay  阅读(138)  评论(0编辑  收藏  举报