結果
| 問題 | No.2563 色ごとのグループ | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-12-02 16:14:13 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 956 ms / 2,000 ms | 
| コード長 | 1,346 bytes | 
| コンパイル時間 | 22,652 ms | 
| コンパイル使用メモリ | 403,272 KB | 
| 実行使用メモリ | 20,440 KB | 
| 最終ジャッジ日時 | 2024-09-26 20:01:05 | 
| 合計ジャッジ時間 | 30,483 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 35 | 
ソースコード
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();
}
            
            
            
        