(UVA) Ordering Tasks - Solution
Solution using Topological Sorting.
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Integer>> adjListOut = new HashMap<Integer, ArrayList<Integer>>();
public static void initAdjList(int numTasks) {
for (int i = 0; i < numTasks; i++) {
adjListOut.put((i+1), new ArrayList<Integer>());
}
}
public static void readDependencies(BufferedReader br, int[] numDependencies, int numRelations) throws NumberFormatException, IOException {
for (int i = 0; i < numRelations; i++) {
int dependency = reader(br);
int dependent = reader(br);
adjListOut.get(dependency).add(dependent);
numDependencies[dependent]++;
}
}
public static void checkZeroDegree(int numTasks, int[] numDependencies, Queue<Integer> zeroDegree) {
for (int i = 0; i < numTasks; i++) {
if (numDependencies[i+1] == 0) {
zeroDegree.add(i+1);
}
}
}
public static void topoSort(int[] seqTasks, Queue<Integer> zeroDegree, int[] numDependencies) {
int index = 0;
while (zeroDegree.size() > 0) {
int currTask = zeroDegree.poll();
seqTasks[index++] = currTask;
for (int i = 0; i < adjListOut.get(currTask).size(); i++) {
int task = adjListOut.get(currTask).get(i);
numDependencies[task]--;
if (numDependencies[task] == 0) {
zeroDegree.add(task);
}
}
}
}
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTasks = reader(br);
int numRelations = reader(br);
while (numTasks != 0 || numRelations != 0) {
initAdjList(numTasks);
int[] numDependencies = new int[numTasks+1];
readDependencies(br, numDependencies, numRelations);
Queue<Integer> zeroDegree = new ArrayDeque<Integer>();
checkZeroDegree(numTasks, numDependencies, zeroDegree);
int[] seqTasks = new int[numTasks];
topoSort(seqTasks, zeroDegree, numDependencies);
for (int i = 0; i < numTasks-1; i++) {
bw.write(seqTasks[i] + " ");
}
bw.write(seqTasks[numTasks-1] + "\n");
adjListOut.clear();
numTasks = reader(br);
numRelations = reader(br);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.io.*;
import java.util.*;
class Main {
public static HashMap<Integer, ArrayList<Integer>> adjListOut = new HashMap<Integer, ArrayList<Integer>>();
public static void initAdjList(int numTasks) {
for (int i = 0; i < numTasks; i++) {
adjListOut.put((i+1), new ArrayList<Integer>());
}
}
public static void readDependencies(BufferedReader br, int[] numDependencies, int numRelations) throws NumberFormatException, IOException {
for (int i = 0; i < numRelations; i++) {
int dependency = reader(br);
int dependent = reader(br);
adjListOut.get(dependency).add(dependent);
numDependencies[dependent]++;
}
}
public static void checkZeroDegree(int numTasks, int[] numDependencies, Queue<Integer> zeroDegree) {
for (int i = 0; i < numTasks; i++) {
if (numDependencies[i+1] == 0) {
zeroDegree.add(i+1);
}
}
}
public static void topoSort(int[] seqTasks, Queue<Integer> zeroDegree, int[] numDependencies) {
int index = 0;
while (zeroDegree.size() > 0) {
int currTask = zeroDegree.poll();
seqTasks[index++] = currTask;
for (int i = 0; i < adjListOut.get(currTask).size(); i++) {
int task = adjListOut.get(currTask).get(i);
numDependencies[task]--;
if (numDependencies[task] == 0) {
zeroDegree.add(task);
}
}
}
}
public static int reader(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') {
break;
}
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') {
break;
}
}
return resp;
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTasks = reader(br);
int numRelations = reader(br);
while (numTasks != 0 || numRelations != 0) {
initAdjList(numTasks);
int[] numDependencies = new int[numTasks+1];
readDependencies(br, numDependencies, numRelations);
Queue<Integer> zeroDegree = new ArrayDeque<Integer>();
checkZeroDegree(numTasks, numDependencies, zeroDegree);
int[] seqTasks = new int[numTasks];
topoSort(seqTasks, zeroDegree, numDependencies);
for (int i = 0; i < numTasks-1; i++) {
bw.write(seqTasks[i] + " ");
}
bw.write(seqTasks[numTasks-1] + "\n");
adjListOut.clear();
numTasks = reader(br);
numRelations = reader(br);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment