zoukankan      html  css  js  c++  java
  • HDU

    题目链接
      如果存在欧拉回路,则需要所有点都是连通的,同时,因为题中的边没有方向,所以只要在连通的基础上判断是否有所有点的度数为偶数即可。
    1.用dfs判断能否访问所有的点。

    const int maxn = 1e3+10;
    int degree[maxn], g[maxn][maxn], n, m;
    void dfs(int u) {
        for (int i = 1; i<=n; ++i)
            if (g[u][i]) {
                --g[u][i], --g[i][u];
                dfs(i);
            }
    }
    int main(void) {
        while(~scanf("%d", &n) && n) {
            scanf("%d", &m);
            while(m--) {
                int a, b;
                scanf("%d%d", &a, &b);
                ++g[a][b], ++g[b][a];
                ++degree[a], ++degree[b];
            }
            dfs(1);
            bool ok = true;
            for (int i = 1; i<=n; ++i)
                for (int j = 1; j<=n; ++j) {
                    if (g[i][j]) ok = false;
                    g[i][j] = 0;
                }
            for (int i = 1; i<=n; ++i) 
                if (degree[i]&1) ok = false;
            printf(ok ? "1
    " : "0
    ");
            zero(degree);
        }
        return 0;
    }
    

    2.用并查集来判断形成的图的个数

    const int maxn = 1e3+10;
    int m, n, p[maxn], degree[maxn];
    int find(int root) {
        int son = root, tmp;
        while(root != p[root]) root = p[root];
        while(son != root) {
            tmp = p[son];
            p[son] = root;
            son = tmp;
        }
        return root;
    }
    void merge(int a, int b) {
        p[find(a)] = find(b);
    }
    int main(void) {
        while(~scanf("%d", &n) && n) {
            for (int i = 1; i<=n; ++i) p[i] = i, degree[i] = 0;
            scanf("%d", &m);
            while(m--) {
                int a, b;
                scanf("%d%d", &a, &b);
                merge(a, b);
                ++degree[a], ++degree[b];
            }
            int cnt = 0; bool ok = true;
            for (int i = 1; i<=n; ++i) {
                if (p[i]==i) ++cnt;
                if (degree[i]&1) ok = false;
            }
            printf(ok && cnt == 1 ? "1
    " : "0
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    iphone/iOS 访问本地数据库sqlite3
    SQLITE3 --详解
    iOS使用MD5
    ASIHTTPRequest实现断点续传
    ios开发
    iOS 5的文件存储策略应对
    由ASIHttpRequest里的block引发的思考
    Blocks编程要点
    [Cocoa]深入浅出Cocoa多线程编程之 block 与 dispatch quene
    ASIHTTPRequest 问题总结
  • 原文地址:https://www.cnblogs.com/shuitiangong/p/12641733.html
Copyright © 2011-2022 走看看