New Year Permutation

CodeForces - 500B

User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty as possible.

Permutation a1, a2, ..., an is prettier than permutation b1, b2, ..., bn, if and only if there exists an integer k (1 ≤ k ≤ n) where a1 = b1, a2 = b2, ..., ak - 1 = bk - 1 and ak < bkall holds.

As known, permutation p is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an n × n binary matrix A, user ainta can swap the values of pi and pj (1 ≤ i, j ≤ ni ≠ j) if and only if Ai, j = 1.

Given the permutation p and the matrix A, user ainta wants to know the prettiest permutation that he can obtain.

Input

The first line contains an integer n (1 ≤ n ≤ 300) — the size of the permutation p.

The second line contains n space-separated integers p1, p2, ..., pn — the permutation p that user ainta has. Each integer between 1 and n occurs exactly once in the given permutation.

Next n lines describe the matrix A. The i-th line contains n characters '0' or '1' and describes the i-th row of A. The j-th character of the i-th line Ai, j is the element on the intersection of the i-th row and the j-th column of A. It is guaranteed that, for all integers i, j where 1 ≤ i < j ≤ nAi, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ nAi, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation that can be obtained.

Examples

Input
7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000
Output
1 2 4 3 6 7 5
Input
5
4 2 1 5 3
00100
00011
10010
01101
01010
Output
1 2 3 4 5

Note

In the first sample, the swap needed to obtain the prettiest permutation is: (p1, p7).

In the second sample, the swaps needed to obtain the prettiest permutation is (p1, p3), (p4, p5), (p3, p4).

A permutation p is a sequence of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. The i-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

sol:十分显然的是a可以与b换,b可以与c换,a就可以和c换,于是把可以换的用并查集缩成一个个连通块,在每个联通块中做到最优,那么就是答案了

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,a[N],Pos[N];
bool Bo[N][N];
int Father[N];
inline int Get_Father(int x)
{
return (Father[x]==x)?(x):(Father[x]=Get_Father(Father[x]));
}
struct Record
{
int Shuz,Weiz;
};
inline bool cmp_Shuz(Record p,Record q)
{
return p.Shuz<q.Shuz;
}
inline bool cmp_Weiz(Record p,Record q)
{
return p.Weiz<q.Weiz;
}
vector<Record>Liantong[N];
vector<int>Xulie[N];
int main()
{
int i,j;
R(n);
for(i=;i<=n;i++)
{
R(a[i]); Father[i]=i;
}
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
char ch=' ';
while(!isdigit(ch)) ch=getchar();
if(ch=='')
{
Father[Get_Father(i)]=Get_Father(j);
}
}
}
for(i=;i<=n;i++)
{
Liantong[Get_Father(i)].push_back((Record){a[i],i});
}
for(i=;i<=n;i++) if(Liantong[i].size())
{
sort(Liantong[i].begin(),Liantong[i].end(),cmp_Shuz);
for(j=;j<Liantong[i].size();j++) Xulie[i].push_back(Liantong[i][j].Shuz);
}
for(i=;i<=n;i++) if(Liantong[i].size())
{
sort(Liantong[i].begin(),Liantong[i].end(),cmp_Weiz);
for(j=;j<Liantong[i].size();j++) a[Liantong[i][j].Weiz]=Xulie[i][j];
}
for(i=;i<n;i++) W(a[i]); Wl(a[n]);
return ;
}
/*
input
7
5 2 4 3 6 7 1
0001001
0000000
0000010
1000001
0000000
0010000
1001000
output
1 2 4 3 6 7 5 input
5
4 2 1 5 3
00100
00011
10010
01101
01010
output
1 2 3 4 5
*/

codeforces500B的更多相关文章

随机推荐

  1. Android学习之多触点滑动

    知识点: 1.对矩阵:Matrix类熟悉,链接:https://blog.csdn.net/jdsjlzx/article/details/52741445 2.MotionEvent详解:https ...

  2. (转)Linux SSH配置和禁止Root远程登陆设置

    原文 一.修改vi /etc/ssh/sshd_config 文件 1.修改默认端口:默认Port为22,并且已经注释掉了:修改是把注释去掉,并修改成其它的端口. 2.禁止root用户远程登陆:修改P ...

  3. 【SQL】MaxComputer中调试与问题排查技巧小结

    1.分段调试 面对长的SQL,出错时一般直接看定位的行号,有时候不出错但是没数据时,应该尝试分段调试,很长的SQL嵌套很多的子查询时,一个一个子查询进行分别调试,看哪一步子查询出了问题,层层推进 2. ...

  4. js 稍微判断下浏览器 pc 还是手机

        function isMobile() {    var a=navigator.userAgent;   var ref=/.*(Android|iPhone|SymbianOS|iPad| ...

  5. Python高级特性(切片,迭代,列表生成式,生成器,迭代器)

    掌握了Python的数据类型.语句和函数,基本上就可以编写出很多有用的程序了. 比如构造一个1, 3, 5, 7, ..., 99的列表,可以通过循环实现: L = [] n = 1 while n ...

  6. VMware workstation运维实践系列博客导航

    第一章:VMware workstation虚拟化1.1 VMware workstation计算网络存储介绍1.2 VMware workstation其他功能特性介绍1.3 VMware work ...

  7. 从源码的角度看 React JS 中批量更新 State 的策略(下)

    这篇文章我们继续从源码的角度学习 React JS 中的批量更新 State 的策略,供我们继续深入学习研究 React 之用. 前置文章列表 深入理解 React JS 中的 setState 从源 ...

  8. Vue 回顾之指令(关于input自动聚焦的问题)

    用了Vue也一年多了,虽然对大部分内容都比较熟悉,但有些用法可能会起到意想不到的作用. 今天在做一个关于抽奖的需求,要求是每次点击编辑按钮显示编辑框,要求自动聚焦. 一开始想到了autofocus属性 ...

  9. Peer Programming Project: 4 Elevators Scheduler 学号后三位 157,165

    1.Advantages and disanvantages of Peer Programming advantages The code are constantly validated by t ...

  10. windows上tomcat8的安装及配置

    提示:在安装tomcat之前,确定安装好jdk. 一.下载tomcat8 http://tomcat.apache.org/download-80.cgi 点击这个网址 根据自己电脑的才做系统版本安装 ...