結果

問題 No.1769 Don't Stop the Game
ユーザー koba-e964koba-e964
提出日時 2021-11-30 01:10:09
言語 Rust
(1.72.1)
結果
WA  
実行時間 -
コード長 4,192 bytes
コンパイル時間 12,032 ms
コンパイル使用メモリ 161,420 KB
実行使用メモリ 79,764 KB
最終ジャッジ日時 2023-09-15 10:41:26
合計ジャッジ時間 20,035 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused macro definition: `puts`
  --> Main.rs:98:18
   |
98 |     macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
   |                  ^^^^
   |
   = note: `#[warn(unused_macros)]` on by default

warning: unused import: `Write`
 --> Main.rs:5:15
  |
5 | use std::io::{Write, BufWriter};
  |               ^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused variable: `out`
  --> Main.rs:97:13
   |
97 |     let mut out = BufWriter::new(out.lock());
   |             ^^^ help: if this is intentional, prefix it with an underscore: `_out`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: variable does not need to be mutable
  --> Main.rs:97:9
   |
97 |     let mut out = BufWriter::new(out.lock());
   |         ----^^^
   |         |
   |         help: remove this `mut`
   |
   = note: `#[warn(unused_mut)]` on by default

warning: 4 warnings emitted

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// 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);
        read_value!($next, [$t; len])
    }};
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

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();
}

fn dfs1(v: usize, par: usize, g: &[Vec<(usize, u32)>], dp: &mut [u32], x: u32) {
    dp[v] = x;
    for &(w, c) in &g[v] {
        if par == w { continue; }
        dfs1(w, v, g, dp, x ^ c);
    }
}

fn dfs2(v: usize, par: usize, g: &[Vec<(usize, u32)>], coo: &[u32],
        dp: &[u32], pre: &mut [usize],
        ch: &mut [Vec<usize>], root: &mut [Vec<usize>]) {
    let idx = coo.binary_search(&dp[v]).unwrap();
    let n = g.len();
    if pre[idx] == n {
        root[idx].push(v);
    } else {
        ch[pre[idx]].push(v);
    }
    pre[idx] = v;
    for &(w, _) in &g[v] {
        if par == w { continue; }
        dfs2(w, v, g, coo, dp, pre, ch, root);
    }
}

fn dfs3(v: usize, par: usize, g: &[Vec<(usize, u32)>], sz: &mut [i32]) {
    let mut s = 1;
    for &(w, _) in &g[v] {
        if par == w { continue; }
        dfs3(w, v, g, sz);
        s += sz[w];
    }
    sz[v] = s;
}


fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););}
    #[allow(unused)]
    macro_rules! putvec {
        ($v:expr) => {
            for i in 0..$v.len() {
                puts!("{}{}", $v[i], if i + 1 == $v.len() {"\n"} else {" "});
            }
        }
    }
    input! {
        n: usize,
        abc: [(usize1, usize1, u32); n - 1],
    }
    let mut g = vec![vec![]; n];
    for &(a, b, c) in &abc {
        g[a].push((b, c));
        g[b].push((a, c));
    }
    let mut dp = vec![0; n];
    dfs1(0, n, &g, &mut dp, 0);
    let mut coo = dp.clone();
    coo.sort(); coo.dedup();
    let m = coo.len();
    let mut pre = vec![n; m];
    let mut ch = vec![vec![]; n];
    let mut root = vec![vec![]; m];
    dfs2(0, n, &g, &coo, &dp, &mut pre, &mut ch, &mut root);
    let mut sz = vec![0; n];
    dfs3(0, n, &g, &mut sz);
    let mut ans = 0i64;
    for i in 0..m {
        let mut cs = n as i32;
        let mut t = 0;
        for &v in &root[i] {
            cs -= sz[v];
            t += 1;
        }
        ans += cs as i64 * t;
    }
    for i in 0..n {
        let mut cs = sz[i];
        let mut t = 0;
        for &v in &ch[i] {
            cs -= sz[v];
            t += 1;
        }
        ans += cs as i64 * t;
    }
    println!("{}", ans);
}
0