除孤立点外图连通且顶点度为偶数即可构成欧拉回路

欧拉回路

https://www.nowcoder.com/practice/0ba5d8f525494a8787aaa9d53b5f9b9e

import java.util.Scanner;
public class Main {
    private static int[] parents;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Label:
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            if (n == 0) break;
            parents = new int[n + 1];
            int[] degrees = new int[n + 1];
            for (int i = 1; i <= n; i++) parents[i] = i;
            int m = sc.nextInt();
            while (m-- > 0) {
                int a = sc.nextInt();
                int b = sc.nextInt();
                degrees[a]++;
                degrees[b]++;
                if (find(a) != find(b)) {
                    parents[find(a)] = find(b);
                }
            }
            for (int i = 1; i <= n; i++) {
                if (degrees[i] != 0 && find(i) != find(1) || degrees[i] % 2 != 0) {
                    System.out.println(0);
                    continue Label;
                }
            }
            System.out.println(1);
        }
    }
    private static int find(int x) {
        return x == parents[x] ? x : (parents[x] = find(parents[x]));
    }
}

全部评论

相关推荐

05-07 19:10
已编辑
中国科学技术大学 C++
silly01:现在先去 momenta,8-9月去鹅找日常实习,八股文算法背好了你这随便进。不过建议补充一下后端知识,MySQL、Redis看下八股,再补个6824,加点go后台的技术栈,9月随便进大厂。CPP后端只能来WXG
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 18:13
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务