Description

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 < bk all 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 ≤ n, i ≠ 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 ≤ n, Ai, j = Aj, i holds. Also, for all integers i where 1 ≤ i ≤ n, Ai, i = 0 holds.

Output

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

Sample Input

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

思路:
用floyd先将左右潜在的可以连通的边在邻接矩阵中标记出来,然后像冒泡一样的给他们排序一下就可以了
要注意两点:
一是刚开始开数组的时候,要从0开始开,不然用scanf输入会很麻烦
二是floyd的三层for循环,注意k的顺序要在最前面 这题也是对并查集数据结构的一种很好的理解,不过忽略了并查集分组的性质,而只关注了并查集的连通性
即通过邻接矩阵得到扩展边与边的连通关系

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; int n;
int p[];
char f[][]; int main()
{
while(~scanf("%d",&n))
{
for(int i = ;i < n;i++)
scanf("%d",&p[i]);
for(int i = ;i < n;i++)
{
scanf("%s",f[i]);
for(int j = ;j < n;j++)
if(f[i][j] == '')
f[i][j] = '';
}
for(int k = ;k < n;k++)
for(int i = ;i < n;i++)
for(int j = ;j < n;j++)
if(f[i][k]=='' && f[k][j]=='')
f[i][j] = ''; for(int i = ;i < n;i++)
for(int j = i+;j < n;j++)
if(f[i][j]=='' && p[j] < p[i])
swap(p[i],p[j]); for(int i = ;i < n-;i++)
printf("%d ",p[i]);
printf("%d\n",p[n-]);
}
return ;
}

New Year Permutation(Floyd+并查集)的更多相关文章

  1. HDU3081:Marriage Match II (Floyd/并查集+二分图匹配/最大流(+二分))

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  3. Educational Codeforces Round 14 D. Swaps in Permutation(并查集)

    题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 ...

  4. CF 500 B. New Year Permutation 并查集

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

  5. [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

    题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...

  6. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  7. 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  8. 求最小环 —— 并查集 与 Floyd

    对于一个图,如何求出其中的最小环(不包括一元环)? 很显然,对于一个无向图,每一条边都是一个二元环:对于有向图,可以考虑从每一个点出发,用DFS求出它到自己的距离,如果遍历了$N$个点仍未便利到自己, ...

  9. Educational Codeforces Round 14 D. Swaps in Permutation 并查集

    D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are gi ...

随机推荐

  1. 安装LVS安装LVS和配置LVS的工作比较繁杂

    安装LVS安装LVS和配置LVS的工作比较繁杂,读者在配置的过程中需要非常细心和耐心.在本节我们将对其进行详细地介绍.主要包括如下几个核心步骤:1.获取支持LVS的内核源代码如果读者需要使用LVS,需 ...

  2. Trie和Ternary Search Tree介绍

    Trie树 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie树与二叉搜索树不同,键不是直接保存在节 ...

  3. java接口与抽象类的区别

    接口可以是标志接口,里面没有任何常量和方法. 抽象类不一定必须有抽象方法,也可也没有方法,但含抽象方法的类必须被声明为抽象类. 在抽象层次结构中,Java接口在最上面,然后紧跟着抽象类,然后是一般类. ...

  4. Swift学习笔记 - 函数与闭包

    import Foundation //1.函数的定义与调用//以 func 作为前缀,返回箭头 -> 表示函数的返回类型func sayHello(name: String) -> St ...

  5. 安装apache重启的时候,报错端口被占用,错误1

    在cmd中执行以下命令来重新分配. netsh winsock reset. 还不行的话可以重启电脑,再不行就算apache配置文件错误.

  6. 让linux(centos)支持中文文件和文件夹

    一.让linux支持中文 1.将Linux的env设置了LANG=en_US.UTF-8: 2.本地的Shell客户端编码也设置成UTF-8,这样让在windows上传到linux的文件或者目录不会出 ...

  7. NYOJ 116士兵杀敌(二) 树状数组

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=116 士兵杀敌(一) 数组是固定的,所以可以用一个sum数组来保存每个元素的和就行,但是不 ...

  8. 用POP动画引擎实现弹簧动画(POPSpringAnimation)

    效果图: #import "ViewController.h" #import <POP.h> @interface ViewController () @proper ...

  9. 使用C#代码追加和提交文件到SVN服务器

    windows系统下使用svn的命令需要安装一个插件,下载地址:http://sourceforge.net/projects/win32svn/?source=typ_redirect 安装后程序会 ...

  10. printf格式输出总结

    #include<stdio.h>    #include<string.h>    int main()    {        ];        ;       floa ...