算法笔记_146:TarJan算法的应用(Java)
目录
1 问题描述
Problem Description
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 int[] inStack;
public static ArrayList<edge>[] map;
public static Stack<Integer> stack;
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 int[n + 1];
map = new ArrayList[n + 1];
stack = new Stack<Integer>();
for(int i = 1;i <= n;i++) {
DFN[i] = -1;
Low[i] = -1;
inStack[i] = -1;
map[i] = new ArrayList<edge>();
}
} public boolean TarJan(int start) {
DFN[start] = count++;
Low[start] = DFN[start];
inStack[start] = start;
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] != -1) {
Low[start] = Math.min(Low[start], DFN[j]);
}
}
if(DFN[start] == Low[start]) {
int num = 0;
do {
j = stack.pop();
num++;
} while(j != start);
if(num == DFN.length - 1)
return true;
}
return false;
} public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
while(true) {
n = in.nextInt(); //图中顶点数
int k = in.nextInt(); // 图中边数
if(n == 0 || k == 0)
break;
test.init();
int start = 1;
for(int i = 0;i < k;i++) {
int a = in.nextInt();
int b = in.nextInt();
map[a].add(new edge(a, b));
start = a;
}
if(test.TarJan(start))
result.add("Yes");
else
result.add("No");
}
for(int i = 0;i < result.size();i++) {
System.out.println(result.get(i));
}
}
}
运行结果:
3 3
1 2
2 3
3 1
3 3
1 2
2 3
3 2
0 0
Yes
No
参考资料:
算法笔记_146:TarJan算法的应用(Java)的更多相关文章
- 算法笔记_071:SPFA算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 具体编码 1 问题描述 何为spfa(Shortest Path Faster Algorithm)算法? spfa算法功能:给定一个加权连通图,选取一个 ...
- 算法笔记之KMP算法
本文是<算法笔记>KMP算法章节的阅读笔记,文中主要内容来源于<算法笔记>.本文主要介绍了next数组.KMP算法及其应用以及对KMP算法的优化. KMP算法主要用于解决字符串 ...
- 算法笔记_066:Kruskal算法详解(Java)
目录 1 问题描述 2 解决方案 2.1 构造最小生成树示例 2.2 伪码及时间效率分析 2.3 具体编码(最佳时间效率) 1 问题描述 何为Kruskal算法? 该算法功能:求取加权连通图的最小 ...
- 算法笔记_054:Prim算法(Java)
目录 1 问题描述 2 解决方案 2.1 贪心法 1 问题描述 何为Prim算法? 此处引用网友博客中一段介绍(PS:个人感觉网友的这篇博客对于Prim算法讲解的很清楚,本文与之相区别的地方在于具 ...
- 算法学习笔记:Tarjan算法
在上一篇文章当中我们分享了强连通分量分解的一个经典算法Kosaraju算法,它的核心原理是通过将图翻转,以及两次递归来实现.今天介绍的算法名叫Tarjan,同样是一个很奇怪的名字,奇怪就对了,这也是以 ...
- 算法笔记--lca倍增算法
算法笔记 模板: vector<int>g[N]; vector<int>edge[N]; ][N]; int deep[N]; int h[N]; void dfs(int ...
- Tarjan 算法求 LCA / Tarjan 算法求强连通分量
[时光蒸汽喵带你做专题]最近公共祖先 LCA (Lowest Common Ancestors)_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili tarjan LCA - YouTube Tarj ...
- 算法笔记_177:历届试题 城市建设(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 栋栋居住在一个繁华的C市中,然而,这个城市的道路大都年久失修.市长准备重新修一些路以方便市民,于是找到了栋栋,希望栋栋能帮助他. C市中有 ...
- 算法笔记_135:格子取数问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 有n*n个格子,每个格子里有正数或者0,从最左上角往最右下角走,只能向下和向右走,一共走两次(即从左上角往右下角走两趟),把所有经过的格子里的数加起 ...
随机推荐
- CodeForces 1063B. Labyrinth 性质
给定$n *m$的格子 询问从$(r, c)$开始最多向左走$x$步,向右走$y$步 询问有多少个格子可以从$(r, c)$到达 有障碍物,$n, m \leqslant 2 * 10^3$ 对于一个 ...
- [原]Android Studio导入外部项目找不到对应的sdk解决办法
示例项目:JPushExample(349872) 打开项目的文件夹目录,找到:JPushExample(349872)\app\build.gradle打开,将里面的 compileSdkVersi ...
- CountDownLatch CyclicBarrier Semaphore 比较
document CountDownLatch A synchronization aid that allows one or more threads to wait until a set of ...
- Vue学习记录-画页面
webstorm 因为之前开发ReactNative的时候,选择了webstorm,这回转战Vue,自然还是用它.如果什么也不做的话,打开Vue工程,编辑区域基本上没有语法高亮.怎么办呢? 安装插件( ...
- SharePoint 2013 排错之"Code blocks are not allowed in this file"
今天,设置页面布局的自己定义母版页时,设置完了以后保存,然后预览报错,错误例如以下截图:删掉自己定义母版页的MasterPageFile属性,页面依旧报错:感觉甚是奇怪,由于有版本号控制,还原为最初的 ...
- Spartan-6 FPGA Configuration
These configuration pins serve as the interface for a number of different configuration modes: • JTA ...
- Read UNIQUE ID and flash size method for stm32
/* 读取stm32的unique id 与 flash size */ /* func: unsigned int Read_UniqueID_Byte(unsigned char offset) ...
- Using TXMLDocument, Working with XML Nodes
Using TXMLDocument The starting point for working with an XML document is the Xml.XMLDoc.TXMLDocumen ...
- jquery.lazyload.js实现图片懒载入
个人理解:将须要延迟载入的图片的src属性所有设置为一张同样尽可能小(目的是尽可能的少占宽带,节省流量,因为缓存机制,当浏览器载入了一张图片之后,同样的图片就会在缓存中拿.不会又一次到server上拿 ...
- Your first NHibernate based application
Welcome to NHibernate If you're reading this, we assume that you've just downloaded NHibernate and w ...