-
Notifications
You must be signed in to change notification settings - Fork 0
Compress tree #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Compress tree #83
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
6ff9e79
saving progress
lrvideckis fef6a44
saving progress
lrvideckis c40002e
Update mono_st.rs
lrvideckis 7a899cc
saving progress
lrvideckis fd59fe1
saving progress
lrvideckis 90572e9
saving progress
lrvideckis 96635dd
another assert
lrvideckis 882bdba
remove
lrvideckis 03682e8
add test
lrvideckis 2eb1c12
asdf
lrvideckis 72658ee
finish
lrvideckis 8eb0abe
better
lrvideckis c656614
fix
lrvideckis 153cec0
uncomment
lrvideckis dc0c736
Update mono_st.rs
lrvideckis c87a544
Update mono_range.rs
lrvideckis 574b24f
Update mono_st.rs
lrvideckis e1f81e5
fix
lrvideckis 8b1614a
change style
lrvideckis 35a88ad
nit
lrvideckis 7646a4a
change
lrvideckis 2dc1895
nits
lrvideckis 0f85db9
different style
lrvideckis 460f332
golf
lrvideckis fc29bdf
consistency with c++ PTC
lrvideckis 55b71bd
finish doc for mono st
lrvideckis fd3b167
simplify test
lrvideckis 00acb23
add
lrvideckis cbb0b82
add docs for mono range
lrvideckis 5f49d8c
fix typo
lrvideckis 016e02e
nit
lrvideckis c97712a
fix
lrvideckis 54936fb
nit to docs
lrvideckis 62e4fc6
now ACs
lrvideckis 2b56c9d
virt tree
lrvideckis 438c6f7
finish docs
lrvideckis 3a1fb27
finished test
lrvideckis ff31ec1
make dist const
lrvideckis 38343f0
consistent var names
lrvideckis 3f89810
Merge branch 'main' into compress_tree
lrvideckis 69c561c
Merge branch 'main' into compress_tree
lrvideckis b9afd08
Merge branch 'main' into compress_tree
cameroncuster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_B | ||
|
||
use proconio::input; | ||
use programming_team_code_rust::graphs::hld::HLD; | ||
use std::collections::VecDeque; | ||
|
||
fn main() { | ||
input! { | ||
n: usize, | ||
edges: [(usize, usize, u32); n - 1], | ||
} | ||
|
||
let mut adj_weighted = vec![vec![]; n]; | ||
let mut adj = vec![vec![]; n]; | ||
for &(u, v, w) in &edges { | ||
adj[u].push(v); | ||
adj[v].push(u); | ||
adj_weighted[u].push((v, w)); | ||
adj_weighted[v].push((u, w)); | ||
} | ||
let adj_weighted = adj_weighted; | ||
|
||
let mut dist = vec![0; n]; | ||
{ | ||
fn dfs(u: usize, p: Option<usize>, adj_weighted: &[Vec<(usize, u32)>], dist: &mut [u32]) { | ||
for &(v, w) in &adj_weighted[u] { | ||
if Some(v) == p { | ||
continue; | ||
} | ||
dist[v] = w + dist[u]; | ||
dfs(v, Some(u), adj_weighted, dist); | ||
} | ||
} | ||
dfs(0, None, &adj_weighted, &mut dist); | ||
} | ||
let dist = dist; | ||
|
||
let hld = HLD::new(&mut adj, true); | ||
|
||
let weighted_dist = |u: usize, v: usize| -> u32 { | ||
let lc = hld.lca(u, v); | ||
dist[u] + dist[v] - 2 * dist[lc] | ||
}; | ||
|
||
let mut diam_u = 0; | ||
for i in 1..n { | ||
if weighted_dist(0, i) > weighted_dist(0, diam_u) { | ||
diam_u = i; | ||
} | ||
} | ||
let mut diam_v = 0; | ||
for i in 1..n { | ||
if weighted_dist(diam_u, i) > weighted_dist(diam_u, diam_v) { | ||
diam_v = i; | ||
} | ||
} | ||
|
||
for u in 0..n { | ||
let (par, to_node) = hld.aux_tree(vec![diam_u, diam_v, u]); | ||
let mut aux_adj = vec![vec![]; par.len()]; | ||
for i in 1..par.len() { | ||
let edge_w = dist[to_node[i]] - dist[to_node[par[i]]]; | ||
aux_adj[i].push((par[i], edge_w)); | ||
aux_adj[par[i]].push((i, edge_w)); | ||
} | ||
let mut q = VecDeque::new(); | ||
q.push_back((to_node.iter().position(|&x| x == u).unwrap(), None, 0)); | ||
let mut res = 0; | ||
while let Some((node, parent, curr_dist)) = q.pop_front() { | ||
res = res.max(curr_dist); | ||
for &(v, w) in &aux_adj[node] { | ||
if Some(v) == parent { | ||
continue; | ||
} | ||
q.push_back((v, Some(node), curr_dist + w)); | ||
} | ||
} | ||
println!("{}", res); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.