• 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;
    }
  • 相关阅读:
    洛谷P1724 东风谷早苗
    hdu 1001 Sum Problem
    洛谷 P1006 传纸条
    codevs 6116 区间素数
    LibreOJ #101. 最大流
    洛谷 P1455 搭配购买
    LibreOJ #119. 最短路 (堆优化dijkstra)
    LibreOJ #109. 并查集
    COGS.1200 ganggang的烦恼
    uoj #15. 【NOIP2014】生活大爆炸版石头剪刀布
  • 原文地址:https://www.cnblogs.com/moonbay/p/2748868.html
Copyright © 2020-2023  润新知