結果

問題 No.2563 色ごとのグループ
ユーザー hatsuka_iwahatsuka_iwa
提出日時 2023-12-07 22:06:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 714 bytes
コンパイル時間 2,266 ms
コンパイル使用メモリ 209,072 KB
実行使用メモリ 18,440 KB
最終ジャッジ日時 2023-12-07 22:06:55
合計ジャッジ時間 6,911 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 1 ms
6,548 KB
testcase_04 AC 2 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 1 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 1 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 AC 2 ms
6,548 KB
testcase_15 AC 3 ms
6,548 KB
testcase_16 AC 3 ms
6,548 KB
testcase_17 AC 3 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 5 ms
6,548 KB
testcase_20 AC 13 ms
6,548 KB
testcase_21 AC 8 ms
6,548 KB
testcase_22 AC 7 ms
6,548 KB
testcase_23 AC 13 ms
6,548 KB
testcase_24 AC 106 ms
6,548 KB
testcase_25 AC 86 ms
6,548 KB
testcase_26 AC 119 ms
7,556 KB
testcase_27 AC 124 ms
9,336 KB
testcase_28 AC 138 ms
8,028 KB
testcase_29 AC 186 ms
9,608 KB
testcase_30 AC 186 ms
9,608 KB
testcase_31 AC 186 ms
9,608 KB
testcase_32 AC 185 ms
9,608 KB
testcase_33 AC 306 ms
18,440 KB
testcase_34 AC 272 ms
18,440 KB
testcase_35 AC 273 ms
18,440 KB
testcase_36 AC 276 ms
18,440 KB
testcase_37 AC 308 ms
18,440 KB
権限があれば一括ダウンロードができます

ソースコード

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