結果

問題 No.1928 Make a Binary Tree
ユーザー akakimidoriakakimidori
提出日時 2022-05-06 22:06:09
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 2,508 bytes
コンパイル時間 1,372 ms
コンパイル使用メモリ 151,528 KB
実行使用メモリ 31,352 KB
最終ジャッジ日時 2023-09-20 03:00:53
合計ジャッジ時間 6,905 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 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 WA -
testcase_14 AC 1 ms
4,380 KB
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 -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 73 ms
26,984 KB
testcase_36 AC 55 ms
28,792 KB
testcase_37 AC 54 ms
31,352 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 77 ms
27,948 KB
testcase_40 AC 81 ms
28,020 KB
testcase_41 AC 76 ms
27,856 KB
testcase_42 AC 75 ms
27,984 KB
testcase_43 AC 74 ms
27,988 KB
testcase_44 AC 78 ms
27,964 KB
testcase_45 AC 76 ms
28,004 KB
testcase_46 AC 76 ms
27,984 KB
testcase_47 AC 81 ms
27,836 KB
testcase_48 AC 75 ms
27,908 KB
testcase_49 AC 50 ms
26,984 KB
testcase_50 AC 138 ms
26,968 KB
testcase_51 AC 128 ms
27,004 KB
testcase_52 AC 128 ms
26,968 KB
testcase_53 AC 128 ms
26,976 KB
testcase_54 AC 134 ms
26,976 KB
testcase_55 AC 48 ms
27,132 KB
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 AC 82 ms
28,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;

fn run() {
    input! {
        n: usize,
        e: [(usize1, usize1); n - 1],
    }
    let mut g = vec![vec![]; n];
    for (a, b) in e {
        g[a].push(b);
        g[b].push(a);
    }
    let mut topo = vec![0];
    for i in 0..n {
        let v = topo[i];
        for u in g[v].clone() {
            g[u].retain(|p| *p != v);
            topo.push(u);
        }
    }
    let mut size = vec![1u32; n];
    let mut h = vec![BinaryHeap::new(); n];
    for &v in topo.iter().rev() {
        if g[v].is_empty() {
            continue;
        }
        g[v].sort_by_key(|u| !size[*u]);
        let mut pq = BinaryHeap::new();
        for (i, &u) in g[v].iter().enumerate() {
            if i < 2 {
                size[v] += size[u];
            } else {
                pq.push(size[u]);
            }
            pq.append(&mut h[u]);
        }
        if g[v].len() == 1 {
            size[v] += pq.pop().unwrap_or(0);
        }
        h[v] = pq;
    }
    println!("{}", size[0]);
}

fn main() {
    run();
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------
0