Skip to content

Commit

Permalink
typical90 039
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 14, 2021
1 parent 8b3bcf1 commit ed442d0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions typical90/src/bin/039.rs
@@ -0,0 +1,34 @@
use proconio::{input, marker::Usize1};

fn adjacency_list(n: usize, uv: &Vec<(usize, usize)>) -> Vec<Vec<usize>> {
let mut e = vec![vec![]; n];
for &(u, v) in uv.iter() {
e[u].push(v);
e[v].push(u);
}
e
}

fn dfs(e: &Vec<Vec<usize>>, dp: &mut Vec<usize>, u: usize, p: usize) {
dp[u] = 1;
for &v in e[u].iter() {
if v == p {
continue;
}
dfs(e, dp, v, u);
dp[u] += dp[v];
}
}

fn main() {
input! {
n: usize,
ab: [(Usize1, Usize1); n - 1],
};

let e = adjacency_list(n, &ab);
let mut dp = vec![0_usize; n];
dfs(&e, &mut dp, 0, 0);
let ans = dp.iter().map(|x| x * (n - x)).sum::<usize>();
println!("{}", ans);
}

0 comments on commit ed442d0

Please sign in to comment.