結果

問題 No.439 チワワのなる木
ユーザー koba-e964koba-e964
提出日時 2021-09-29 11:05:26
言語 Rust
(1.77.0)
結果
AC  
実行時間 94 ms / 5,000 ms
コード長 2,682 bytes
コンパイル時間 2,217 ms
コンパイル使用メモリ 155,012 KB
実行使用メモリ 28,084 KB
最終ジャッジ日時 2023-09-23 08:31:11
合計ジャッジ時間 4,844 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 50 ms
8,700 KB
testcase_19 AC 45 ms
8,504 KB
testcase_20 AC 72 ms
10,868 KB
testcase_21 AC 18 ms
4,380 KB
testcase_22 AC 16 ms
4,376 KB
testcase_23 AC 79 ms
12,932 KB
testcase_24 AC 94 ms
24,640 KB
testcase_25 AC 59 ms
11,672 KB
testcase_26 AC 55 ms
12,288 KB
testcase_27 AC 53 ms
28,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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:ty) => ($next().parse::<$t>().expect("Parse error"));
}

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>], s: &[char],
        c: &mut [i64], w: &mut [i64]) {
    if s[v] == 'c' {
        c[v] += 1;
    } else {
        w[v] += 1;
    }
    for &ww in &g[v] {
        if ww == par { continue; }
        dfs1(ww, v, g, s, c, w);
        c[v] += c[ww];
        w[v] += w[ww];
    }
}

fn dfs2(v: usize, par: usize, g: &[Vec<usize>], s: &[char],
        c: &[i64], w: &[i64]) -> i64 {
    let mut ans = 0;
    let mut sc = c[0];
    let mut sw = w[0];
    if s[v] == 'w' {
        sw -= 1;
    } else {
        sc -= 1;
    }
    if s[v] == 'w' {
        ans += sc * sw;
    }
    for &ww in &g[v] {
        if ww == par { continue; }
        ans += dfs2(ww, v, g, s, c, w);
        if s[v] == 'w' {
            ans -= c[ww] * w[ww];
        }
        sc -= c[ww];
        sw -= w[ww];
    }
    if s[v] == 'w' {
        ans -= sc * sw;
    }
    ans
}

fn solve() {
    input! {
        n: usize,
        s: chars,
        ab: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for &(a, b) in &ab {
        g[a].push(b);
        g[b].push(a);
    }
    let mut c = vec![0i64; n];
    let mut w = vec![0i64; n];
    dfs1(0, n, &g, &s, &mut c, &mut w);
    println!("{}", dfs2(0, n, &g, &s, &c, &w));
}
0