結果

問題 No.2563 色ごとのグループ
ユーザー 👑 KA37RIKA37RI
提出日時 2023-12-02 16:14:13
言語 Rust
(1.77.0)
結果
AC  
実行時間 923 ms / 2,000 ms
コード長 1,346 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 197,704 KB
実行使用メモリ 20,436 KB
最終ジャッジ日時 2023-12-02 16:14:26
合計ジャッジ時間 11,139 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 0 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 0 ms
6,548 KB
testcase_14 AC 1 ms
6,548 KB
testcase_15 AC 1 ms
6,548 KB
testcase_16 AC 1 ms
6,548 KB
testcase_17 AC 1 ms
6,548 KB
testcase_18 AC 1 ms
6,548 KB
testcase_19 AC 3 ms
6,548 KB
testcase_20 AC 15 ms
6,548 KB
testcase_21 AC 6 ms
6,548 KB
testcase_22 AC 4 ms
6,548 KB
testcase_23 AC 6 ms
6,548 KB
testcase_24 AC 205 ms
10,840 KB
testcase_25 AC 218 ms
10,552 KB
testcase_26 AC 571 ms
14,996 KB
testcase_27 AC 778 ms
15,700 KB
testcase_28 AC 669 ms
16,352 KB
testcase_29 AC 900 ms
20,436 KB
testcase_30 AC 880 ms
20,436 KB
testcase_31 AC 923 ms
20,436 KB
testcase_32 AC 877 ms
20,436 KB
testcase_33 AC 239 ms
19,264 KB
testcase_34 AC 243 ms
19,196 KB
testcase_35 AC 233 ms
19,196 KB
testcase_36 AC 240 ms
19,264 KB
testcase_37 AC 243 ms
19,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::HashSet;

fn solve() -> u64 {
  let nm = input();
  let mut nm = nm.split(' ');
  let n: usize = nm.next().unwrap().parse().unwrap();
  let m: usize = nm.next().unwrap().parse().unwrap();
  let c: Vec<usize> = input().split(' ').map(|di| di.parse().unwrap()).collect();
  let mut graph = vec![vec![]; n];
  for _ in 0..m {
    let uv = input();
    let mut uv = uv.split(' ');
    let u: usize = uv.next().unwrap().parse().unwrap_or(1) - 1;
    let v: usize = uv.next().unwrap().parse().unwrap_or(1) - 1;
    graph[u].push(v);
    graph[v].push(u);
  }
  let mut colors = vec![0; n];
  let mut not_visited: HashSet<_> = (0..n).collect();
  while !not_visited.is_empty() {
    let start = *not_visited.iter().next().unwrap();
    let col = c[start];
    colors[col-1] += 1;
    not_visited.remove(&start);
    let mut stack = vec![start];
    while let Some(cur) = stack.pop() {
      for &nxt in graph[cur].iter() {
        if c[nxt] == col && not_visited.contains(&nxt) {
          stack.push(nxt);
          not_visited.remove(&nxt);
        }
      }
    }
  }
  return colors.iter().map(|&ci: &u64| ci.saturating_sub(1)).sum();
}

fn main() {
  println!("{}", solve());
}

fn input() -> String {
  let mut buffer = String::new();
  std::io::stdin().read_line(&mut buffer).unwrap();
  return buffer.trim().to_string();
}
0