結果

問題 No.898 tri-βutree
ユーザー ziitaziita
提出日時 2019-10-06 19:45:15
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 479 ms / 4,000 ms
コード長 5,992 bytes
コンパイル時間 27,093 ms
コンパイル使用メモリ 377,920 KB
実行使用メモリ 104,320 KB
最終ジャッジ日時 2024-11-08 22:55:35
合計ジャッジ時間 22,672 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 352 ms
104,320 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 1 ms
5,248 KB
testcase_07 AC 441 ms
97,024 KB
testcase_08 AC 446 ms
96,896 KB
testcase_09 AC 455 ms
97,024 KB
testcase_10 AC 479 ms
97,024 KB
testcase_11 AC 463 ms
96,896 KB
testcase_12 AC 462 ms
96,896 KB
testcase_13 AC 453 ms
97,024 KB
testcase_14 AC 465 ms
96,896 KB
testcase_15 AC 453 ms
97,024 KB
testcase_16 AC 437 ms
96,896 KB
testcase_17 AC 436 ms
97,024 KB
testcase_18 AC 433 ms
96,896 KB
testcase_19 AC 435 ms
97,024 KB
testcase_20 AC 428 ms
96,896 KB
testcase_21 AC 433 ms
96,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(dead_code)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;
use std::io::{Write, BufWriter};

const MOD: usize = 998244353;

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[derive(Copy,Clone)]
struct Pair<T,U> {
    x: T,
    y: U,
}

impl<T,U> Pair<T,U> {
    fn new(x: T, y: U) -> Pair<T,U> {
        Pair {
            x: x,
            y: y,
        }
    }
}

struct euler_tour {
    G: Vec<Vec<Pair<usize,i64>>>,
    tour: Vec<usize>,
    L: Vec<usize>,
    R: Vec<usize>,
    depth: Vec<usize>,
    weight: Vec<i64>,
    n: usize,
}

impl euler_tour{
    fn new(n: usize) -> euler_tour {
        euler_tour{
            G: vec![vec![];n],
            tour: vec![],
            L: vec![0;n],
            R: vec![0;n],
            depth: vec![0;n],
            weight: vec![0;n],
            n: n,
        }
    }

    fn add_edge(&mut self, s: usize, t: usize, w: i64){
        self.G[s].push(Pair::new(t,w));
        self.G[t].push(Pair::new(s,w));
    }

    fn init(&mut self, root: usize) {
        self.dfs(root, self.n, 0, 0);
    }

    fn dfs(&mut self, v: usize, p: usize, d: usize, w: i64){
        self.tour.push(v);
        self.L[v] = self.tour.len()-1;
        self.depth[v] = d;
        self.weight[v] = w;
        for z in self.G[v].clone() {
            if z.x == p {continue;}
            self.dfs(z.x, v, d+1, w+z.y);
            self.tour.push(v);
        }
        self.R[v] = self.tour.len()-1;
    }
}

struct sparse_table {
    log_t: Vec<usize>,
    table: Vec<Vec<Pair<usize,usize>>>,
    n: usize,
}

impl sparse_table {

    fn new() -> sparse_table{
        sparse_table{
            log_t: vec![],
            table: vec![vec![]],
            n: 0,
        }
    }

    fn init(&mut self, arr: Vec<Pair<usize,usize>>) {
        let n = arr.len();
        let mut lt: Vec<usize> = vec![0;n+1];
        for i in 2..n+1 {
            lt[i] = lt[i >> 1] + 1;
        }
        let sz = lt[n];

        self.log_t = lt;
        self.table = vec![vec![Pair::new(usize::max_value(),0);sz+1];n];
        self.n = n;

        for i in 0..n {
            self.table[i][0] = arr[i];
        }
        for k in 1..n {
            if (1 << k) > n {break;}
            for i in 0..n {
                if i + (1 << k) > n {break;}
                self.table[i][k] = {
                    let a = self.table[i][k - 1];
                    let b = self.table[i + (1 << (k - 1))][k - 1];
                    if a.x <= b.x {a}
                    else {b}
                };
            }
        }
    }

    fn query(&mut self, s: usize, t: usize) -> Pair<usize,usize> {
        let k = self.log_t[t-s+1];
        let a = self.table[s][k];
        let b = self.table[t + 1 - (1 << k)][k];
        if a.x <= b.x {a}
        else {b}
    }
}

struct LCA{
    euler: euler_tour,
    st: sparse_table,
    n: usize,
}

impl LCA{
    fn new(n: usize) -> LCA {
        LCA{
            euler: euler_tour::new(n),
            st: sparse_table::new(),
            n: n,
        }
    }

    fn add_edge(&mut self, s: usize, t: usize, w: i64){
        self.euler.G[s].push(Pair::new(t,w));
        self.euler.G[t].push(Pair::new(s,w));
    }

    fn init(&mut self) {
        self.euler.dfs(0, self.n, 0, 0);
        let sz = self.euler.tour.len();
        let mut arr: Vec<Pair<usize,usize>> = vec![Pair::new(0,0);sz];
        for i in 0..sz {
            arr[i] = Pair::new(self.euler.depth[self.euler.tour[i]], self.euler.tour[i]);
        }
        self.st.init(arr);
    }

    fn lca(&mut self, s: usize, t: usize) -> usize {
        self.st.query(min(self.euler.L[s],self.euler.L[t]), max(self.euler.R[s], self.euler.R[t])).y
    }
}

fn dist(x: usize, y: usize, lca: &mut LCA) -> i64 {
    let c = lca.lca(x,y);
    lca.euler.weight[x] + lca.euler.weight[y] - 2*lca.euler.weight[c]
}

fn solve() {
    input! {
        n: usize,
        a: [[usize;3];n-1],
        q: usize,
        b: [[usize;3];q],
    }

    let mut lca = LCA::new(n);
    for i in 0..n-1{
        lca.add_edge(a[i][0], a[i][1], a[i][2] as i64);
    }
    lca.init();

    for i in 0..q {
        let x = b[i][0];
        let y = b[i][1];
        let z = b[i][2];
        let ans = dist(x,y,&mut lca) + dist(y,z,&mut lca) + dist(x,z,&mut lca);
        println!("{}", ans/2);
    }
}

fn main() {
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}
0