結果

問題 No.2296 Union Path Query (Hard)
ユーザー akakimidoriakakimidori
提出日時 2023-03-15 02:24:36
言語 Rust
(1.77.0)
結果
AC  
実行時間 988 ms / 7,000 ms
コード長 6,151 bytes
コンパイル時間 2,405 ms
コンパイル使用メモリ 159,916 KB
実行使用メモリ 72,640 KB
最終ジャッジ日時 2023-08-15 01:46:38
合計ジャッジ時間 34,706 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 32 ms
40,188 KB
testcase_02 AC 32 ms
40,260 KB
testcase_03 AC 33 ms
40,100 KB
testcase_04 AC 354 ms
54,468 KB
testcase_05 AC 345 ms
54,572 KB
testcase_06 AC 507 ms
57,680 KB
testcase_07 AC 503 ms
65,424 KB
testcase_08 AC 509 ms
65,400 KB
testcase_09 AC 446 ms
38,908 KB
testcase_10 AC 427 ms
38,996 KB
testcase_11 AC 435 ms
38,908 KB
testcase_12 AC 403 ms
34,940 KB
testcase_13 AC 110 ms
8,876 KB
testcase_14 AC 85 ms
7,568 KB
testcase_15 AC 553 ms
59,784 KB
testcase_16 AC 502 ms
47,436 KB
testcase_17 AC 257 ms
20,660 KB
testcase_18 AC 787 ms
67,256 KB
testcase_19 AC 610 ms
39,776 KB
testcase_20 AC 768 ms
67,340 KB
testcase_21 AC 734 ms
63,848 KB
testcase_22 AC 742 ms
63,400 KB
testcase_23 AC 533 ms
70,272 KB
testcase_24 AC 667 ms
40,292 KB
testcase_25 AC 328 ms
60,924 KB
testcase_26 AC 328 ms
60,872 KB
testcase_27 AC 988 ms
60,908 KB
testcase_28 AC 766 ms
60,848 KB
testcase_29 AC 844 ms
62,848 KB
testcase_30 AC 874 ms
62,896 KB
testcase_31 AC 924 ms
61,404 KB
testcase_32 AC 902 ms
60,376 KB
testcase_33 AC 739 ms
58,320 KB
testcase_34 AC 544 ms
55,932 KB
testcase_35 AC 458 ms
54,056 KB
testcase_36 AC 325 ms
51,768 KB
testcase_37 AC 739 ms
46,464 KB
testcase_38 AC 780 ms
46,452 KB
testcase_39 AC 858 ms
46,496 KB
testcase_40 AC 896 ms
46,464 KB
testcase_41 AC 1 ms
4,384 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 1 ms
4,384 KB
testcase_44 AC 649 ms
71,576 KB
testcase_45 AC 651 ms
71,352 KB
testcase_46 AC 709 ms
71,824 KB
testcase_47 AC 713 ms
72,640 KB
testcase_48 AC 683 ms
71,460 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();
    let mut dsu = DSU::new(n);
    let mut g = vec![vec![]; n];
    let mut depth = vec![(0, 0); n]; // 根からの辺の個数、重みの和
    let mut table = vec![vec![0; n]; 18];
    let mut diameter = (0..n).map(|v| (0, v, v)).collect::<Vec<_>>(); // 直径, 端点
    for table in table.iter_mut() {
        for (j, t) in table.iter_mut().enumerate() {
            *t = j;
        }
    }
    for _ in 0..q {
        let op: u8 = sc.next();
        if op == 1 {
            let v: usize = sc.next();
            let w: i64 = sc.next();
            let u = dsu.root(v);
            g[v].push((x, w));
            g[x].push((v, w));
            let (p, c) = dsu.unite(x, v).unwrap();
            let (a, b) = if u == c { (x, v) } else { (v, x) };
            let topo = bfs(b, a, &g);
            for &(v, p, c, d) in topo.iter() {
                table[0][v] = p;
                let p = depth[a];
                depth[v] = (p.0 + 1 + c, p.1 + w + d);
            }
            for i in 1..table.len() {
                for &(v, _, _, _) in topo.iter() {
                    table[i][v] = table[i - 1][table[i - 1][v]];
                }
            }
            let a = diameter[p];
            let b = diameter[c];
            let mut next = a.max(b);
            for a in [a.1, a.2].iter() {
                for b in [b.1, b.2].iter() {
                    let d = dist(*a, *b, &depth, &table);
                    next = (d, *a, *b).max(next);
                }
            }
            diameter[p] = next;
        } else if op == 2 {
            let u: usize = sc.next();
            let v: usize = sc.next();
            if dsu.same(u, v) {
                let d = dist(u, v, &depth, &table);
                writeln!(out, "{}", d).ok();
                x += d as usize;
            } else {
                writeln!(out, "-1").ok();
            }
        } else if op == 3 {
            let v: usize = sc.next();
            writeln!(out, "{}", diameter[dsu.root(v)].0).ok();
        } else {
            let value: usize = sc.next();
            x += value;
        }
        x %= n;
    }
}

pub fn bfs(src: usize, p: usize, g: &[Vec<(usize, i64)>]) -> Vec<(usize, usize, usize, i64)> {
    let mut topo = vec![(src, p, 0, 0)];
    for i in 0.. {
        if i >= topo.len() {
            break;
        }
        let (v, p, c, d) = topo[i];
        for &(u, w) in g[v].iter().filter(|e| e.0 != p) {
            topo.push((u, v, c + 1, d + w));
        }
    }
    topo
}

pub fn dist(x: usize, y: usize, depth: &[(usize, i64)], table: &[Vec<usize>]) -> i64 {
    let mut a = x;
    let mut b = y;
    if depth[a].0 < depth[b].0 {
        std::mem::swap(&mut a, &mut b);
    }
    for (i, table) in table.iter().enumerate().rev() {
        if (depth[a].0 - depth[b].0) >> i & 1 == 1 {
            a = table[a];
        }
    }
    if a == b {
        return depth[x].1.max(depth[y].1) - depth[a].1;
    }
    for table in table.iter().rev() {
        if table[a] != table[b] {
            a = table[a];
            b = table[b];
        }
    }
    let lca = table[0][a];
    depth[x].1 + depth[y].1 - 2 * depth[lca].1
}
0