結果

問題 No.1266 7 Colors
ユーザー phsplsphspls
提出日時 2022-11-13 21:43:59
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 347 ms / 3,000 ms
コード長 3,946 bytes
コンパイル時間 13,932 ms
コンパイル使用メモリ 377,296 KB
実行使用メモリ 38,604 KB
最終ジャッジ日時 2024-09-15 03:06:58
合計ジャッジ時間 21,641 ms
ジャッジサーバーID
(参考情報)
judge5 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 215 ms
10,880 KB
testcase_04 AC 332 ms
28,412 KB
testcase_05 AC 226 ms
12,288 KB
testcase_06 AC 334 ms
31,360 KB
testcase_07 AC 347 ms
35,272 KB
testcase_08 AC 315 ms
29,184 KB
testcase_09 AC 294 ms
23,096 KB
testcase_10 AC 275 ms
20,992 KB
testcase_11 AC 236 ms
14,464 KB
testcase_12 AC 252 ms
16,624 KB
testcase_13 AC 271 ms
18,432 KB
testcase_14 AC 220 ms
12,160 KB
testcase_15 AC 347 ms
36,096 KB
testcase_16 AC 254 ms
17,024 KB
testcase_17 AC 328 ms
34,304 KB
testcase_18 AC 250 ms
38,604 KB
testcase_19 AC 122 ms
37,760 KB
testcase_20 AC 122 ms
37,760 KB
testcase_21 AC 166 ms
6,144 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::collections::HashSet`
 --> src/main.rs:1:5
  |
1 | use std::collections::HashSet;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

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

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

ソースコード

diff #

use std::collections::HashSet;


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 nmq = String::new();
    std::io::stdin().read_line(&mut nmq).ok();
    let nmq: Vec<usize> = nmq.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nmq[0];
    let m = nmq[1];
    let q = nmq[2];
    let mut uf = UnionFind::new(7*n);
    let mut size = vec![1usize; 7*n];
    let mut colors = vec![vec![false; 7]; n];
    let mut paths = vec![vec![]; n];
    let merge = |left: usize, right: usize, uf: &mut UnionFind, size: &mut Vec<usize>| {
        let pu = uf.find(left);
        let pv = uf.find(right);
        if pu == pv { return; }
        uf.unite(pu, pv);
        if pu == uf.find(pu) {
            size[pu] += size[pv];
            size[pv] = 0;
        } else {
            size[pv] += size[pu];
            size[pu] = 0;
        }
    };
    for i in 0..n {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).ok();
        let s = s.trim().chars().map(|c| c as usize - '0' as usize).collect::<Vec<_>>();
        for j in 0..7 {
            let idx = (j+1) % 7;
            colors[i][j] = s[j] == 1;
            if s[j] == 1 && s[idx] == 1 {
                merge(i+j*n, i+idx*n, &mut uf, &mut size);
            }
        }
    }
    for _ in 0..m {
        let mut uv = String::new();
        std::io::stdin().read_line(&mut uv).ok();
        let uv: Vec<usize> = uv.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
        let u = uv[0]-1;
        let v = uv[1]-1;
        paths[u].push(v);
        paths[v].push(u);
        for i in 0..7 {
            if colors[u][i] && colors[v][i] {
                merge(u+i*n, v+i*n, &mut uf, &mut size);
            }
        }
    }
    let queries = (0..q).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], temp[1], temp[2])
        })
        .collect::<Vec<_>>();

    for &(t, x, y) in queries.iter() {
        if t == 1 {
            let x = x-1;
            let y = y-1;
            colors[x][y] = true;
            let pidx = (7+y-1) % 7;
            let nidx = (y+1) % 7;
            if colors[x][pidx] {
                merge(x+y*n, x+pidx*n, &mut uf, &mut size);
            }
            if colors[x][nidx] {
                merge(x+y*n, x+nidx*n, &mut uf, &mut size);
            }
            for &v in paths[x].iter() {
                if colors[v][y] {
                    merge(x + y*n, v + y*n, &mut uf, &mut size);
                }
            }
        } else {
            println!("{}", size[uf.find(x-1)]);
        }
    }
}
0