結果

問題 No.2563 色ごとのグループ
ユーザー hatsuka_iwa
提出日時 2023-12-07 22:06:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 333 ms / 2,000 ms
コード長 714 bytes
コンパイル時間 2,183 ms
コンパイル使用メモリ 200,072 KB
最終ジャッジ日時 2025-02-18 09:04:11
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, M, Ans = 0; cin >> N >> M;
  vector<int> C(N), Count(N, -1); for (int &A : C) { cin >> A; A--; }
  vector<bool> seen(N);
  vector<vector<int>> G(N);

  for (int i = 0; i < M; i++) {
    int u, v; cin >> u >> v;
    u--; v--;
    if (C.at(u) == C.at(v)) {
      G.at(u).push_back(v);
      G.at(v).push_back(u);
    }
  }

  auto DFS = [&](auto DFS, int x) -> void {
    seen.at(x) = true;
    for (int to : G.at(x))
      if (!seen.at(to)) DFS(DFS, to);
  };

  for (int i = 0; i < N; i++) {
    if (!seen.at(i)) {
      Count.at(C.at(i))++;
      DFS(DFS, i);
    }
  }

  for (int i : Count)
    if (i != -1) Ans += i;
  cout << Ans << endl;
}
0