zoukankan      html  css  js  c++  java
  • 图论

    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<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<queue>
    using namespace std;
    
    #define Int __int64
    #define INF 0x3f3f3f3f
    
    const int MAXN = 1111;
    int road[MAXN];
    int grade[MAXN];
    int sum;
    int n, m;
    
    int FindRoot(int rt) {
        return road[rt] == rt ? rt : (road[rt] = FindRoot(road[rt]));
    }
    
    void init() {
        for (int i = 1; i <= n; i++) {
            road[i] = i;
        }
        sum = n;
        memset(grade, 0, sizeof(grade));
    }
    
    int main()
    {
        //freopen("input.txt", "r", stdin);
        while (scanf("%d %d", &n, &m), n) {
            int u, v;
            init();//以后定要记得优先把初始化函数写上,总是丢三落四的= =
            for (int i = 1; i <= m; i++) {
                scanf("%d %d", &u, &v);
                grade[u]++;
                grade[v]++;
                int root1 = FindRoot(u);
                int root2 = FindRoot(v);
                if (root1 != root2) {
                    sum--;
                    road[root2] = root1;
                }
            }
            if (sum != 1) {
                printf("0
    ");
                continue;
            }
            bool judge = true;
            for (int i = 1; i <= n; i++) {
                if (grade[i] % 2 != 0) {
                    printf("0
    ");
                    judge = false;
                    break;
                }
            }
            if (judge) printf("1
    ");
        }
        return 0;
    }
  • 相关阅读:
    typescript学习记录-联合类型(14)
    typescript学习记录-元组(13)
    typescript学习记录-Map(对象)(12)
    typescript学习记录-Array(数组)(11)
    typescript学习记录-String(10)
    typescript学习记录-Number(9)
    typescript学习记录-函数(8)重点重点
    typescript学习记录-循环(7)
    SQL注入基础知识及绕过方式
    暴力破解攻击方式及思路
  • 原文地址:https://www.cnblogs.com/steamedbun/p/5740711.html
Copyright © 2011-2022 走看看