zoukankan      html  css  js  c++  java
  • NYOJ 42 一笔画问题

    http://acm.nyist.net/JudgeOnline/problem.php?pid=42

    判断一个无向图是否存在欧拉通路。

    首先用并查集排除图不联通的情况

    如果存在欧拉回路的话,那么所有顶点的度数应该都是偶数。

    如果存在欧拉通路的话,那么有且仅有两个顶点的度数是奇数,其他的都是偶数。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    const int maxn = 1000 + 20;
    int Degree[maxn];
    int fa[maxn];
    int find(int u) {
        if (fa[u] == u) return fa[u];
        else return fa[u] = find(fa[u]);
    }
    void merge(int x, int y) {
        x = find(x);
        y = find(y);
        if (x != y) fa[y] = x;
    }
    void work() {
        memset(Degree, 0, sizeof Degree);
        for (int i = 0; i <= maxn - 20; ++i) fa[i] = i;
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; ++i) {
            int a, b;
            scanf("%d%d", &a, &b);
            Degree[a]++;
            Degree[b]++;
            merge(a, b);
        }
        int flag = 0;
        for (int i = 1; i <= n; ++i) {
            flag += find(i) == i;
        }
        if (flag != 1) {
            printf("No
    ");
            return;
        }
        int odd = 0;
        for (int i = 1; i <= n; ++i) {
            odd += Degree[i] & 1;
        }
        if (odd == 0 || odd == 2) {
            cout << "Yes" << endl;
        } else cout << "No" << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt","r",stdin);
    #endif
        int t;
        scanf("%d", &t);
        while (t--) work();
        return 0;
    }
    View Code
  • 相关阅读:
    CSS3旋转动画
    CSS3的动画属性
    CSS选择器
    JS事件委托
    js 轮播图效果
    JS事件冒泡和事件捕获
    JS自定义播放器
    js闭包for循环只执行最后一个值得解决方法
    交通红绿灯
    汉明距
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6016935.html
Copyright © 2011-2022 走看看