結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー StrorkisStrorkis
提出日時 2021-03-05 22:36:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,508 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 158,708 KB
実行使用メモリ 17,792 KB
最終ジャッジ日時 2024-04-16 10:22:40
合計ジャッジ時間 2,553 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 38 ms
10,112 KB
testcase_04 AC 32 ms
10,112 KB
testcase_05 AC 35 ms
10,112 KB
testcase_06 AC 36 ms
9,984 KB
testcase_07 AC 35 ms
10,112 KB
testcase_08 AC 20 ms
7,424 KB
testcase_09 AC 8 ms
5,376 KB
testcase_10 AC 9 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 15 ms
6,272 KB
testcase_13 AC 18 ms
7,040 KB
testcase_14 AC 17 ms
7,296 KB
testcase_15 AC 13 ms
6,016 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 25 ms
9,472 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 13 ms
5,888 KB
testcase_22 AC 10 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 1 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 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 7 ms
5,504 KB
testcase_34 AC 39 ms
17,792 KB
testcase_35 AC 13 ms
8,448 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 22 ms
9,856 KB
testcase_38 AC 21 ms
9,344 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 #

fn dfs(ans: &mut usize, graph: &[Vec<usize>], prev: usize, from: usize) -> usize {
    let n = graph.len();
    let mut count = 0;
    for &to in &graph[from] {
        if to == prev { continue; }
        let x = dfs(ans, graph, from, to);
        *ans += x * (n - x);
        count += x;
    }
    *ans += (n - count - 1) * (count + 1) + n;
    count + 1
}

fn run<'a, F: FnMut() -> &'a str, W: std::io::Write>(scan: &mut F, writer: &mut W) {
    macro_rules! scan {
        ([$t:tt; $n:expr]) => ((0..$n).map(|_| scan!($t)).collect::<Vec<_>>());
        (($($t:tt),*)) => (($(scan!($t)),*));
        (Usize1) => (scan!(usize) - 1);
        (Bytes) => (scan().as_bytes().to_vec());
        ($t:ty) => (scan().parse::<$t>().unwrap());
    }
    macro_rules! println {
        ($($arg:tt)*) => (writeln!(writer, $($arg)*).ok());
    }

    let n = scan!(usize);

    let mut graph = vec![vec![]; n];
    for _ in 0..(n - 1) {
        let (a, b) = scan!((Usize1, Usize1));
        graph[a].push(b);
        graph[b].push(a);
    }

    let mut ans = 0;
    dfs(&mut ans, &graph, !0, 0);
    println!("{}", ans);
}

fn main() {
    let ref mut buf = Vec::new();
    std::io::Read::read_to_end(&mut std::io::stdin(), buf).ok();
    let mut scanner = unsafe {
        std::str::from_utf8_unchecked(buf).split_ascii_whitespace()
    };
    let ref mut scan = || scanner.next().unwrap();

    let stdout = std::io::stdout();
    let ref mut writer = std::io::BufWriter::new(stdout.lock());

    run(scan, writer);
}
0