結果

問題 No.2563 色ごとのグループ
ユーザー neko_the_shadowneko_the_shadow
提出日時 2024-02-16 18:42:28
言語 Rust
(1.77.0)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 1,489 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 201,840 KB
実行使用メモリ 36,348 KB
最終ジャッジ日時 2024-02-16 18:42:34
合計ジャッジ時間 5,801 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 1 ms
6,676 KB
testcase_10 AC 1 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 1 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 1 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 1 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 1 ms
6,676 KB
testcase_19 AC 3 ms
6,676 KB
testcase_20 AC 9 ms
6,676 KB
testcase_21 AC 5 ms
6,676 KB
testcase_22 AC 4 ms
6,676 KB
testcase_23 AC 7 ms
6,676 KB
testcase_24 AC 81 ms
13,912 KB
testcase_25 AC 70 ms
14,252 KB
testcase_26 AC 115 ms
21,840 KB
testcase_27 AC 115 ms
31,596 KB
testcase_28 AC 127 ms
23,200 KB
testcase_29 AC 200 ms
36,344 KB
testcase_30 AC 197 ms
36,344 KB
testcase_31 AC 196 ms
36,348 KB
testcase_32 AC 229 ms
36,348 KB
testcase_33 AC 151 ms
20,968 KB
testcase_34 AC 150 ms
20,968 KB
testcase_35 AC 152 ms
20,968 KB
testcase_36 AC 157 ms
20,968 KB
testcase_37 AC 162 ms
20,968 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: fields `parent` and `size` are never read
  --> Main.rs:63:5
   |
62 | pub struct UnionFind {
   |            --------- fields in this struct
63 |     parent: Vec<usize>,
   |     ^^^^^^
64 |     size: Vec<usize>,
   |     ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::{collections::{HashMap, HashSet}, io::stdin};

fn main() {
    let nm = input();
    let n = nm[0];
    let m = nm[1];
    let c = input();

    let mut g = vec![vec![]; n];
    for _ in 0..m {
        let uv = input();
        let u = uv[0] - 1;
        let v = uv[1] - 1;
        g[u].push(v);
        g[v].push(u);
    }

    let mut parent = (0..n).collect::<Vec<usize>>();
    let mut visited = vec![false; n];
    for start in 0..n {
        if visited[start] {
            continue
        }

        let mut stack = vec![start];
        while !stack.is_empty() {
            let cur = stack.pop().unwrap();
            visited[cur] = true;
            parent[cur] = start;
    
            for &nxt in &g[cur] {
                if c[cur]==c[nxt] && !visited[nxt] {
                    stack.push(nxt);
                }
            }
        }
    }

    let mut map = HashMap::new();
    for i in 0..n {
        map.entry(c[i]).or_insert_with(|| vec![]).push(i); 
    }

    let mut ret = 0;
    for (_color, curs) in map {
        let mut groups = HashSet::new();
        for &cur in &curs {
            groups.insert(parent[cur]);
        }
        ret += groups.len()-1;
    }

    println!("{}", ret);
}

fn input() -> Vec<usize> {
    let mut buf = String::new();
    stdin().read_line(&mut buf).unwrap();
    buf.split_whitespace().map(|token| token.parse().unwrap()).collect::<Vec<usize>>()
}

pub struct UnionFind {
    parent: Vec<usize>,
    size: Vec<usize>,
}
0