結果

問題 No.2072 Anatomy
ユーザー phsplsphspls
提出日時 2022-09-19 04:30:19
言語 Rust
(1.77.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 2,454 bytes
コンパイル時間 13,401 ms
コンパイル使用メモリ 383,520 KB
実行使用メモリ 13,020 KB
最終ジャッジ日時 2024-06-01 16:06:47
合計ジャッジ時間 16,032 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,812 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 55 ms
9,984 KB
testcase_09 AC 56 ms
10,716 KB
testcase_10 AC 58 ms
8,436 KB
testcase_11 AC 55 ms
10,072 KB
testcase_12 AC 47 ms
7,808 KB
testcase_13 AC 60 ms
11,080 KB
testcase_14 AC 40 ms
7,040 KB
testcase_15 AC 35 ms
7,552 KB
testcase_16 AC 68 ms
11,008 KB
testcase_17 AC 55 ms
10,876 KB
testcase_18 AC 30 ms
6,940 KB
testcase_19 AC 62 ms
11,136 KB
testcase_20 AC 61 ms
11,244 KB
testcase_21 AC 58 ms
11,648 KB
testcase_22 AC 67 ms
11,064 KB
testcase_23 AC 58 ms
11,776 KB
testcase_24 AC 60 ms
12,960 KB
testcase_25 AC 59 ms
11,204 KB
testcase_26 AC 1 ms
6,940 KB
testcase_27 AC 58 ms
13,020 KB
testcase_28 AC 51 ms
11,188 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `n` is never read
 --> src/main.rs:3:5
  |
2 | struct UnionFind {
  |        --------- field in this struct
3 |     n: usize,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: method `chain_cnt` is never used
  --> src/main.rs:46:8
   |
9  | impl UnionFind {
   | -------------- method in this implementation
...
46 |     fn chain_cnt(&mut self, i: usize) -> usize {
   |        ^^^^^^^^^

ソースコード

diff #

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

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            n: n,
            parents: (0..n).collect(),
            depths: vec![0; n],
            chains: vec![1; n],
        }
    }

    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 x = self.parents[a];
        let y = self.parents[b];
        if self.depths[x] > self.depths[y] {
            self.parents[x] = self.parents[y];
            self.chains[y] += self.chains[x];
        } else {
            self.parents[y] = self.parents[x];
            self.chains[x] += self.chains[y];
            if self.depths[x] == self.depths[y] {
                self.depths[x] += 1;
            }
        }
    }

    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 chain_cnt(&mut self, i: usize) -> usize {
        let idx = self.find(i);
        self.chains[idx]
    }
}

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

    lines.reverse();
    let mut uf = UnionFind::new(n);
    let mut cnts = vec![0usize; n];
    for &(u, v) in lines.iter() {
        let pu = uf.find(u);
        let pv = uf.find(v);
        if pu == pv {
            cnts[pu] += 1;
        } else {
            uf.unite(pu, pv);
            let p = uf.find(pu);
            let maxval = cnts[pu].max(cnts[pv]);
            if p == pu {
                cnts[pu] = maxval + 1;
                cnts[pv] = 0;
            } else {
                cnts[pv] = maxval + 1;
                cnts[pu] = 0;
            }
        }
    }
    println!("{}", cnts.iter().max().unwrap());
}
0