PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FW0000035. Hoán vị đẹp nhất năm mới (New Year Permutation)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Hoán vị đẹp nhất năm mới (New Year Permutation)
New Year Permutation
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given a permutation and a symmetric matrix of allowed swaps, perform any number of swaps and find the lexicographically smallest reachable permutation.
Input
Integer , a permutation , then binary strings of length specifying allowed swaps.
Output
Print the lexicographically smallest reachable permutation.
Subtasks
- Subtask 1 (40 points): ; ; is a permutation of ; the adjacency matrix is symmetric with zero diagonal.
- Subtask 2 (60 points): ; is a permutation of ; the adjacency matrix is symmetric with zero diagonal.
Examples
Input
4
4 3 2 1
0100
1010
0101
0010
Output
1 2 3 4
Explanation
All four positions lie in the same connected component of swappable positions. Sorting the component values produces the lexicographically smallest permutation.