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. 扫描Linux服务器查找恶意软件和rootkit的一款工具

    官网参考官网安装教程:wget https://www.clamav.net/downloads/production/clamav-0.101.1.tar.gztar -zxvf clamav-0. ...

  2. java JDK安装教程

    JAVA_HOME G:\JDK\java7\jdk1.7.0_80    根据自己的哈     ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin 然后找到CLASSPATH ...

  3. LeetCode112:Path Sum

    正常写法 bool HasPathSum(TreeNode root, int sum) { bool ret=false; if(root==null)return false; if(root.l ...

  4. [02] mybatis-config.xml 全局配置文件解析

    本篇会摘录mybatis全局配置文件中比较常见常用的一些配置进行说明,其实官方文档已经写得足够清晰了(关键是还有中文版完全不慌),此处记录重要还是作为个人的巩固记录. 1.properties 在全局 ...

  5. Oracle存储过程的调试

    在工作汇总有时候程序会调用存储过程来实现某些功能,因为这样的话,速度更快.所以学习如何调试存储过程就非常的重要. 首先,打开PLSQL DEVELOPMENT首先介绍一下,这个软件的各个窗口是用来干什 ...

  6. Linux系列教程(五)——Linux常用命令之链接命令和权限管理命令

    前一篇博客我们讲解了Linux文件和目录处理命令,还是老生常淡,对于新手而言,我们不需要完全记住命令的详细语法,记住该命令能完成什么功能,然后需要的时候去查就好了,用的多了我们就自然记住了.这篇博客我 ...

  7. LOJ564 613的天网 构造

    题目传送门 题意:给出一个$N \times N \times N$的方块,你可以在每一个$1 \times 1 \times 1的方块上放上一个摄像头,摄像头的监视范围为6个方向的无限远距离.问最少 ...

  8. url 传递中文参数乱码问题的终极解决方法。

    估计很多人在做web开发的时候,都会碰到过url传递中文参数,有时候会出现乱码的问题,但有些项目或者环境,又不会有问题.当遇到乱码的时候,上网找了很多解决方案,比如: 页面设置它的编码方式,改成utf ...

  9. zookeepeer使用zkCli.sh命令

    一.连接服务器端 [root@sxl132 zookeepeer]# ./bin/zkCli. Connecting to -- ::, [myid:] - INFO [main:Environmen ...

  10. WebApi 接口返回值不困惑:返回值类型详解。IHttpActionResult、void、HttpResponseMessage、自定义类型

    首先声明,我还没有这么强大的功底,只是感觉博主写的很好,就做了一个复制,请别因为这个鄙视我,博主网址:http://www.cnblogs.com/landeanfen/p/5501487.html ...