結果
問題 | No.2295 Union Path Query (Medium) |
ユーザー | akakimidori |
提出日時 | 2023-03-14 23:36:16 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 136 ms / 4,000 ms |
コード長 | 4,468 bytes |
コンパイル時間 | 14,944 ms |
コンパイル使用メモリ | 397,304 KB |
実行使用メモリ | 10,624 KB |
最終ジャッジ日時 | 2024-11-22 13:43:26 |
合計ジャッジ時間 | 23,918 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 121 ms
6,528 KB |
testcase_04 | AC | 83 ms
6,912 KB |
testcase_05 | AC | 136 ms
7,296 KB |
testcase_06 | AC | 58 ms
7,680 KB |
testcase_07 | AC | 113 ms
6,656 KB |
testcase_08 | AC | 61 ms
9,984 KB |
testcase_09 | AC | 58 ms
9,984 KB |
testcase_10 | AC | 59 ms
9,984 KB |
testcase_11 | AC | 67 ms
10,496 KB |
testcase_12 | AC | 63 ms
10,112 KB |
testcase_13 | AC | 56 ms
9,216 KB |
testcase_14 | AC | 44 ms
6,528 KB |
testcase_15 | AC | 47 ms
9,600 KB |
testcase_16 | AC | 45 ms
9,600 KB |
testcase_17 | AC | 54 ms
9,728 KB |
testcase_18 | AC | 70 ms
9,856 KB |
testcase_19 | AC | 75 ms
9,856 KB |
testcase_20 | AC | 63 ms
9,856 KB |
testcase_21 | AC | 57 ms
9,856 KB |
testcase_22 | AC | 73 ms
9,728 KB |
testcase_23 | AC | 56 ms
9,600 KB |
testcase_24 | AC | 53 ms
9,088 KB |
testcase_25 | AC | 47 ms
9,472 KB |
testcase_26 | AC | 58 ms
9,216 KB |
testcase_27 | AC | 56 ms
9,088 KB |
testcase_28 | AC | 55 ms
9,088 KB |
testcase_29 | AC | 55 ms
9,216 KB |
testcase_30 | AC | 58 ms
9,088 KB |
testcase_31 | AC | 61 ms
9,088 KB |
testcase_32 | AC | 61 ms
9,088 KB |
testcase_33 | AC | 60 ms
9,216 KB |
testcase_34 | AC | 56 ms
9,216 KB |
testcase_35 | AC | 58 ms
9,344 KB |
testcase_36 | AC | 57 ms
9,088 KB |
testcase_37 | AC | 59 ms
9,088 KB |
testcase_38 | AC | 59 ms
9,088 KB |
testcase_39 | AC | 56 ms
9,216 KB |
testcase_40 | AC | 60 ms
9,216 KB |
testcase_41 | AC | 58 ms
9,216 KB |
testcase_42 | AC | 54 ms
9,216 KB |
testcase_43 | AC | 52 ms
9,344 KB |
testcase_44 | AC | 53 ms
9,088 KB |
testcase_45 | AC | 53 ms
9,088 KB |
testcase_46 | AC | 62 ms
9,600 KB |
testcase_47 | AC | 64 ms
9,984 KB |
testcase_48 | AC | 73 ms
10,624 KB |
testcase_49 | AC | 72 ms
10,624 KB |
testcase_50 | AC | 49 ms
8,832 KB |
testcase_51 | AC | 49 ms
9,088 KB |
testcase_52 | AC | 54 ms
8,832 KB |
testcase_53 | AC | 64 ms
9,216 KB |
testcase_54 | AC | 52 ms
10,112 KB |
testcase_55 | AC | 47 ms
9,984 KB |
ソースコード
//---------- 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 ---------- // ---------- begin scannner ---------- #[allow(dead_code)] mod scanner { use std::str::FromStr; pub struct Scanner<'a> { it: std::str::SplitWhitespace<'a>, } impl<'a> Scanner<'a> { pub fn new(s: &'a String) -> Scanner<'a> { Scanner { it: s.split_whitespace(), } } pub fn next<T: FromStr>(&mut self) -> T { self.it.next().unwrap().parse::<T>().ok().unwrap() } pub fn next_bytes(&mut self) -> Vec<u8> { self.it.next().unwrap().bytes().collect() } pub fn next_chars(&mut self) -> Vec<char> { self.it.next().unwrap().chars().collect() } pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> { (0..len).map(|_| self.next()).collect() } } } // ---------- end scannner ---------- use std::io::Write; fn main() { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut sc = scanner::Scanner::new(&s); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); run(&mut sc, &mut out); } fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) { let n: usize = sc.next(); let mut x: usize = sc.next(); let q: usize = sc.next(); const MOD: usize = 998_244_353; let mut dsu = DSU::new(n); let mut sum = vec![0; n];// 連結成分内の距離の総和 let mut weight = vec![std::usize::MAX; n]; for _ in 0..q { let op: u8 = sc.next(); if op == 1 { let v: usize = sc.next(); let w: usize = sc.next(); if !dsu.same(x, v) { let add = w * dsu.size(x) % MOD * dsu.size(v) % MOD; let (p, c) = dsu.unite(x, v).unwrap(); sum[p] = (sum[p] + add + sum[c]) % MOD; weight[c] = w; } } else if op == 2 { let u: usize = sc.next(); let v: usize = sc.next(); if dsu.same(u, v) { let mut w = 0; let mut u = u; let mut v = v; while u != v { if weight[u] > weight[v] { std::mem::swap(&mut u, &mut v); } w = weight[u]; u = dsu.parent(u).unwrap(); } writeln!(out, "{}", w).ok(); x += w; } else { writeln!(out, "-1").ok(); } } else if op == 3 { let v: usize = sc.next(); writeln!(out, "{}", sum[dsu.root(v)]).ok(); } else { let value: usize = sc.next(); x += value; } x %= n; } }