結果

問題 No.1928 Make a Binary Tree
ユーザー akakimidoriakakimidori
提出日時 2022-05-06 22:07:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 184 ms / 3,000 ms
コード長 2,364 bytes
コンパイル時間 1,651 ms
コンパイル使用メモリ 157,396 KB
実行使用メモリ 33,316 KB
最終ジャッジ日時 2023-09-20 03:02:56
合計ジャッジ時間 8,068 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 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 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 5 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 3 ms
4,384 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 5 ms
4,376 KB
testcase_25 AC 5 ms
4,384 KB
testcase_26 AC 147 ms
28,744 KB
testcase_27 AC 73 ms
17,992 KB
testcase_28 AC 79 ms
19,316 KB
testcase_29 AC 164 ms
31,728 KB
testcase_30 AC 67 ms
16,916 KB
testcase_31 AC 95 ms
21,396 KB
testcase_32 AC 164 ms
31,096 KB
testcase_33 AC 74 ms
18,228 KB
testcase_34 AC 99 ms
21,492 KB
testcase_35 AC 107 ms
33,300 KB
testcase_36 AC 68 ms
31,404 KB
testcase_37 AC 58 ms
32,144 KB
testcase_38 AC 1 ms
4,384 KB
testcase_39 AC 99 ms
32,360 KB
testcase_40 AC 100 ms
32,612 KB
testcase_41 AC 105 ms
32,440 KB
testcase_42 AC 104 ms
32,408 KB
testcase_43 AC 109 ms
32,472 KB
testcase_44 AC 100 ms
32,444 KB
testcase_45 AC 101 ms
32,480 KB
testcase_46 AC 101 ms
32,428 KB
testcase_47 AC 101 ms
32,488 KB
testcase_48 AC 99 ms
32,324 KB
testcase_49 AC 65 ms
33,316 KB
testcase_50 AC 172 ms
30,064 KB
testcase_51 AC 176 ms
30,052 KB
testcase_52 AC 164 ms
30,152 KB
testcase_53 AC 171 ms
30,092 KB
testcase_54 AC 167 ms
30,072 KB
testcase_55 AC 57 ms
30,084 KB
testcase_56 AC 176 ms
31,912 KB
testcase_57 AC 183 ms
31,844 KB
testcase_58 AC 184 ms
31,916 KB
testcase_59 AC 80 ms
27,984 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:28:14
   |
28 |         for (i, &u) in g[v].iter().enumerate() {
   |              ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

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;
        }
        let mut pq = BinaryHeap::new();
        for (i, &u) in g[v].iter().enumerate() {
            pq.push(size[u]);
            pq.append(&mut h[u]);
        }
        for _ in 0..2 {
            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