結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー akakimidoriakakimidori
提出日時 2021-03-05 21:56:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 2,345 bytes
コンパイル時間 1,176 ms
コンパイル使用メモリ 169,776 KB
実行使用メモリ 12,920 KB
最終ジャッジ日時 2024-04-16 09:24:41
合計ジャッジ時間 3,237 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 50 ms
12,496 KB
testcase_04 AC 61 ms
12,628 KB
testcase_05 AC 62 ms
12,500 KB
testcase_06 AC 56 ms
12,368 KB
testcase_07 AC 57 ms
12,632 KB
testcase_08 AC 31 ms
9,032 KB
testcase_09 AC 13 ms
5,376 KB
testcase_10 AC 15 ms
5,800 KB
testcase_11 AC 7 ms
5,376 KB
testcase_12 AC 22 ms
7,504 KB
testcase_13 AC 28 ms
8,548 KB
testcase_14 AC 30 ms
8,928 KB
testcase_15 AC 21 ms
7,136 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 53 ms
11,528 KB
testcase_19 AC 7 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 20 ms
6,924 KB
testcase_22 AC 16 ms
6,356 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 8 ms
5,376 KB
testcase_34 AC 49 ms
12,328 KB
testcase_35 AC 15 ms
6,340 KB
testcase_36 AC 4 ms
5,376 KB
testcase_37 AC 30 ms
12,920 KB
testcase_38 AC 27 ms
12,036 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
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_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_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 ----------

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 parent = vec![0; n];
    let mut topo = vec![0];
    for i in 0..n {
        let v = topo[i];
        for u in g[v].clone() {
            let x = g[u].iter().position(|p| *p == v).unwrap();
            g[u].remove(x);
            parent[u] = v;
            topo.push(u);
        }
    }
    let mut size = vec![1usize; n];
    let mut ans = 0;
    for &v in topo.iter().rev() {
        let mut c = vec![];
        for &u in g[v].iter() {
            size[v] += size[u];
            c.push(size[u]);
        }
        if size[v] < n {
            c.push(n - size[v]);
        }
        for c in c.iter() {
            ans += *c * (n - *c);
        }
    }
    ans += n * n;
    println!("{}", ans);
}

fn main() {
    run();
}
0