結果

問題 No.2563 色ごとのグループ
ユーザー Kyo_s_sKyo_s_s
提出日時 2023-11-27 16:29:10
言語 Rust
(1.77.0)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 2,887 bytes
コンパイル時間 10,583 ms
コンパイル使用メモリ 171,776 KB
実行使用メモリ 30,288 KB
最終ジャッジ日時 2023-12-02 14:01:46
合計ジャッジ時間 5,014 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 1 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 1 ms
6,548 KB
testcase_06 AC 1 ms
6,548 KB
testcase_07 AC 1 ms
6,548 KB
testcase_08 AC 1 ms
6,548 KB
testcase_09 AC 1 ms
6,548 KB
testcase_10 AC 1 ms
6,548 KB
testcase_11 AC 1 ms
6,548 KB
testcase_12 AC 1 ms
6,548 KB
testcase_13 AC 1 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 2 ms
6,548 KB
testcase_16 AC 2 ms
6,548 KB
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 3 ms
6,548 KB
testcase_20 AC 10 ms
6,548 KB
testcase_21 AC 6 ms
6,548 KB
testcase_22 AC 5 ms
6,548 KB
testcase_23 AC 8 ms
6,548 KB
testcase_24 AC 85 ms
14,068 KB
testcase_25 AC 72 ms
14,824 KB
testcase_26 AC 112 ms
20,908 KB
testcase_27 AC 99 ms
24,888 KB
testcase_28 AC 130 ms
23,216 KB
testcase_29 AC 178 ms
30,284 KB
testcase_30 AC 179 ms
30,280 KB
testcase_31 AC 181 ms
30,288 KB
testcase_32 AC 183 ms
30,284 KB
testcase_33 AC 175 ms
25,804 KB
testcase_34 AC 166 ms
25,924 KB
testcase_35 AC 168 ms
25,932 KB
testcase_36 AC 173 ms
25,804 KB
testcase_37 AC 178 ms
25,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::vec;

macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) };
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

struct UnionFind {
    par: Vec<usize>,
    rank: Vec<usize>,
}

impl UnionFind {
    fn new(n: usize) -> Self {
        UnionFind {
            par: (0..n).collect(),
            rank: vec![0; n],
        }
    }

    fn root(&mut self, x: usize) -> usize {
        if self.par[x] == x {
            x
        } else {
            let par = self.par[x];
            let root = self.root(par);
            self.par[x] = root;
            root
        }
    }

    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }

    fn unite(&mut self, x: usize, y: usize) {
        let x = self.root(x);
        let y = self.root(y);
        if x == y {
            return;
        }
        if self.rank[x] < self.rank[y] {
            self.par[x] = y;
        } else {
            self.par[y] = x;
            if self.rank[x] == self.rank[y] {
                self.rank[x] += 1;
            }
        }
    }
}

fn main() {
    input! {
        n: usize,
        m: usize,
        c: [usize; n],
        edges: [(usize, usize); m],
    }

    let graph = {
        let mut graph = vec![vec![]; n];
        for (a, b) in edges {
            graph[a - 1].push(b - 1);
            graph[b - 1].push(a - 1);
        }
        graph
    };

    let colors = {
        let mut colors = vec![vec![]; n + 1];
        for i in 0..n {
            colors[c[i]].push(i);
        }
        colors
    };

    let mut uf = UnionFind::new(n);
    for i in 0..n {
        for &e in &graph[i] {
            if c[i] == c[e] {
                uf.unite(i, e);
            }
        }
    }

    let mut ans = 0;
    for color in colors {
        if color.len() == 0 {
            continue;
        }
        let re = color[0];
        for &i in &color {
            if !uf.same(re, i) {
                ans += 1;
                uf.unite(re, i);
            }
        }
    }

    println!("{}", ans);
}
0