-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewYearPermutation.java
110 lines (101 loc) · 2.86 KB
/
NewYearPermutation.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
/**
* https://codeforces.com/problemset/problem/500/B #dfs #dsu #greedy #math #sorting
*
* <p>-> vua xay, va xu ly tap hop -> xay xong tap hop, roi moi xu ly. -> b,c div 2.
*
* <p>1 2 3 ... <- a[idx] ... a[i] < a[j] --- a[k]
*
* <p>a[idx] / \ a[i] a[j] \ a[k]
*
* <p>1 3 2 4 5
*
* <p>5 6 4 3 2 7 1 0001001 0000100 0000010 1000001 0100000 0010000 1001000
*
* <p>1 - 4 1 - 7 3 - 6 4 - 7 2 - 5
*
* <p>(1, 4, 7) (5, 3, 1) -> (1, 3, 5)
*
* <p>(3, 6) (4, 7)
*
* <p>(2, 5) (6, 2) -> (2, 6)
*
* <p>init: 5 6 4 3 2 7 1 gr 1: 1 6 4 3 2 7 5 gr 2: gr 3: 1 2 4 3 6 7 5
*
* <p>4 2 1 5 3 00100 00011 10010 01101 01010
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
public class NewYearPermutation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sz = n + 1;
int[] arr = new int[sz];
int[] arrIdx = new int[sz];
char[][] arrChar = new char[n][n];
int tmp;
for (int i = 1; i < sz; i++) {
tmp = sc.nextInt();
arr[i] = tmp;
arrIdx[tmp] = i;
}
String str;
for (int i = 0; i < n; i++) {
str = sc.next();
arrChar[i] = str.toCharArray();
}
ArrayList<ArrayList<Integer>> graph = new ArrayList<>(sz);
for (int i = 0; i < sz; i++) {
graph.add(new ArrayList<Integer>());
}
for (int i = 0; i < arrChar.length; i++) {
for (int j = 0; j < arrChar[0].length; j++) {
if (arrChar[i][j] == '1') {
graph.get(i + 1).add(j + 1);
graph.get(j + 1).add(i + 1);
}
}
}
boolean[] visitedArr = new boolean[sz];
Arrays.fill(visitedArr, false);
for (int node = 1; node < sz; node++) {
if (!visitedArr[arrIdx[node]]) {
DFS(graph, visitedArr, arrIdx[node], arr);
}
}
for (int i = 1; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void DFS(
ArrayList<ArrayList<Integer>> graph, boolean[] visitedArr, int vertex, int[] arr) {
ArrayList<Integer> idxList = new ArrayList<>();
ArrayList<Integer> valList = new ArrayList<>();
idxList.add(vertex);
valList.add(arr[vertex]);
Deque<Integer> queue = new LinkedList<>();
visitedArr[vertex] = true;
queue.addLast(vertex);
while (!queue.isEmpty()) {
int node = queue.pollLast();
for (int neighbourNode : graph.get(node)) {
if (!visitedArr[neighbourNode]) {
visitedArr[neighbourNode] = true;
queue.addLast(neighbourNode);
idxList.add(neighbourNode);
valList.add(arr[neighbourNode]);
}
}
}
Collections.sort(valList);
Collections.sort(idxList);
for (int i = 0; i < idxList.size(); i++) {
arr[idxList.get(i)] = valList.get(i);
}
}
}