結果

問題 No.898 tri-βutree
ユーザー ziitaziita
提出日時 2019-10-06 19:04:00
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 5,334 bytes
コンパイル時間 17,195 ms
コンパイル使用メモリ 402,336 KB
実行使用メモリ 106,296 KB
最終ジャッジ日時 2024-10-10 22:37:36
合計ジャッジ時間 28,422 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

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 start_tour(&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(arr: Vec<Pair<usize,usize>>) -> sparse_table {
        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];
        let mut st = sparse_table {
            log_t: lt,
            table: vec![vec![Pair::new(0,0);sz+1];n],
            n: n,
        };
        for i in 0..n {
            st.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;}
                st.table[i][k] = {
                    let a = st.table[i][k - 1];
                    let b = st.table[i + (1 << (k - 1))][k - 1];
                    if a.x <= b.x {a}
                    else {b}
                };
            }
        }
        st
    }

    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}
    }
}

fn dist(x: usize, y: usize, euler: &euler_tour, st: &mut sparse_table) -> i64 {
    let c = st.query(min(euler.L[x],euler.L[y]), max(euler.R[x], euler.R[y])).y;
    euler.weight[x] + euler.weight[y] - 2*euler.weight[c]
}

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

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

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

    let mut st = sparse_table::new(arr);

    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,&euler,&mut st) + dist(y,z,&euler,&mut st) + dist(x,z,&euler,&mut st);
        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