-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplicationGraph.java
More file actions
69 lines (59 loc) · 2.18 KB
/
ImplicationGraph.java
File metadata and controls
69 lines (59 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* @author Chandan Singh
*
*/
public class ImplicationGraph {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in);) {
int testCaseCount = sc.nextInt();
sc.nextLine();
if(testCaseCount == 0) {System.exit(0);} ;
for (int caseCount = 0; caseCount < testCaseCount; caseCount++) {
String nextLine = sc.nextLine();
String[] numbers = nextLine.split(" ");
int stmtCount = Integer.valueOf(numbers[0]);
int implicationsCount = Integer.valueOf(numbers[1]);
if(implicationsCount == 0)
{
System.out.println(stmtCount);
continue;
}
else
{
final Map<Integer, Integer> inDegree = new HashMap<>(stmtCount);
final Map<Integer, Integer> outDegree = new HashMap<>(stmtCount);
initializeDegrees(inDegree, outDegree,stmtCount);
for(int implication = 0; implication < implicationsCount ; implication++)
{
String graphInfoLine = sc.nextLine();
String[] implicationStatement = graphInfoLine.split(" ");
int fromVertex = Integer.valueOf(implicationStatement[0]);
int toVertex = Integer.valueOf(implicationStatement[1]);
outDegree.put(fromVertex, (outDegree.get(fromVertex)+1));
inDegree.put(toVertex, (inDegree.get(toVertex)+1));
}
long additionalImplicationsReqd = findAdditionalImplications(outDegree,inDegree);
System.out.println(additionalImplicationsReqd);
}
}
}
}
private static long findAdditionalImplications(Map<Integer, Integer> outDegree, Map<Integer, Integer> inDegree) {
long vertexWithZeroOutDegree = findKeysWithZeroCount(outDegree);
long vertexWithZeroInDegree = findKeysWithZeroCount(inDegree);
return Math.max(vertexWithZeroInDegree,vertexWithZeroOutDegree);
}
private static long findKeysWithZeroCount(Map<Integer, Integer> graphMap) {
return graphMap.values().stream().filter(v -> v.equals(0)).count();
}
private static void initializeDegrees(Map<Integer, Integer> inDegree, Map<Integer, Integer> outDegree, int statmentCount) {
for(int vertex= 1; vertex <= statmentCount; vertex++)
{
inDegree.put(vertex, 0);
outDegree.put(vertex, 0);
}
}
}