forked from bamsarts/DS-ALGO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopological Sorting(Find the Array) .java
208 lines (207 loc) · 7.17 KB
/
Topological Sorting(Find the Array) .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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*We can build a directed graph based on the given M information. if "i<j" we can put an edge from i to j, or from j to i if "i>j". Now if there's a cycle in this directed graph, no such array will exist, thus answer will be "NO". And if ther's no cycle, definately we have a valid solution. For that, we need the topological order of this DAG. Now, we have to binary search on the lenth L, where L indicated both array A and array B will have same integers for indexes 1 to L, and ,A[L+1]=>B[L+1]+1 thus A becomes lexicographically larger than B. We will try to maximize the value of L with binary search. This is how we will get a lexicographically smallest array that is lexicographically larger than .
Complexity: O((N+M).log(N)) */
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class FindTheArray {
public static void main(String[] args) throws Exception {
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
solver(in, pw);
pw.close();
}
static void solver(InputReader in, PrintWriter pw) throws Exception {
int test = in.nextInt();
for (int t = 1; t <= test; t++) {
int n = in.nextInt(), m = in.nextInt();
CHECK(1, (int) 1e5, n);
CHECK(0, (int) 1e5, m);
List<Integer> g[] = genList(n);
List<Integer> r[] = genList(n);
int b[] = new int[n];
for (int i = 0; i < n; i++) {
b[i] = in.nextInt();
CHECK(1, (int)1e9, b[i]);
}
for (int i = 0; i < m; i++) {
int u = in.nextInt();
char sign = in.next().charAt(0);
int v = in.nextInt();
CHECK(1, n, u);
CHECK(1, n, v);
if (u == v) throw new RuntimeException("i = j");
u--;
v--;
if (sign == '>') { //swap
int tmp = u;
u = v;
v = tmp;
}
g[u].add(v);
r[v].add(u);
}
int order[] = topologicalSort(g);
if (order == null) {
pw.println("NO");
} else {
int a[] = new int[n];
for (int u : order) {
int max = 1;
for (int v : r[u]) {
max = max(max, a[v] + 1);
}
a[u] = max;
}
int k = binarySearch(n, a, b, order, r);
boolean ok = check(k, n, a, b, order, r);
if (!ok || !isCorrectSolution(n, a, b, g)) {
throw new RuntimeException("WRONG ANSWER");
}
pw.println("YES");
for (int i = 0; i < n; i++) {
pw.print(a[i]);
if (i < n - 1) pw.print(" ");
else pw.println();
}
}
}
pw.close();
}
static void CHECK(int l, int r, int val) {
if (val < l || val > r) throw new RuntimeException("Value out of bound");
}
static void reverseArray(int a[]){
for(int i = 0, j = a.length-1; i < j; i++, j--){
int temp = a[i]; a[i] = a[j]; a[j] = temp;
}
}
static boolean check_2(int k, int n, int b[], int order[], List<Integer> g[]) {
long [] l = new long[n], r = new long[n];
for(int i = 0; i < n; i++) {
if (i < k) {
l[i] = r[i] = b[i];
}
else {
l[i] = 1;
r[i] = (long)1e15;
}
}
for (int u : order) {
for (int v : g[u]) {
l[u] = max(l[u], l[v] + 1);
}
}
reverseArray(order);
for (int u : order) {
for (int v : g[u]) {
r[v] = min(r[v], r[u] - 1);
}
}
reverseArray(order);
for(int u = 0; u < n; u++) {
if (l[u] > r[u]) {
return false;
}
}
for(int u = 0; u < n; u++) {
if (r[u] > b[u]) {
return true;
}
if (r[u] < b[u]) {
return false;
}
}
return false;
}
static boolean check(int k, int n, int a[], int b[], int order[], List<Integer> r[]) {
a[k] = max(a[k], b[k] + 1);
for (int i = 0; i < k; i++) {
a[i] = max(a[i], b[i]);
if (a[i] > b[i]) return false;
}
//debug("K", k, a);
for (int u : order) {
int max = 1;
for (int v : r[u]) {
max = max(max, a[v] + 1);
}
a[u] = max(a[u], max);
}
for (int i = 0; i < k; i++) {
if (a[i] > b[i]) return true;
if(a[i] < b[i]) return false;
}
return true;
}
static int binarySearch(int n, int a[], int b[], int order[], List<Integer> r[]) {
int lo = 0, hi = n - 1, mid;
while (lo < hi) {
mid = (lo + hi) / 2;
if (!check_2(mid, n, b, order, r)) hi = mid;
else lo = mid + 1;
}
if (lo > 0 && !check_2(lo, n, b, order, r)) {
lo--;
}
return lo;
}
static boolean isCorrectSolution(int n, int a[], int b[], List<Integer> g[]) {
for (int u = 0; u < n; u++) {
for (int v : g[u]) {
if (a[u] >= a[v]) return false;
}
}
for (int i = 0; i < n; i++) {
if (a[i] < b[i]) return false;
if (a[i] > b[i]) return true;
}
return false;
}
static <T> List<T>[] genList(int n) {
List<T> list[] = new List[n];
for (int i = 0; i < n; i++) list[i] = new ArrayList<T>();
return list;
}
public static int[] topologicalSort(List<Integer> g[]) {
int n = g.length;
int[] ec = new int[n];
for (int i = 0; i < n; i++) {
for (int to : g[i]) ec[to]++;
}
int[] ret = new int[n];
int q = 0;
// sources
for (int i = 0; i < n; i++) {
if (ec[i] == 0) ret[q++] = i;
}
for (int p = 0; p < q; p++) {
for (int to : g[ret[p]]) {
if (--ec[to] == 0) ret[q++] = to;
}
}
// loop
for (int i = 0; i < n; i++) {
if (ec[i] > 0) return null;
}
return ret;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() throws Exception {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
}
}