結果

問題 No.1843 Tree ANDistance
ユーザー phsplsphspls
提出日時 2023-01-10 23:30:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,048 bytes
コンパイル時間 2,850 ms
コンパイル使用メモリ 149,084 KB
実行使用メモリ 5,892 KB
最終ジャッジ日時 2023-08-23 10:27:59
合計ジャッジ時間 7,450 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
5,892 KB
testcase_01 AC 148 ms
5,824 KB
testcase_02 AC 142 ms
5,852 KB
testcase_03 AC 144 ms
5,852 KB
testcase_04 AC 153 ms
5,788 KB
testcase_05 AC 140 ms
5,816 KB
testcase_06 AC 154 ms
5,880 KB
testcase_07 AC 141 ms
5,696 KB
testcase_08 AC 145 ms
5,752 KB
testcase_09 AC 135 ms
5,612 KB
testcase_10 AC 132 ms
5,560 KB
testcase_11 AC 153 ms
5,716 KB
testcase_12 AC 127 ms
5,488 KB
testcase_13 AC 137 ms
5,448 KB
testcase_14 AC 5 ms
4,380 KB
testcase_15 AC 5 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 56 ms
4,944 KB
testcase_29 AC 37 ms
4,376 KB
testcase_30 AC 36 ms
4,380 KB
testcase_31 AC 62 ms
5,328 KB
testcase_32 AC 58 ms
5,072 KB
testcase_33 AC 17 ms
4,380 KB
testcase_34 AC 45 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> Main.rs:5:5
  |
4 | struct UnionFind {
  |        --------- field in this struct
5 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

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