Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#pragma comment(linker, "/STACK:36777216")
- #include <algorithm>
- #include <bitset>
- #include <cassert>
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <complex>
- #include <ctime>
- #include <stack>
- //#include <bits/stdc++.h>
- //#include <ext/pb_ds/assoc_container.hpp>
- //#include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- //using namespace __gnu_pbds;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef vector<int> VI;
- typedef vector< VI > VVI;
- typedef pair<int, int> PII;
- typedef vector<PII> VPII;
- #define REP(i, n) for(int i = 0; i < (n); ++i)
- #define RREP(i, n) for(int i = (n) - 1; i >= 0; --i)
- #define FOR(i, x, y) for(int i = (x); i <= (y); ++i)
- #define RFOR(i, y, x) for(int i = (x); i >= (y); --i)
- #define SZ(a) (int)(a).size()
- #define ALL(a) (a).begin(),(a).end()
- #define SORT(a) sort(ALL(a))
- #define CLEAR(x) memset(x, 0, sizeof x);
- #define COPY(FROM, TO) memcpy(TO, FROM, sizeof TO);
- #define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
- #define pb push_back
- #define mk make_pair
- #define sqr(x) ((x)*(x))
- #define X first
- #define Y second
- //typedef tree<int, null_type, less<int>,
- // rb_tree_tag, tree_order_statistics_node_update> order_set;
- const long double PI = acos(-1.0);
- const double EPS = 1E-6;
- const int INF = 2 * (1e+9) + 1;
- const int MAXN = 3 * 100000001;
- const int MOD = (1e+9) + 7;
- int n, m, t;
- int u[301], ans[301];
- int cc[301];
- int a[100100];
- int pp[301][301];
- void dfs (int v, VI &p, VI &pos) {
- p.pb(a[v]);
- pos.pb(v);
- u[v] = 1;
- REP (i, n) {
- if (pp[v][i] == 0 && !u[i]) {
- dfs (i, p, pos);
- }
- }
- u[v] = 2;
- }
- int main () {
- cin >> n;
- REP (i, n) cin >> a[i];
- REP (i, n) {
- REP (j, n) {
- char c; cin >> c;
- pp[i][j] = (c - '0');
- }
- }
- REP (i, n) {
- VI p, pos;
- if (!u[i]) {
- dfs (i, p, pos);
- SORT(p);
- SORT(pos);
- REP (j, SZ(pos)) {
- ans[pos[j]] = p[j];
- }
- }
- }
- REP (i, n) cout << ans[i] << " ";
- }
- /*
- 7
- 5 2 4 3 6 7 1
- 0001001
- 0000000
- 0000010
- 1000001
- 0000000
- 0010000
- 1001000
- */
Advertisement
Add Comment
Please, Sign In to add comment