結果

問題 No.1843 Tree ANDistance
ユーザー phsplsphspls
提出日時 2023-01-10 23:30:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,048 bytes
コンパイル時間 13,877 ms
コンパイル使用メモリ 380,268 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-01 08:05:28
合計ジャッジ時間 19,717 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
6,816 KB
testcase_01 AC 140 ms
6,944 KB
testcase_02 AC 131 ms
6,940 KB
testcase_03 AC 134 ms
6,940 KB
testcase_04 AC 140 ms
6,944 KB
testcase_05 AC 128 ms
6,944 KB
testcase_06 AC 142 ms
6,944 KB
testcase_07 AC 132 ms
6,940 KB
testcase_08 AC 136 ms
6,944 KB
testcase_09 AC 126 ms
6,940 KB
testcase_10 AC 122 ms
6,940 KB
testcase_11 AC 141 ms
6,940 KB
testcase_12 AC 120 ms
6,940 KB
testcase_13 AC 130 ms
6,940 KB
testcase_14 AC 5 ms
6,940 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 3 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 5 ms
6,940 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 1 ms
6,948 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 1 ms
6,944 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 53 ms
6,940 KB
testcase_29 AC 36 ms
6,940 KB
testcase_30 AC 34 ms
6,940 KB
testcase_31 AC 58 ms
6,944 KB
testcase_32 AC 55 ms
6,944 KB
testcase_33 AC 17 ms
6,944 KB
testcase_34 AC 43 ms
6,940 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> src/main.rs:5:5
  |
4 | struct UnionFind {
  |        --------- field in this struct
5 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

const MOD: usize = 1e9 as usize + 7;

struct UnionFind {
    n: usize,
    parents: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
        }
    }

    fn equiv(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }
    
    fn unite(&mut self, a: usize, b: usize) {
        if self.equiv(a, b) { return; }
        let (a, b) = (a.min(b), a.max(b));
        let x = self.parents[a];
        let y = self.parents[b];
        self.parents[y] = self.parents[x];
    }

    fn find(&mut self, a: usize) -> usize {
        if self.parents[a] == a { return a; }
        let p = self.find(self.parents[a]);
        self.parents[a] = p;
        p
    }
}

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let lines = (0..n-1).map(|_| {
            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();
            (temp[0]-1, temp[1]-1, temp[2])
        })
        .collect::<Vec<_>>();

    let mut result = 0usize;
    for i in 0..30 {
        let mut uf = UnionFind::new(n);
        let mut size = vec![1usize; n];
        for &(a, b, c) in lines.iter() {
            if ((c >> i) & 1) == 1 {
                let pa = uf.find(a);
                let pb = uf.find(b);
                uf.unite(pa, pb);
                let p = uf.find(pa);
                if p == pa {
                    size[pa] += size[pb];
                    size[pb] = 0;
                } else {
                    size[pb] += size[pa];
                    size[pa] = 0;
                }
            }
        }
        for j in 0..n {
            if size[j] > 0 {
                result += size[j] * (size[j]-1) / 2 * (1usize << i) % MOD;
                result %= MOD;
            }
        }
    }
    println!("{}", result);
}
0