Skip to content

Commit

Permalink
pastbook2 pastbook2022_c
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jun 29, 2023
1 parent d8835bb commit 03ce79a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cargo-compete/pastbook2/Cargo.toml
Expand Up @@ -26,6 +26,7 @@ edition = "2018"
128 = { alias = "tdpc_house", problem = "https://atcoder.jp/contests/tdpc/tasks/tdpc_house" }
133 = { alias = "past202104_n", problem = "https://atcoder.jp/contests/past202104-open/tasks/past202104_n" }
137 = { alias = "dp_x", problem = "https://atcoder.jp/contests/dp/tasks/dp_x" }
143 = { alias = "pastbook2022_c", problem = "https://atcoder.jp/contests/pastbook2022/tasks/pastbook2022_c" }

[[bin]]
name = "027"
Expand Down Expand Up @@ -111,6 +112,10 @@ path = "src/bin/133.rs"
name = "137"
path = "src/bin/137.rs"

[[bin]]
name = "143"
path = "src/bin/143.rs"

[dependencies]
num = "=0.2.1"
num-bigint = "=0.2.6"
Expand Down
1 change: 1 addition & 0 deletions cargo-compete/pastbook2/README.md
Expand Up @@ -22,3 +22,4 @@
- P.128 家 (Typical DP Contest:M問題) <https://atcoder.jp/contests/tdpc/tasks/tdpc_house>
- P.133 活動 (第六回 アルゴリズム実技検定:N問題) <https://atcoder.jp/contests/past202104-open/tasks/past202104_n>
- P.137 Tower (Educational DP Contest:X問題) <https://atcoder.jp/contests/dp/tasks/dp_x>
- P.143 各部分木の大きさ (オリジナル問題) <https://atcoder.jp/contests/pastbook2022/tasks/pastbook2022_c>
36 changes: 36 additions & 0 deletions cargo-compete/pastbook2/src/bin/143.rs
@@ -0,0 +1,36 @@
use proconio::{input, marker::Usize1};

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

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

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

let edges = adjacency_list(n, &ab);
let mut ans = vec![0_usize; n];
dfs(&mut ans, &edges, 0, 0);

for a in ans {
println!("{}", a);
}
}

0 comments on commit 03ce79a

Please sign in to comment.