#DSU0000044. Hoán vị năm mới (New Year Permutation)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Hoán vị năm mới (New Year Permutation)
New Year Permutation
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation of length and an binary matrix . The matrix is symmetric, so , and every diagonal entry is .
You may swap the values at two distinct positions whenever . These swaps may be used any number of times in any order. Swapping values changes only the permutation; the matrix and the allowed pairs of positions remain fixed.
Find the lexicographically smallest permutation among all reachable permutations. When comparing two permutations, the first position where their values differ determines which is smaller.
Input
The first line contains , the second the permutation , followed by binary strings describing the rows of .
Output
Print the lexicographically smallest permutation.
Subtasks
- Subtask 1 (20 points): ; is a permutation of ; the matrix contains only
0and1, with and . - Subtask 2 (30 points): ; is a permutation of ; the matrix contains only
0and1, with and . - Subtask 3 (50 points): ; is a permutation of ; the matrix contains only
0and1, with and .
Examples
Input
5
4 2 1 5 3
00100
00011
10010
01101
01010
Output
1 2 3 4 5
Explanation
All positions lie in the same connected component of allowable swaps. Therefore the values can be rearranged into 1 2 3 4 5, which is the lexicographically smallest permutation.