1 问题描述

Problem 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

2 解决方案

package com.liuzhen.practice;

import java.util.ArrayList;
import java.util.Scanner; public class Main {
public static int MAX = 1000;
public static int[][] map = new int[MAX][MAX]; //输入图
public static ArrayList<Integer> result = new ArrayList<Integer>(); //用于存放最终输出结果 //判断给定图的每个顶点的度是否均为偶数
public boolean judge(int[] degree) {
for(int i = 0;i < degree.length;i++) {
if(degree[i] % 2 != 0)
return false;
}
return true;
} //使用BFS遍历,判断给定图是否为连通图
public boolean bfs(int n) {
boolean[] used = new boolean[n];
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0);
used[0] = true;
while(!list.isEmpty()) {
int temp = list.get(0);
list.remove(0);
for(int i = 0;i < n;i++) {
if(!used[i] && map[temp][i] != 0) {
used[i] = true;
list.add(i);
}
}
}
for(int i = 0;i < n;i++) {
if(used[i] == false)
return false;
}
return true;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
while(true) {
int n = in.nextInt(); //输入图的顶点数
if(n == 0)
break;
int m = in.nextInt(); //输入图的边数目
int[] degree = new int[n]; //用于计算输入图的每个顶点的度
for(int i = 0;i < m;i++) {
int a = in.nextInt();
int b = in.nextInt();
map[a - 1][b - 1] = 1;
map[b - 1][a - 1] = 1;
degree[a - 1]++;
degree[b - 1]++;
}
if(test.judge(degree) && test.bfs(n))
result.add(1);
else
result.add(0);
}
for(int i = 0;i < result.size();i++)
System.out.println(result.get(i));
}
}

运行结果:

3
2
3
3
2
2
3
1

Java实现无向图的欧拉回路判断问题的更多相关文章

  1. 算法笔记_141:无向图的欧拉回路判断问题(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回 ...

  2. hdu 1878 无向图的欧拉回路

    原题链接 hdu1878 大致题意: 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个无向图,问是否存在欧拉回路? 思路: 无向图存在欧拉回路的条件:1.图是连 ...

  3. SGU---101 无向图的欧拉回路

    题目链接: https://cn.vjudge.net/problem/SGU-101 题目大意: 给定你n张骨牌,每张牌左右两端有一个数字,每张牌的左右两端数字可以颠倒,找出一种摆放骨牌的顺序,使得 ...

  4. Java程序流程控制:判断结构、选择结构、循环结构

    本文内容: 判断结构 if 选择结构 switch 循环结构 while do-while for for each break.continue return 首发时间:2017-06-22 21: ...

  5. “全栈2019”Java多线程第九章:判断线程是否存活isAlive()详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  6. 为什么java里面经常作List判断的时候,既要判断list不为null,又要判断size>0呢?

    没有考虑到具体的问题上面,我们单纯的来讲: 为什么java里面经常作List判断的时候,既要判断list不为null,又要判断size>0呢? list == null 说明list没有初始化( ...

  7. 算法笔记_143:构造无向图的欧拉回路(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 具体链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

  8. 算法笔记_142:无向图的欧拉回路求解(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8 ...

  9. 算法笔记_147:有向图欧拉回路判断应用(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 Description In order to make their sons brave, Jiajia and Wind take them t ...

随机推荐

  1. [codeforces-542-C]YY?

    链接:http://codeforces.com/problemset/problem/542/C 题意:对一个函数f(x),定义域[1,n], 令f(k,x) = f(f(f(f...f(x)))) ...

  2. linux-rpm强制安装跳过依赖包

    [root@localhost ~]# rpm -ivh tigervnc-1.10.80-4.20200317git8b4be5fd.el7.x86_64.rpm --nodeps --force ...

  3. android 数据库是否该关闭

    关于android多线程数据读写请看博客: android 多线程数据库读写 常常纠结于获取了SQLiteDatabase每次操作完是否要关闭的问题,每次关闭又怕影响性能,这里记录下SQLiteOpe ...

  4. vue中使用mixins

    Mixins (混合或混入)——定义的是一个对象 1.概念:一种分发Vue组件可复用功能的非常灵活的方式.混入对象可以包含任意组件选项(组件选项:data.watch.computed.methods ...

  5. Codeforces1176A(A题)Divide it!

    Divide it! You are given an integer nn. You can perform any of the following operations with this nu ...

  6. P1251 餐巾计划问题 网络流

    P1251 餐巾计划问题 #include <bits/stdc++.h> using namespace std; typedef long long ll; , inf = 0x3f3 ...

  7. jQuery的插件和跨域、ajax

    1. 插件: 也称组件 什么是: 拥有专属的HTML,CSS和js的独立页面区域 为什么: 重用! 何时: 只要一个功能/区域可能被反复使用时 如何: 3个来源: 1. 官方插件:jquery ui ...

  8. Centos7访问Win7/Win10系统中的共享文件

    服务器挂在U盘: 1.先将U盘格式化为Fat格式或其他服务器能识别的格式. 2.使用fdisk -l,找到自己的U盘(根据盘大小) 3.新建一个文件夹(mkdir /mnt/usb)用于挂在数据 4. ...

  9. sourcetree 拉取 一直让输入密码

    以下方法都没用 在控制台中 git gc git prune git config --global credential.helper store git pull 输入账号密码 git pull ...

  10. tp5插入百万条数据处理优化

    <?php namespace app\index\controller; use think\Controller; use think\Db; class Charu extends Con ...