hdu 1269 迷宫城堡 java
来源:互联网 发布:对冲平仓 知乎 编辑:程序博客网 时间:2024/05/18 01:36
import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;import java.util.Stack;class Main { private int numOfNode; private List< ArrayList<Integer> > graph;//图 private List< ArrayList<Integer> > result;//保存极大强连通图 private boolean[] inStack; private Stack<Integer> stack; private int[] dfn; private int[] low; private int time; public static void main(String[] args) { Scanner in = new Scanner(System.in ); while(in.hasNext()){ int n=in.nextInt(); int m=in.nextInt(); if(n==0&&m==0){ break; } List< ArrayList<Integer> > graph = new ArrayList<ArrayList<Integer>>(); for(int i=0;i<n;i++){ graph.add(new ArrayList<Integer>()); } for(int i=0;i<m;i++){ int a=in.nextInt()-1; int b=in.nextInt()-1; graph.get(a).add(b); } Main t = new Main(graph, n); List< ArrayList<Integer> > result = t.run(); //打印结果 int sum=0; for(int i=0;i<result.size();i++){ if(sum<result.get(i).size()){ sum=result.get(i).size(); } } if(sum==n){ System.out.println("Yes"); }else{ System.out.println("No"); } } } public Main(List< ArrayList<Integer> > graph,int numOfNode){ this.graph = graph; this.numOfNode = numOfNode; this.inStack = new boolean[numOfNode]; this.stack = new Stack<Integer>(); dfn = new int[numOfNode]; low = new int[numOfNode]; Arrays.fill(dfn, -1);//将dfn所有元素都置为-1,其中dfn[i]=-1代表i还有没被访问过。 Arrays.fill(low, -1); result = new ArrayList<ArrayList<Integer>>(); } public List< ArrayList<Integer> > run(){ for(int i=0;i<numOfNode;i++){ if(dfn[i]==-1){ tarjan(i); } } return result; } public void tarjan(int current){ dfn[current]=low[current]=time++; inStack[current]=true; stack.push(current); for(int i=0;i<graph.get(current).size();i++){ int next = graph.get(current).get(i); if(dfn[next]==-1){ tarjan(next); low[current]=Math.min(low[current], low[next]); }else if(inStack[next]){ low[current]=Math.min(low[current], dfn[next]); } } if(low[current]==dfn[current]){ ArrayList<Integer> temp =new ArrayList<Integer>(); int j = -1; while(current!=j){ j = stack.pop(); inStack[j]=false; temp.add(j); } result.add(temp); } } }
阅读全文
1 0
- hdu 1269 迷宫城堡 java
- hdu-1269-迷宫城堡-Tarjan算法-java
- hdu 1269 迷宫城堡
- HDU 1269 迷宫城堡
- HDU 1269 - 迷宫城堡
- HDU 1269 迷宫城堡
- hdu 1269 迷宫城堡
- hdu 1269 迷宫城堡
- HDU 1269 迷宫城堡
- HDU 1269 迷宫城堡
- hdu 1269 迷宫城堡
- HDU 1269 迷宫城堡
- HDU - 1269 迷宫城堡
- hdu 1269 迷宫城堡
- hdu 1269 迷宫城堡
- hdu 1269 迷宫城堡
- 【HDU 1269】迷宫城堡
- hdu 1269 迷宫城堡
- 使用JDBC进行数据库的事务操作(2)
- Android 优化之 纹理
- linux中vi怎么根据关键词查找
- inout用法浅析
- 综合---有限状态机实例(附带普通状态模式)
- hdu 1269 迷宫城堡 java
- top命令动态监控进程
- 配置数据源
- 五分钟学GIS | iMobile for ReactNative安装及使用
- android studio ndk 资料整理
- Eclipse使用EGit插件连接Github的设置(一):Push篇
- ROS Robotics Projects(9)虚拟现实VR头戴设备和Leap Motion手势操控
- Re:从0开始的微服务架构:(一)重识微服务架构 http://www.infoq.com/cn/articles/micro-service-architecture-from-zero#0-you
- 通过jQuery Ajax使用FormData对象上传文件