結果

問題 No.2296 Union Path Query (Hard)
ユーザー akakimidoriakakimidori
提出日時 2023-03-15 00:03:48
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 4,568 bytes
コンパイル時間 15,470 ms
コンパイル使用メモリ 379,764 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-05-02 14:11:19
合計ジャッジ時間 23,653 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 5 ms
7,296 KB
testcase_02 AC 5 ms
7,168 KB
testcase_03 AC 5 ms
7,296 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

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();
    let mut dsu = DSU::new(n);
    let mut max = vec![0; n];
    let mut g = vec![vec![]; n];
    let bfs = |src: usize, g: &[Vec<(usize, i64)>]| -> Vec<(usize, usize, i64)> {
        let mut topo = vec![(src, src, 0)];
        for i in 0.. {
            if i >= topo.len() {
                break;
            }
            let (v, p, w) = topo[i];
            for &(u, c) in g[v].iter().filter(|e| e.0 != p) {
                topo.push((u, v, w + c));
            }
        }
        topo
    };
    for _ in 0..q {
        let op: u8 = sc.next();
        if op == 1 {
            let v: usize = sc.next();
            let w: i64 = sc.next();
            let a = bfs(x, &g).iter().map(|p| p.2).max().unwrap();
            let b = bfs(v, &g).iter().map(|p| p.2).max().unwrap();
            let (p, c) = dsu.unite(x, v).unwrap();
            max[p] = max[p].max(max[c]).max(w + a + b);
            g[x].push((v, w));
            g[v].push((x, w));
        } else if op == 2 {
            let u: usize = sc.next();
            let v: usize = sc.next();
            let memo = bfs(u, &g);
            if let Some(p) = memo.iter().find(|p| p.0 == v) {
                x += p.2 as usize;
                writeln!(out, "{}", p.2).ok();
            } else {
                writeln!(out, "-1").ok();
            }
        } else if op == 3 {
            let v: usize = sc.next();
            writeln!(out, "{}", max[dsu.root(v)]).ok();
        } else {
            let value: usize = sc.next();
            x += value;
        }
        x %= n;
    }
}
0