1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::collections::VecDeque;

pub fn from_root<T: Clone>(
    n: usize,
    es: &[(usize, usize, T)],
    r: usize,
) -> Vec<Vec<(usize, T)>> {
    let mut g = vec![vec![]; n];
    for &(u, v, ref w) in es {
        g[u].push((v, w.clone()));
        g[v].push((u, w.clone()));
    }

    let mut res = vec![vec![]; n];
    let mut q = VecDeque::new();
    let mut seen = vec![false; n];
    q.push_back(r);
    seen[r] = true;
    while let Some(v) = q.pop_front() {
        for (nv, nw) in g[v].drain(..) {
            if seen[nv] {
                continue;
            }
            seen[nv] = true;
            res[v].push((nv, nw));
            q.push_back(nv);
        }
    }
    res
}