Advertisement
newb_ie

Untitled

Oct 31st, 2021
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. const int maxN = 310;
  5. vector<int> adj[maxN];
  6. bool visited[maxN];
  7. vector<int> idx;
  8.  
  9. void dfs (int node) {
  10. visited[node] = true;
  11. idx.push_back(node);
  12. for (int child : adj[node]) {
  13. if (!visited[child]) dfs(child);
  14. }
  15. }
  16.  
  17. int main () {
  18. ios::sync_with_stdio(false);
  19. cin.tie(nullptr);
  20. cout.tie(nullptr);
  21. int n;
  22. cin >> n;
  23. int a[n + 1];
  24. for (int i = 1; i <= n; ++i) {
  25. cin >> a[i];
  26. }
  27. for (int i = 1; i <= n; ++i) {
  28. for (int j = 1; j <= n; ++j) {
  29. char c;
  30. cin >> c;
  31. if (c == '1') {
  32. if (i == j) continue;
  33. adj[i].push_back(j);
  34. adj[j].push_back(i);
  35. }
  36. }
  37. }
  38. for (int i = 1; i <= n; ++i) {
  39. if (!visited[i]) {
  40. idx.clear();
  41. dfs(i);
  42. vector<int> elem;
  43. for (int x : idx) {
  44. elem.push_back(a[x]);
  45. }
  46. sort (idx.begin(), idx.end());
  47. sort (elem.begin(), elem.end());
  48. for (int j = 0; j < (int) idx.size(); ++j) {
  49. a[idx[j]] = elem[j];
  50. }
  51. }
  52. }
  53. for (int i = 1; i <= n; ++i) cout << a[i] << ' ';
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement