算法笔记_141:无向图的欧拉回路判断问题(Java)
目录
1 问题描述
束。
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 3
1 2
1 3
2 3
3 2
1 2
2 3
0
1
0
参考资料:
1. 欧拉回路
算法笔记_141:无向图的欧拉回路判断问题(Java)的更多相关文章
- 算法笔记_142:无向图的欧拉回路求解(Java)
目录 1 问题描述 2 解决方案 1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8 ...
- 算法笔记_147:有向图欧拉回路判断应用(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Description In order to make their sons brave, Jiajia and Wind take them t ...
- Java实现无向图的欧拉回路判断问题
1 问题描述 Problem Description 欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是否存在欧拉回路? Input 测试输入包含若干测试 ...
- 算法笔记_044:表达式计算求值(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的 ...
- 算法笔记_052:蓝桥杯练习Multithreading(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 现有如下一个算法: repeat ni times yi := y y := yi+1 end repeat 令n[1]为你需要算加法的第 ...
- 算法笔记_180:历届试题 国王的烦恼(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 C国由n个小岛组成,为了方便小岛之间联络,C国在小岛间建立了m座大桥,每座大桥连接两座小岛.两个小岛间可能存在多座桥连接.然而,由于海水冲 ...
- 算法笔记_149:图论之桥的应用(Java)
目录 1 问题描述 2 解决方案 1 问题描述 1310 One-way traffic In a certain town there are n intersections connected ...
- 算法笔记_139:二分图的最大权匹配(Java)
目录 1 问题描述 2 解决方案 1 问题描述 何为二分图的最大权匹配问题? 最大权二分匹配问题就是给二分图的每条边一个权值,选择若干不相交的边,得到的总权值最大. 2 解决方案 对于此问题的讲解 ...
- 算法笔记_133:最大连续乘积子数组(Java)
目录 1 问题描述 2 解决方案 2.1 蛮力法 2.2 动态规划法 1 问题描述 给定一个浮点数组,任意取出数组中的若干个连续的数相乘,请找出其中乘积最大的子数组. 2 解决方案 2.1 蛮力法 ...
随机推荐
- bzoj 1008 组合计数
正难则反 前面定后面就定->枚举开头 /************************************************************** Problem: 1008 ...
- SHELL异常处理(转载)
写SHELL好久了,经常被异常困扰,可竟然坚持了若干年没用过,回想以前服务过的公司,阿弥陀佛,罪过罪过.废话少说,希望此篇文章可以协助大家和我彻底结束SHELL脚本就是LINUX命令集合的初级阶段. ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造
C. Bear and Forgotten Tree 3 题目连接: http://www.codeforces.com/contest/658/problem/C Description A tre ...
- CROC 2016 - Qualification B. Processing Queries 模拟
B. Processing Queries 题目连接: http://www.codeforces.com/contest/644/problem/B Description In this prob ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- 【美】范·K·萨普曼 - 通向财务自由之路(2013年11月26日)
<通向财务自由之路> 作 者:[美]范·K·萨普曼 译 者:董梅 系 列: 出 版:机械工业出版社 字 数:约40千字 阅读完成:2013年11月26日
- C#获取picturebox图片路径
path = ofd.FileName; filename = ofd.SafeFileName; lu="E:\\鹿瑶\\Csharp\\DB学生报名系统\\baomingDemo\\bi ...
- Windows 10 UWP程序标题栏设置
在Windows 10程序中,以前只能用于全屏方式的Metro程序现在可以运行在窗口模式下了,并且改了个新名字,叫Windows 通用程序(Universal Windows app),简称UWP程序 ...
- POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题
http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...
- Druid 配置_LogFilter
Druid内置提供了四种LogFilter(Log4jFilter.Log4j2Filter.CommonsLogFilter.Slf4jLogFilter),用于输出JDBC执行的日志.这些Filt ...