Here you can find the java code and photo of challenge:
import java.io.*;import java.math.*;import java.security.*;import java.text.*;import java.util.*;import java.util.concurrent.*;import java.util.function.*;import java.util.regex.*;import java.util.stream.*;
public class Solution {
public static class DirectedGraph {/* Adjacency List representation of the given graph */private Map> adjList = new HashMap>();public String toString() {StringBuffer s = new StringBuffer();for (Integer v : adjList.keySet())s.append("\n " + v + " -> " + adjList.get(v));return s.toString();}public void add(Integer vertex) {if (adjList.containsKey(vertex))return;adjList.put(vertex, new ArrayList());}public void add(Integer source, Integer dest) {add(source);add(dest);adjList.get(source).add(dest);}/* Indegree of each vertex as a Map */public Map inDegree() {Map result = new HashMap();for (Integer v : adjList.keySet())result.put(v, 0);for (Integer from : adjList.keySet()) {for (Integer to : adjList.get(from)) {result.put(to, result.get(to) + 1);}}return result;}public Map outDegree () {Map result = new HashMap();for (Integer v: adjList.keySet()) result.put(v, adjList.get(v).size());return result;}}// Complete the isDAG function below.public static boolean isDag(DirectedGraph digraph) {}public static void main(String[] args) throws IOException {BufferedWriter bufferredWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));BufferedReader bufferredReader = new BufferedReader(new InputStreamReader(System.in));DirectedGraph digraph = new DirectedGraph();String line;while ((line = bufferredReader.readLine()) != null) {String[] v = line.split(" ");digraph.add(Integer.parseInt(v[0]), Integer.parseInt(v[1]));}bufferredWriter.write((isDag(digraph) ? "1" : "0"));bufferredReader.close();bufferredWriter.close();}}Extracted text: Given a directed graph as an adjacency list, you need to determine whether it is acyclic or not. Input Format • Each of the lines in the input contains two space-separated integers, sand d, that represents an edge from the node sto d. Constraints Node numbers are 0 based, i.e. if there n nodes, the nodes are numnered as 0,1,2.,n-1. Output Format The output of the program is 0 or 1, depending on the result of the isDag() function. If it returns TRUE, output is 1, otherwise it is 0. Sample Input 0 1 2 13 2 3 2 4 4 5 5 6 Sample Output 0 1 Explanation 0 The example graph in this example is (1 3 Since there is no cycle in the graph, the result is 1 4. 2.
public String toString() {StringBuffer s = new StringBuffer();for (Integer v : adjList.keySet())s.append("\n " + v + " -> " + adjList.get(v));return s.toString();}
public void add(Integer vertex) {if (adjList.containsKey(vertex))return;adjList.put(vertex, new ArrayList());}public void add(Integer source, Integer dest) {add(source);add(dest);adjList.get(source).add(dest);}/* Indegree of each vertex as a Map */public Map inDegree() {Map result = new HashMap();for (Integer v : adjList.keySet())result.put(v, 0);for (Integer from : adjList.keySet()) {for (Integer to : adjList.get(from)) {result.put(to, result.get(to) + 1);}}return result;}public Map outDegree () {Map result = new HashMap();for (Integer v: adjList.keySet()) result.put(v, adjList.get(v).size());return result;}}// Complete the isDAG function below.public static boolean isDag(DirectedGraph digraph) {}public static void main(String[] args) throws IOException {BufferedWriter bufferredWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));BufferedReader bufferredReader = new BufferedReader(new InputStreamReader(System.in));DirectedGraph digraph = new DirectedGraph();String line;while ((line = bufferredReader.readLine()) != null) {String[] v = line.split(" ");digraph.add(Integer.parseInt(v[0]), Integer.parseInt(v[1]));}bufferredWriter.write((isDag(digraph) ? "1" : "0"));bufferredReader.close();bufferredWriter.close();}}Extracted text: Given a directed graph as an adjacency list, you need to determine whether it is acyclic or not. Input Format • Each of the lines in the input contains two space-separated integers, sand d, that represents an edge from the node sto d. Constraints Node numbers are 0 based, i.e. if there n nodes, the nodes are numnered as 0,1,2.,n-1. Output Format The output of the program is 0 or 1, depending on the result of the isDag() function. If it returns TRUE, output is 1, otherwise it is 0. Sample Input 0 1 2 13 2 3 2 4 4 5 5 6 Sample Output 0 1 Explanation 0 The example graph in this example is (1 3 Since there is no cycle in the graph, the result is 1 4. 2.
public void add(Integer source, Integer dest) {add(source);add(dest);adjList.get(source).add(dest);}
/* Indegree of each vertex as a Map */public Map inDegree() {Map result = new HashMap();for (Integer v : adjList.keySet())result.put(v, 0);for (Integer from : adjList.keySet()) {for (Integer to : adjList.get(from)) {result.put(to, result.get(to) + 1);}}return result;}public Map outDegree () {Map result = new HashMap();for (Integer v: adjList.keySet()) result.put(v, adjList.get(v).size());return result;}}// Complete the isDAG function below.public static boolean isDag(DirectedGraph digraph) {}public static void main(String[] args) throws IOException {BufferedWriter bufferredWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));BufferedReader bufferredReader = new BufferedReader(new InputStreamReader(System.in));DirectedGraph digraph = new DirectedGraph();String line;while ((line = bufferredReader.readLine()) != null) {String[] v = line.split(" ");digraph.add(Integer.parseInt(v[0]), Integer.parseInt(v[1]));}bufferredWriter.write((isDag(digraph) ? "1" : "0"));bufferredReader.close();bufferredWriter.close();}}Extracted text: Given a directed graph as an adjacency list, you need to determine whether it is acyclic or not. Input Format • Each of the lines in the input contains two space-separated integers, sand d, that represents an edge from the node sto d. Constraints Node numbers are 0 based, i.e. if there n nodes, the nodes are numnered as 0,1,2.,n-1. Output Format The output of the program is 0 or 1, depending on the result of the isDag() function. If it returns TRUE, output is 1, otherwise it is 0. Sample Input 0 1 2 13 2 3 2 4 4 5 5 6 Sample Output 0 1 Explanation 0 The example graph in this example is (1 3 Since there is no cycle in the graph, the result is 1 4. 2.
public Map outDegree () {Map result = new HashMap();for (Integer v: adjList.keySet()) result.put(v, adjList.get(v).size());return result;}}// Complete the isDAG function below.public static boolean isDag(DirectedGraph digraph) {}public static void main(String[] args) throws IOException {BufferedWriter bufferredWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));BufferedReader bufferredReader = new BufferedReader(new InputStreamReader(System.in));DirectedGraph digraph = new DirectedGraph();String line;while ((line = bufferredReader.readLine()) != null) {String[] v = line.split(" ");digraph.add(Integer.parseInt(v[0]), Integer.parseInt(v[1]));}bufferredWriter.write((isDag(digraph) ? "1" : "0"));bufferredReader.close();bufferredWriter.close();}}Extracted text: Given a directed graph as an adjacency list, you need to determine whether it is acyclic or not. Input Format • Each of the lines in the input contains two space-separated integers, sand d, that represents an edge from the node sto d. Constraints Node numbers are 0 based, i.e. if there n nodes, the nodes are numnered as 0,1,2.,n-1. Output Format The output of the program is 0 or 1, depending on the result of the isDag() function. If it returns TRUE, output is 1, otherwise it is 0. Sample Input 0 1 2 13 2 3 2 4 4 5 5 6 Sample Output 0 1 Explanation 0 The example graph in this example is (1 3 Since there is no cycle in the graph, the result is 1 4. 2.
// Complete the isDAG function below.public static boolean isDag(DirectedGraph digraph) {}
public static void main(String[] args) throws IOException {BufferedWriter bufferredWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
BufferedReader bufferredReader = new BufferedReader(new InputStreamReader(System.in));
DirectedGraph digraph = new DirectedGraph();String line;while ((line = bufferredReader.readLine()) != null) {String[] v = line.split(" ");digraph.add(Integer.parseInt(v[0]), Integer.parseInt(v[1]));}
bufferredWriter.write((isDag(digraph) ? "1" : "0"));bufferredReader.close();bufferredWriter.close();}}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here