-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUVa00124_FollowingOrders.java
113 lines (91 loc) · 2.47 KB
/
UVa00124_FollowingOrders.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 60 (124 - Following Orders) */
/* SUBMISSION: 09367326 */
/* SUBMISSION TIME: 2011-10-13 17:28:05 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00124_FollowingOrders {
static int n;
static boolean[][] adj;
static char[] vertices;
static colors[] color;
static int time;
static int[] ti;
static int[] tf;
static String sort;
static boolean[] used;
static Set<String> res;
enum colors {WHITE, GRAY, BLACK};
static void dfs(int s) {
ti[s] = ++time; // Discover time
color[s] = colors.GRAY; // Discover vertex
for (int i = 0; i < 26; ++i)
if (adj[s][i] && color[i] == colors.WHITE) // If neighbor and not discovered
dfs(i);
color[s] = colors.BLACK; // Vertex is finished
tf[s] = ++time; // Finish time
sort = (char)(s + 'a') + sort;
}
static void topsort(char[] b) {
time = 0;
ti = new int[26];
tf = new int[26];
color = new colors[26];
sort = "";
Arrays.fill(color, colors.WHITE);
for (int i = 0; i < n; ++i)
if (color[b[i] - 'a'] == colors.WHITE)
dfs(b[i] - 'a');
res.add(sort);
}
static void backtrack(char[] b, int k) {
if (k == n) {
topsort(b);
return;
}
for (int i = 0; i < n; ++i)
if (!used[i]) {
int v = vertices[k] - 'a';
int w = vertices[i] - 'a';
used[i] = true;
b[k] = vertices[i];
backtrack(b, k + 1);
used[i] = false;
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
StringTokenizer stk;
boolean first = true;
while ((line = in.readLine()) != null) {
stk = new StringTokenizer(line);
n = stk.countTokens();
vertices = new char[n];
adj = new boolean[26][26];
for (int i = 0; i < n; ++i)
vertices[i] = stk.nextToken().charAt(0);
stk = new StringTokenizer(in.readLine());
int m = stk.countTokens() / 2;
for (int i = 0; i < m; ++i) {
char from = stk.nextToken().charAt(0);
char to = stk.nextToken().charAt(0);
adj[from - 'a'][to - 'a'] = true;
}
if (first)
first = false;
else
System.out.println();
used = new boolean[n];
char[] b = new char[n];
res = new TreeSet<String>();
backtrack(b, 0);
for (String s : res)
System.out.println(s);
}
in.close();
System.exit(0);
}
}