Good Bye B. New year permutation "transitive closure greedy"

Source: Internet
Author: User

Problem-solving ideas: Give a column number an, and then give a matrix d[i][j], in the case of satisfying d[i][j]=1, called A[i] and A[j] can be exchanged, ask through the exchange and finally get the smallest dictionary sequence a[n]

The first is greedy thought, big can change to go back, so as to make small in front.

Then it is judged a[i] and a[j] can be exchanged, if d[i][j]=1, representing A[i] and A[j] can be exchanged directly if d[i][k]=1&&d[k][j]=1, means a[i] may be exchanged via a[k] and A[j]

For example 1 7 3 4 2 5 if 7 can and 4 Exchange, 4 can and 2 exchange, then 7 can and 2 Exchange

When the matrix is processed, the transmission closure of the purple book is used.

 for (k=1; k<=n;k++)          for (i=1; i<=n;i++)              for (j=1; j<=n;j++)            d[i][j]=d[i][j]| | (D[i][k]&&d[k][j]);
B. New year Permutationtime limit per test2 secondsmemory limit per test256 Megabytesinputstandard Inputoutputstandard out Put

User Ainta has a permutation p1, P2, ..., pn. As the New year was coming, he wants to make his permutation as pretty as possible.

Permutationa1,a2,...,aNIsPrettierthan permutationb1,b2,...,bNIf and only if there exists an integerk  (1≤ k n ) where  a 1 = b 1, a 2 = b 2, ..., a k -1 = B k -1 and   a Sub class= "Lower-index" > K < b K  all holds.

As known, permutationPis so sensitive the it could be only modified by swapping and distinct elements. But swapping and elements are harder than you think. Given anNXNBinary matrixA, user ainta can swap the values of P i  and   p J   (1≤ i , j n ,   i J ) if and only If  A i , j = 1.

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

Input

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

The second line containsNspace-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 NLines describe the matrixA. TheI-th Line containsNCharacters '0 ' or '1 ' and describes theI-th row ofA. TheJ-th character of theI-th LineAI,Jis the element on the intersection of theI-th Row and theJ-th column of A. It is guaranteed this, for all integersI,Jwhere1≤I<JN, ai, j = aJ, I holds. Also, for all integers i where 1≤ in, aI, i = 0 holds.

Output

In the first and only line, print n space-separated integers, describing the prettiest permutation th At can is obtained.

Sample Test (s) 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).

APermutationPis a sequence of integersP1,P2,...,PN, consisting ofNDistinct positive integers, each of them doesn ' t exceedN. The i-th element of the permutation p is denoted as pI. The size of the permutation p is denoted as n.

#include <stdio.h> #include <algorithm>using namespace Std;int a[1010],d[1010][1010];char s[1020];int main () {int n,i,j,k;scanf ("%d", &n), for (i=1;i<=n;i++) scanf ("%d", &a[i]), and for (i=1;i<=n;i++) {scanf ("%s", &s); for (j=0;j<n;j++) {if (s[j]== ' 1 ')  d[i][j+1]=1;else  d[i][j+1]=0;}} for (k=1;k<=n;k++) for (i=1;i<=n;i++) for (j=1;j<=n;j++) d[i][j]=d[i][j]| | (D[i][k]&&d[k][j]); for (i=1;i<=n;i++) {for (j=i+1;j<=n;j++) {if (D[i][j]&&a[i]>a[j]) swap (a [I],a[j]);}} for (i=1;i<n;i++) printf ("%d", A[i]);p rintf ("%d\n", A[i]);}

  

Good Bye B. New year permutation "transitive closure greedy"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.