結果
| 問題 |
No.1779 Magical Swap
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2021-12-08 01:57:41 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 45 ms / 2,000 ms |
| コード長 | 2,798 bytes |
| コンパイル時間 | 20,463 ms |
| コンパイル使用メモリ | 380,048 KB |
| 実行使用メモリ | 13,512 KB |
| 最終ジャッジ日時 | 2024-07-16 07:39:06 |
| 合計ジャッジ時間 | 16,506 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
//---------- begin union_find ----------
pub struct DSU {
p: Vec<i32>,
}
impl DSU {
pub fn new(n: usize) -> DSU {
assert!(n < std::i32::MAX as usize);
DSU { p: vec![-1; n] }
}
pub fn init(&mut self) {
self.p.iter_mut().for_each(|p| *p = -1);
}
pub fn root(&self, mut x: usize) -> usize {
assert!(x < self.p.len());
while self.p[x] >= 0 {
x = self.p[x] as usize;
}
x
}
pub fn same(&self, x: usize, y: usize) -> bool {
assert!(x < self.p.len() && y < self.p.len());
self.root(x) == self.root(y)
}
pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
assert!(x < self.p.len() && y < self.p.len());
let mut x = self.root(x);
let mut y = self.root(y);
if x == y {
return None;
}
if self.p[x] > self.p[y] {
std::mem::swap(&mut x, &mut y);
}
self.p[x] += self.p[y];
self.p[y] = x as i32;
Some((x, y))
}
pub fn parent(&self, x: usize) -> Option<usize> {
assert!(x < self.p.len());
let p = self.p[x];
if p >= 0 {
Some(p as usize)
} else {
None
}
}
pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
where
F: FnMut(usize),
{
while let Some(p) = self.parent(x) {
f(x);
x = p;
}
x
}
pub fn size(&self, x: usize) -> usize {
assert!(x < self.p.len());
let r = self.root(x);
(-self.p[r]) as usize
}
}
//---------- end union_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 = DSU::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