Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const int maxN = 310;
- vector<int> adj[maxN];
- bool visited[maxN];
- vector<int> idx;
- void dfs (int node) {
- visited[node] = true;
- idx.push_back(node);
- for (int child : adj[node]) {
- if (!visited[child]) dfs(child);
- }
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- int a[n + 1];
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- }
- for (int i = 1; i <= n; ++i) {
- for (int j = 1; j <= n; ++j) {
- char c;
- cin >> c;
- if (c == '1') {
- if (i == j) continue;
- adj[i].push_back(j);
- adj[j].push_back(i);
- }
- }
- }
- for (int i = 1; i <= n; ++i) {
- if (!visited[i]) {
- idx.clear();
- dfs(i);
- vector<int> elem;
- for (int x : idx) {
- elem.push_back(a[x]);
- }
- sort (idx.begin(), idx.end());
- sort (elem.begin(), elem.end());
- for (int j = 0; j < (int) idx.size(); ++j) {
- a[idx[j]] = elem[j];
- }
- }
- }
- for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement