-
Notifications
You must be signed in to change notification settings - Fork 0
/
B.cpp
70 lines (62 loc) · 1.29 KB
/
B.cpp
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
// Problem: B. New Year Permutation
// Contest: Codeforces - Good Bye 2014
// URL: https://codeforces.com/problemset/problem/500/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 307;
const int MOD = 1000000007;
int n;
vector<int> g[MAX];
bool vis[MAX];
vector<int> curr;
void dfs(int v) {
curr.push_back(v);
vis[v] = true;
for (int i : g[v]) {
if (!vis[i]) {
dfs(i);
}
}
}
void solve() {
cin >> n;
int a[MAX];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
string s;
for (int i = 1; i <= n; i++) {
cin >> s;
for (int j = 1; j <= n; j++) {
if (s[j - 1] == '1') {g[i].push_back(j);}
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
curr.clear();
dfs(i);
vector<int> now;
for (int j : curr) {
now.push_back(a[j]);
}
sort(now.begin(), now.end());
sort(curr.begin(), curr.end());
for (int j = 0; j < curr.size(); j++) {
a[curr[j]] = now[j];
}
}
}
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.in", "r", stdin); freopen("out.out", "w", stdout);
//int tt; cin >> tt; for (int i = 1; i <= tt; i++) {solve();}
solve();
}