-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeTooFar.java
More file actions
94 lines (77 loc) · 2.32 KB
/
NodeTooFar.java
File metadata and controls
94 lines (77 loc) · 2.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class NodeTooFar {
public static int totalNodes;
public static HashSet<Integer> seenSet;
public static void main(String[] args) throws IOException{
Scanner scan = new Scanner(System.in);
//Scanner scan = new Scanner(new File("testfile.txt"));
int caseNum = 0;
seenSet = new HashSet<Integer>();
//continuous structure
while(scan.hasNext()) {
int numConnections = scan.nextInt();
if(numConnections == 0) {
break; //end case
}
HashMap<Integer, ArrayList<Integer>> connections = new HashMap<>();// store connections 35: [15, 40, 55]
for(int x = 0; x < numConnections; x++) {
int first = scan.nextInt();
int second = scan.nextInt();
//CASES FOR INSERTING CONNECTIONS
if(connections.containsKey(first)) {
connections.get(first).add(second);
}
else {
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(second);
connections.put(first, li);
}
if(connections.containsKey(second)) {
connections.get(second).add(first);
}
else {
ArrayList<Integer> li = new ArrayList<Integer>();
li.add(first);
connections.put(second, li);
}
}
while(scan.hasNext()) { //query section, terminated by 0 0
int startNode = scan.nextInt();
int ttl = scan.nextInt();
if(startNode == 0 && ttl == 0) { //end case
break;
}
caseNum++;
totalNodes = connections.size();
seenSet.clear();
seenSet.add(startNode);
recursive(connections, ttl, startNode, startNode);
int unreach = totalNodes - seenSet.size();
System.out.println("Case " + caseNum + ": " + unreach + " nodes not reachable from node " + startNode+" with TTL = "+ttl+".");
}
}
}
public static void recursive( HashMap<Integer, ArrayList<Integer>> connections, int ttl, int currentNode, int initialNode){ //, HashSet<Integer> seenSet) {
//base case, -1 ttl
if(ttl < 0) {
return ;
}
//update case
ttl--;
seenSet.add(currentNode);
//recursive send to all nodes attached
//HashSet<Integer> retSet = new HashSet<Integer>();
for(Integer i : connections.get(currentNode)) {
if(!seenSet.contains(i))
{
recursive(connections,ttl,i,initialNode) ;
//System.out.println(i);
}
}
return ;
}
}