New Year Permutation

来源:互联网 发布:淘宝口令红包怎么抢 编辑:程序博客网 时间:2024/05/17 22:52


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) wherea1 = 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 ifAi, 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 1and 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 thei-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 where1 ≤ 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 test(s)
input
75 2 4 3 6 7 10001001000000000000101000001000000000100001001000
output
1 2 4 3 6 7 5
input
54 2 1 5 30010000011100100110101010
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. Thei-th element of the permutation p is denoted as pi. The size of the permutation p is denoted as n.

一个数列,如果前k个数相等,a[k+1]<b[k+1]那么 a比b好,给出一个矩阵,如果aij=1,那么i和j位置的数是可以交换的,求给出矩阵状态下最好的数列。其实这个比较法就是尽量把小的数放在前面,但是加了一个交换的位置条件。自己想法就是把每个位置遍历,然后把连通分支整个找出来,再内部排序,赋值,接着找下一个连通分支,重复上述动作,循环n次,虽然有点重复,但是只有300个点,并不会超时。话说1A真开心。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>using namespace std;int b[303][303];int a[303], leap[303], dp[303];queue<int>p;int main(){int i, j, m, n, ans, temp;char c;cin >> n;for (i = 1; i <= n; i++)cin >> a[i];getchar();for (i = 1; i <= n; i++){for (j = 1; j <= n; j++){c = getchar();b[i][j] = c - '0';}getchar();}for (i = 1; i <= n; i++){p.push(i);memset(leap, 0, sizeof(leap));leap[i] = 1;while (p.size()){temp = p.front();for (j = 1; j <= n; j++){if (b[temp][j] == 1 && !leap[j]){p.push(j);leap[j] = 1;}}p.pop();}ans = 0;for (j = 1; j <= n; j++){if (leap[j]){ans++;dp[ans] = a[j];}}sort(dp + 1, dp + 1 + ans);ans = 0;for (j = 1; j <= n; j++){if (leap[j]){ans++;a[j] = dp[ans];}}}for (i = 1; i <= n; i++)cout << a[i] << " ";cout << endl;return 0;}
这是我当时第一次做cf的b题,当时的a题还是50分钟这样做出来的,b题就更不敢奢望了,现在感觉自己有提升,但是 作为一个初学者,想要获得提升是很容易的,还要继续走下去。


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 左拐车道直行了怎么办 踏板摩托车淋雨打不着火怎么办 购房小蓝本丢了怎么办 3d下载模型没颜色怎么办 车辆被扣30分怎么办 驾驶证被扣15分怎么办 驾驶本被扣分了怎么办 违章扣分扣错了怎么办 出国自驾游怎么办手续 新加坡开车工作遇到坏人怎么办 身份证户口本都丢了怎么办 户口本和身份证都丢了怎么办 网上预约挂完号没收到短信怎么办 驾驶证过了换证期限怎么办 身体弱末梢神经循环不好怎么办 自来水钙镁离子超标怎么办 呼吸感觉有煤烟味是怎么办 酒精弄到衣服上怎么办 孕妇吃了糟卤怎么办 气泡机打不了气怎么办 膝盖好冷好凉怎么办吃什么好 卸妆水进眼睛了怎么办 外出没带卸妆水怎么办? 种睫毛卸睫膏弄眼睛里面了 怎么办 化了妆没卸妆水怎么办 痘痘变成了黑痣怎么办 痘痘形成的痣怎么办 图片文件重命名改不了怎么办 想给宝宝改名字怎么办 洗照片像素过低怎么办 用ps改尺寸照片变形怎么办 平安银行卡三次密码错误怎么办 平安银行卡密码忘了怎么办 平安银行行用卡多次还款怎么办 平安银行大润发卡还不上怎么办 八载图片被投诉怎么办 ps用替换颜色后怎么办 三星s8百度闪退怎么办 吃了过敏的东西怎么办 过敏了痒的厉害怎么办 脸吃麻辣过敏了怎么办