(UVA) A Node Too Far - Solução
If you want to see another solution for this problem, click here.
Comparing the run time between the solutions:
- The oldest solution ran in: 1.742s.
- This new solution ran in: 0.655s.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Integer>> adjList = new HashMap<Integer, ArrayList<Integer>>();
public static int bfs(int start, int ttl) {
Queue<Element> queue = new ArrayDeque<Element>();
Element e = new Element(start, 0);
queue.add(e);
HashSet<Integer> addedToQueue = new HashSet<Integer>();
addedToQueue.add(start);
int reach = 0;
while (queue.size() > 0 && queue.element().distance <= ttl) {
int currNode = queue.element().node;
ArrayList<Integer> neighbors = adjList.get(currNode);
for (int i = 0; i < neighbors.size(); i++) {
if (!addedToQueue.contains(neighbors.get(i))) {
e = new Element(neighbors.get(i), queue.element().distance+1);
queue.add(e);
addedToQueue.add(neighbors.get(i));
}
}
reach++;
queue.remove();
}
return reach;
}
public static void checkAdjList(int node1, int node2) {
if (!adjList.containsKey(node1)) {
ArrayList<Integer> list = new ArrayList<Integer>();
adjList.put(node1, list);
}
adjList.get(node1).add(node2);
}
public static void readConnections(Scanner sc, int numConnections) {
for (int i = 0; i < numConnections; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
checkAdjList(a, b);
checkAdjList(b, a);
}
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int count = 0;
int numConnections = sc.nextInt();
while (numConnections != 0) {
readConnections(sc, numConnections);
int start = sc.nextInt();
int ttl = sc.nextInt();
while (start != 0 || ttl != 0) {
System.out.println("Case " + ++count + ": " + (adjList.size()-bfs(start, ttl)) + " nodes not reachable from node " + start + " with TTL = " + ttl + ".");
start = sc.nextInt();
ttl = sc.nextInt();
}
adjList.clear();
numConnections = sc.nextInt();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Element {
int node;
int distance;
public Element(int node, int distance) {
this.node = node;
this.distance = distance;
}
}
Comparing the run time between the solutions:
- The oldest solution ran in: 1.742s.
- This new solution ran in: 0.655s.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Integer>> adjList = new HashMap<Integer, ArrayList<Integer>>();
public static int bfs(int start, int ttl) {
Queue<Element> queue = new ArrayDeque<Element>();
Element e = new Element(start, 0);
queue.add(e);
HashSet<Integer> addedToQueue = new HashSet<Integer>();
addedToQueue.add(start);
int reach = 0;
while (queue.size() > 0 && queue.element().distance <= ttl) {
int currNode = queue.element().node;
ArrayList<Integer> neighbors = adjList.get(currNode);
for (int i = 0; i < neighbors.size(); i++) {
if (!addedToQueue.contains(neighbors.get(i))) {
e = new Element(neighbors.get(i), queue.element().distance+1);
queue.add(e);
addedToQueue.add(neighbors.get(i));
}
}
reach++;
queue.remove();
}
return reach;
}
public static void checkAdjList(int node1, int node2) {
if (!adjList.containsKey(node1)) {
ArrayList<Integer> list = new ArrayList<Integer>();
adjList.put(node1, list);
}
adjList.get(node1).add(node2);
}
public static void readConnections(Scanner sc, int numConnections) {
for (int i = 0; i < numConnections; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
checkAdjList(a, b);
checkAdjList(b, a);
}
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int count = 0;
int numConnections = sc.nextInt();
while (numConnections != 0) {
readConnections(sc, numConnections);
int start = sc.nextInt();
int ttl = sc.nextInt();
while (start != 0 || ttl != 0) {
System.out.println("Case " + ++count + ": " + (adjList.size()-bfs(start, ttl)) + " nodes not reachable from node " + start + " with TTL = " + ttl + ".");
start = sc.nextInt();
ttl = sc.nextInt();
}
adjList.clear();
numConnections = sc.nextInt();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Element {
int node;
int distance;
public Element(int node, int distance) {
this.node = node;
this.distance = distance;
}
}
Comments
Post a Comment