結果
| 問題 |
No.1779 Magical Swap
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2021-12-08 02:00:07 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 58 ms / 2,000 ms |
| コード長 | 2,581 bytes |
| コンパイル時間 | 14,509 ms |
| コンパイル使用メモリ | 389,948 KB |
| 実行使用メモリ | 13,508 KB |
| 最終ジャッジ日時 | 2024-07-16 07:39:23 |
| 合計ジャッジ時間 | 16,419 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
// ---------- begin quick find ----------
pub struct QuickFind {
size: usize,
id: Vec<usize>,
list: Vec<Vec<usize>>,
}
impl QuickFind {
pub fn new(size: usize) -> Self {
let id = (0..size).collect::<Vec<_>>();
let list = (0..size).map(|x| vec![x]).collect::<Vec<_>>();
QuickFind { size, id, list }
}
pub fn root(&self, x: usize) -> usize {
assert!(x < self.size);
self.id[x]
}
pub fn same(&self, x: usize, y: usize) -> bool {
assert!(x < self.size);
assert!(y < self.size);
self.root(x) == self.root(y)
}
pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
assert!(x < self.size);
assert!(y < self.size);
let mut x = self.root(x);
let mut y = self.root(y);
if x == y {
return None;
}
if (self.list[x].len(), x) < (self.list[y].len(), y) {
std::mem::swap(&mut x, &mut y);
}
let mut z = std::mem::take(&mut self.list[y]);
z.iter().for_each(|y| self.id[*y] = x);
self.list[x].append(&mut z);
Some((x, y))
}
pub fn enumerate(&self, x: usize) -> &[usize] {
assert!(x < self.size);
&self.list[self.root(x)]
}
pub fn size(&self, x: usize) -> usize {
assert!(x < self.size);
self.enumerate(x).len()
}
}
// ---------- end quick find ----------
use std::io::*;
fn read() -> Vec<(Vec<u32>, Vec<u32>)> {
let mut s = String::new();
std::io::stdin().read_to_string(&mut s).unwrap();
let mut it = s.trim().split_whitespace().flat_map(|s| s.parse::<u32>());
let mut next = || it.next().unwrap();
let t = next();
(0..t).map(|_| {
let n = next();
let a = (0..n).map(|_| next()).collect();
let b = (0..n).map(|_| next()).collect();
(a, b)
}).collect()
}
fn main() {
let out = std::io::stdout();
let mut out = std::io::BufWriter::new(out.lock());
for (a, b) in read() {
let n = a.len();
let mut dsu = QuickFind::new(n);
for k in 2..=n {
for j in 2..=(n / k) {
dsu.unite(k * (j - 1) - 1, k * j - 1);
}
}
let mut a = a.iter().enumerate().map(|a| (dsu.root(a.0), *a.1)).collect::<Vec<_>>();
let mut b = b.iter().enumerate().map(|a| (dsu.root(a.0), *a.1)).collect::<Vec<_>>();
a.sort();
b.sort();
if a == b {
writeln!(out, "Yes").ok();
} else {
writeln!(out, "No").ok();
}
}
}
akakimidori