This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class FastReader | |
{ | |
BufferedReader br; | |
StringTokenizer st; | |
public FastReader() | |
{ | |
br = new BufferedReader(new | |
InputStreamReader(System.in)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class EdgeWeightedGraph { | |
ArrayList<ArrayList<Edge>> adj; | |
int noOfVertices; | |
public EdgeWeightedGraph(int v) { | |
this.adj = new ArrayList<ArrayList<Edge>>(v + 1); | |
this.noOfVertices = v+1; | |
// Vertices start from 1, ignore the list at index 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class CycleDetector { | |
ArrayList<ArrayList<Edge>> adj; | |
boolean[] marked; | |
boolean hasCycle = false; | |
public CycleDetector(ArrayList<ArrayList<Edge>> adj) { | |
this.adj = adj; | |
marked = new boolean[adj.size()]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class DFS{ | |
boolean[] marked; | |
public DFS(EdgeWeightedGraph G, int source){ | |
marked = new boolean[G.noOfVertices]; | |
dfs(G,source); | |
} | |
public void dfs(EdgeWeightedGraph G, int v){ | |
marked[v] = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class BFS { | |
boolean[] marked; | |
int[] distTo; | |
public BFS(EdgeWeightedGraph G, int source) { | |
marked = new boolean[G.noOfVertices]; | |
distTo = new int[G.noOfVertices]; | |
for(int i=0; i<G.noOfVertices; i++ ){ | |
distTo[i] = Integer.MAX_VALUE ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class BFS { | |
boolean[] marked; | |
int[] distTo; | |
int sink; | |
public BFS(EdgeWeightedGraph G, int source, int destination) { | |
marked = new boolean[G.noOfVertices]; | |
distTo = new int[G.noOfVertices]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class ConnectedComponents{ | |
boolean[] marked; | |
int[] id; | |
int count; | |
public ConnectedComponents(EdgeWeightedGraph G){ | |
marked = new boolean[G.noOfVertices]; | |
id = new int[G.noOfVertices]; | |
count = 1; | |
for(int i=1; i<G.noOfVertices; i++){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class Dijkstra { | |
int source; | |
int v; | |
int[] distTo; | |
boolean[] marked; | |
Edge[] edgeTo; | |
EdgeWeightedGraph G; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// USAGE: FactorialPoorMans F = new FactorialPoorMans(); | |
// String n! = F.factorial(n); | |
static class FactorialPoorMans { | |
public FactorialPoorMans() { | |
} | |
private long N; | |
public String factorial(int n) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BTtoBST | |
{ | |
static ArrayList<Integer> inOrder(Node root, ArrayList<Integer> orig){ | |
if(root==null) return orig; | |
inOrder(root.left, orig); | |
orig.add(root.data); | |
inOrder(root.right, orig); | |
return orig; |
OlderNewer