結果

問題 No.2295 Union Path Query (Medium)
ユーザー akakimidoriakakimidori
提出日時 2023-03-14 23:36:16
言語 Rust
(1.77.0)
結果
AC  
実行時間 143 ms / 4,000 ms
コード長 4,468 bytes
コンパイル時間 12,086 ms
コンパイル使用メモリ 378,988 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-02 03:28:17
合計ジャッジ時間 20,488 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 101 ms
6,528 KB
testcase_04 AC 143 ms
6,912 KB
testcase_05 AC 61 ms
7,296 KB
testcase_06 AC 63 ms
7,808 KB
testcase_07 AC 93 ms
6,656 KB
testcase_08 AC 111 ms
9,984 KB
testcase_09 AC 52 ms
10,112 KB
testcase_10 AC 45 ms
9,984 KB
testcase_11 AC 48 ms
10,624 KB
testcase_12 AC 49 ms
10,112 KB
testcase_13 AC 53 ms
9,344 KB
testcase_14 AC 38 ms
6,656 KB
testcase_15 AC 34 ms
9,600 KB
testcase_16 AC 32 ms
9,600 KB
testcase_17 AC 39 ms
9,728 KB
testcase_18 AC 54 ms
9,856 KB
testcase_19 AC 57 ms
9,856 KB
testcase_20 AC 45 ms
9,856 KB
testcase_21 AC 44 ms
9,728 KB
testcase_22 AC 55 ms
9,856 KB
testcase_23 AC 44 ms
9,472 KB
testcase_24 AC 43 ms
9,088 KB
testcase_25 AC 36 ms
9,472 KB
testcase_26 AC 41 ms
9,216 KB
testcase_27 AC 43 ms
9,088 KB
testcase_28 AC 41 ms
9,088 KB
testcase_29 AC 41 ms
9,088 KB
testcase_30 AC 43 ms
9,088 KB
testcase_31 AC 43 ms
9,216 KB
testcase_32 AC 44 ms
9,088 KB
testcase_33 AC 45 ms
8,960 KB
testcase_34 AC 43 ms
9,216 KB
testcase_35 AC 41 ms
9,216 KB
testcase_36 AC 41 ms
9,344 KB
testcase_37 AC 41 ms
9,216 KB
testcase_38 AC 41 ms
9,216 KB
testcase_39 AC 42 ms
9,216 KB
testcase_40 AC 41 ms
9,216 KB
testcase_41 AC 41 ms
9,216 KB
testcase_42 AC 43 ms
9,216 KB
testcase_43 AC 42 ms
9,216 KB
testcase_44 AC 40 ms
9,088 KB
testcase_45 AC 71 ms
9,088 KB
testcase_46 AC 73 ms
9,472 KB
testcase_47 AC 86 ms
9,856 KB
testcase_48 AC 66 ms
10,624 KB
testcase_49 AC 64 ms
10,624 KB
testcase_50 AC 74 ms
8,832 KB
testcase_51 AC 47 ms
9,088 KB
testcase_52 AC 39 ms
8,832 KB
testcase_53 AC 42 ms
9,216 KB
testcase_54 AC 44 ms
10,112 KB
testcase_55 AC 42 ms
9,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//---------- 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;
    }
}
0