結果

問題 No.2563 色ごとのグループ
ユーザー neko_the_shadowneko_the_shadow
提出日時 2024-02-16 18:20:06
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 1,695 bytes
コンパイル時間 3,407 ms
コンパイル使用メモリ 198,020 KB
実行使用メモリ 27,644 KB
最終ジャッジ日時 2024-02-16 18:20:18
合計ジャッジ時間 11,924 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,480 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 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 4 ms
6,676 KB
testcase_20 AC 57 ms
6,676 KB
testcase_21 AC 18 ms
6,676 KB
testcase_22 AC 8 ms
6,676 KB
testcase_23 AC 15 ms
6,676 KB
testcase_24 AC 1,041 ms
13,796 KB
testcase_25 AC 1,310 ms
14,104 KB
testcase_26 TLE -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

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 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 uf = UnionFind::new(n);
        for &cur in &curs {
            for &nxt in &g[cur] {
                if c[cur] == c[nxt] {
                    uf.union(cur, nxt)
                }
            }
        }

        let mut groups = HashSet::new();
        for &cur in &curs {
            groups.insert(uf.find(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 {
    parents: Vec<usize>
}

impl UnionFind {
    pub fn new(size: usize) -> Self {
        let parents = (0..size).collect::<Vec<_>>();
        UnionFind{ parents }
    }

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

    pub fn union(&mut self, x: usize, y: usize) {
        let x = self.find(x);
        let y = self.find(y);
        if x != y {
            self.parents[x] = self.parents[y]
        }
    }
}
0