-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanonic.java
72 lines (69 loc) · 2.29 KB
/
Canonic.java
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
import java.util.*;
import static java.lang.Math.min;
public class Canonic {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
int m = in.nextInt();
in.nextLine();
int start = in.nextInt();
in.nextLine();
List<List<Integer>> g = new ArrayList<>();
List<List<String>> f = new ArrayList<>();
for (int i = 0; i < n; i++) {
g.add(new ArrayList<Integer>());
for (int j = 0; j < m; j++) {
var a = in.nextInt();
g.get(i).add(a);
}
in.nextLine();
}
for (int i = 0; i < n; i++) {
f.add(new ArrayList<>());
String line = in.nextLine();
String[] outs = line.split(" ");
for (int j = 0; j < m; j++) {
f.get(i).add(outs[j]);
}
}
List<Integer> done = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
stack.push(start);
while (stack.size() != 0) {
var i = stack.pop();
if (done.stream().anyMatch(x -> Objects.equals(x, i))){
continue;
}
done.add(i);
Stack<Integer> gstack = new Stack<>();
List<Integer> doneable = new ArrayList<>();
for(int c: g.get(i)){
if (done.stream().noneMatch(x -> x == c) && doneable.stream().noneMatch(x -> x == c)){
gstack.push(c);
doneable.add(c);
}
}
while (gstack.size() != 0){
stack.push(gstack.pop());
}
}
System.out.println(n);
System.out.println(m);
System.out.println(done.indexOf(start));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(done.indexOf(g.get(done.get(i)).get(j)));
System.out.print(" ");
}
System.out.print("\n");
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(f.get(done.get(i)).get(j));
System.out.print(" ");
}
System.out.print("\n");
}
}
}