算法笔记_147:有向图欧拉回路判断应用(Java)
目录
1 问题描述
Description
Input
The first line for each case contains two integers n, m(0 < n < 1001,m < 6000), the number of rooms and corridors in the cave. The next m lines each contains two integers u and v, indicating that there is a corridor connecting room u and room v directly.
Output
Sample Input
1
3 3
1 2
2 3
3 1
Sample Output
Yes
Source
2 解决方案
具体代码如下:
package com.liuzhen.practice; import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack; public class Main {
public static int n; //顶点数
public static int count;
public static int[] DFN;
public static int[] Low;
public static boolean[] inStack;
public static int group; //强连通分量组
public static int[] belong;
public static Stack<Integer> stack;
public static ArrayList<edge>[] map;
public static ArrayList<String> result = new ArrayList<String>(); static class edge {
public int a;
public int b; public edge(int a, int b) {
this.a = a;
this.b = b;
}
} @SuppressWarnings("unchecked")
public void init() {
count = 1;
DFN = new int[n + 1];
Low = new int[n + 1];
inStack = new boolean[n + 1];
group = 0;
belong = new int[n + 1];
stack = new Stack<Integer>();
map = new ArrayList[n + 1];
for(int i = 1;i <= n;i++) {
DFN[i] = -1;
Low[i] = -1;
inStack[i] = false;
belong[i] = -1;
map[i] = new ArrayList<edge>();
}
} public void TarJan(int start) {
DFN[start] = count++;
Low[start] = DFN[start];
inStack[start] = true;
stack.push(start);
int j = start;
for(int i = 0;i < map[start].size();i++) {
j = map[start].get(i).b;
if(DFN[j] == -1) {
TarJan(j);
Low[start] = Math.min(Low[start], Low[j]);
} else if(inStack[j]) {
Low[start] = Math.min(Low[start], DFN[j]);
}
}
if(DFN[start] == Low[start]) {
group++;
do {
j = stack.pop();
belong[j] = group;
inStack[j] = false;
} while(j != start);
}
} public boolean TopSort(ArrayList<edge>[] lessMap, int[] degree) {
int count = 0;
Stack<Integer> s = new Stack<Integer>();
for(int i = 1;i < degree.length;i++) {
if(degree[i] == 0) {
count++;
s.push(i);
}
}
if(count > 1)
return false;
while(!s.empty()) {
int start = s.pop();
count = 0;
for(int i = 0;i < lessMap[start].size();i++) {
int j = lessMap[start].get(i).b;
degree[j]--;
if(degree[j] == 0) {
count++;
s.push(j);
}
}
if(count > 1)
return false;
}
return true;
} @SuppressWarnings("unchecked")
public void getResult() {
for(int i = 1;i <= n;i++) {
if(DFN[i] == -1)
TarJan(i);
}
ArrayList<edge>[] lessMap = new ArrayList[group + 1];
int[] degree = new int[group + 1];
for(int i = 1;i <= group;i++)
lessMap[i] = new ArrayList<edge>();
for(int i = 1;i < map.length;i++) {
for(int j = 0;j < map[i].size();j++) {
int a = map[i].get(j).a;
int b = map[i].get(j).b;
if(belong[a] != belong [b]) {
lessMap[belong[a]].add(new edge(belong[a], belong[b]));
degree[belong[b]]++;
}
}
}
if(TopSort(lessMap, degree)) {
result.add("Yes");
} else {
result.add("No");
}
return;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t > 0) {
t--;
n = in.nextInt();
int k = in.nextInt();
test.init();
for(int i = 0;i < k;i++) {
int a = in.nextInt();
int b = in.nextInt();
map[a].add(new edge(a, b));
}
test.getResult();
}
for(int i = 0;i < result.size();i++)
System.out.println(result.get(i));
}
}
运行结果:
1
3 3
1 2
2 3
3 1
Yes
参考资料:
1. 欧拉回路
2. POJ2762 Going from u to v or from v to u?(强连通分量缩点+拓扑排序)
算法笔记_147:有向图欧拉回路判断应用(Java)的更多相关文章
- 算法笔记_148:有向图欧拉回路求解(Java)
目录 1 问题描述 2 解决方案 1 问题描述 Description A catenym is a pair of words separated by a period such that t ...
- 算法笔记_030:回文判断(Java)
目录 1 问题描述 2 解决方案 1 问题描述 给定一个字符串,如何判断这个字符串是否是回文串? 所谓回文串,是指正读和反读都一样的字符串,如madam.我爱我等. 2 解决方案 解决上述问题,有 ...
- 算法笔记_144:有向图强连通分量的Tarjan算法(Java)
目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...
- 算法笔记_177:历届试题 城市建设(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 栋栋居住在一个繁华的C市中,然而,这个城市的道路大都年久失修.市长准备重新修一些路以方便市民,于是找到了栋栋,希望栋栋能帮助他. C市中有 ...
- 算法笔记_135:格子取数问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 有n*n个格子,每个格子里有正数或者0,从最左上角往最右下角走,只能向下和向右走,一共走两次(即从左上角往右下角走两趟),把所有经过的格子里的数加起 ...
- 算法笔记_045:币值最大化问题(Java)
目录 1 问题描述 2 解决方案 2.1 动态规划法 1 问题描述 给定一排n个硬币,其面值均为正整数c1,c2,...,cn,这些整数并不一定两两不同.请问如何选择硬币,使得在其原始位置互不相邻 ...
- 算法笔记_029:约瑟夫斯问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 引用自<算法设计与分析基础>第三版: 约瑟夫斯问题,是以弗拉瓦斯.约瑟夫斯(Flavius Josephus)的名字命名的.约瑟夫斯是一 ...
- 算法笔记_024:字符串的包含(Java)
目录 1 问题描述 2 解决方案 2.1 蛮力轮询法 2.2 素数相乘法 2.3 位运算法 1 问题描述 给定一长字符串A和一短字符串B.请问,如何最快地判断出短字符串B中的所有字符是否都在长字符串A ...
- 算法笔记_051:荷兰国旗问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 现有n个红白蓝三种不同颜色的小球,乱序排列在一起,请通过两两交换任意两个球,使得从左至右的球依次为红球.白球.蓝球.这个问题之所以叫荷兰国旗,是因为 ...
随机推荐
- 【BZOJ 2728】 2728: [HNOI2012]与非 (线性基?)
2728: [HNOI2012]与非 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 813 Solved: 389 Description Inpu ...
- android intent 传递 二进制数据
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 实现序列化.
- 【51Nod 1815】【算法马拉松 23】调查任务
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1815 tarjan缩点后在DAG上递推即可. 每个点维护所有根到它的路径 ...
- 【hash】BZOJ3751-[NOIP2014]解方程
[题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少 ...
- 25.最小生成树(kruskal算法)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立 ...
- Memcached源码分析——连接状态变化分析(drive_machine)
这篇文章主要介绍Memcached中,基于libevent构造的主线程和worker线程所处理连接的状态互相转换的过程(不涉数据的存取等操作),也就是drive_machine的主要业务逻辑了.状态转 ...
- jq设置样式
单个样式: $(this).css("color","red"); 多个样式: $(this).css({color:"red",backg ...
- EF DBContext中DbSet中Hashset添加对象后,DataGrid UI没有刷新的问题
使用EF4/5添加数据库视图生成DBContext,如果数据表/对象之间有M-N对应关系,EF对自动添加引用类集合,是Hashset类型.由于不是ObservableCellection类型,在引用类 ...
- iOS: 删除真机测试的Provisioning Profile后,在Code Singing中出现entitlements.plist文件无效,解决办法如下:
问题主题:method to The entitlements specified in your application’s Code Signing Entitlements file do no ...
- Orchard运用 - 理解App_Data目录结构
了解一个系统,应该基本上要了解目录结构及其组织形式.这样对于开发人员更是必备的知识,比如开发模块最终安装到哪,主题Themes是如何配置启用. 今天跟大家分享其实是个笔记记录,就是看到有一篇文章介绍A ...