結果
| 問題 | No.872 All Tree Path | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-12-06 13:53:23 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 166 ms / 3,000 ms | 
| コード長 | 1,154 bytes | 
| コンパイル時間 | 12,066 ms | 
| コンパイル使用メモリ | 378,008 KB | 
| 実行使用メモリ | 44,140 KB | 
| 最終ジャッジ日時 | 2024-10-13 05:01:44 | 
| 合計ジャッジ時間 | 13,805 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 18 | 
ソースコード
fn dfs(cur: usize, prev: usize, paths: &Vec<Vec<(usize, usize)>>, children: &mut Vec<usize>) -> usize {
    let mut ret = 1usize;
    for &(v, qidx) in paths[cur].iter() {
        if v == prev { continue; }
        let val = dfs(v, cur, paths, children);
        children[qidx] = val;
        ret += val;
    }
    ret
}
fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut paths = vec![vec![]; n];
    let mut weights = vec![0usize; n-1];
    for i in 0..n-1 {
        let mut temp = String::new();
        std::io::stdin().read_line(&mut temp).ok();
        let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let u = temp[0]-1;
        let v = temp[1]-1;
        let w = temp[2];
        paths[u].push((v, i));
        paths[v].push((u, i));
        weights[i] = w;
    }
    let mut children = vec![0usize; n-1];
    dfs(0, 0, &paths, &mut children);
    let mut result = 0usize;
    for i in 0..n-1 {
        result += children[i] * (n - children[i]) * weights[i];
    }
    println!("{}", result * 2);
}
            
            
            
        