import java io File import java io FileNotFoundExceptio import java ut

 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class MyClass {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("example.txt"));
String line = scanner.nextLine().trim();
int n = line.charAt(0) - '0';
int [][] arr = new int[n + 1][n + 1]; //матрица инциндентности
line = scanner.nextLine().trim();
int i = 0;
while (i < line.length() - 2) {
int a1 = line.charAt(i) - '0';
System.out.println(a1);
int a2 = line.charAt(i + 2) - '0';
System.out.println(a2);
arr[a1][a2]++;
i += 4;
}
line = scanner.nextLine().trim();
int source = line.charAt(0) - '0';
int topological_sort[] = null;
System.out.print("Result: ");
TopologicalSort topologicalSort = new TopologicalSort();
topological_sort = topologicalSort.topological(arr, source);
for (i = topological_sort.length - 1; i > 0; i-- ) {
if (topological_sort[i] != 0)
System.out.print(topological_sort[i]+" ");
}
}
}