結果

問題 No.2563 色ごとのグループ
ユーザー Nichi10pNichi10p
提出日時 2023-12-02 15:08:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 313 ms / 2,000 ms
コード長 870 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 208,428 KB
実行使用メモリ 15,820 KB
最終ジャッジ日時 2023-12-02 15:08:41
合計ジャッジ時間 7,100 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 3 ms
6,676 KB
testcase_15 AC 3 ms
6,676 KB
testcase_16 AC 3 ms
6,676 KB
testcase_17 AC 3 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 6 ms
6,676 KB
testcase_20 AC 13 ms
6,676 KB
testcase_21 AC 9 ms
6,676 KB
testcase_22 AC 7 ms
6,676 KB
testcase_23 AC 12 ms
6,676 KB
testcase_24 AC 110 ms
6,676 KB
testcase_25 AC 90 ms
6,676 KB
testcase_26 AC 153 ms
7,632 KB
testcase_27 AC 108 ms
9,360 KB
testcase_28 AC 143 ms
8,112 KB
testcase_29 AC 194 ms
9,676 KB
testcase_30 AC 198 ms
9,676 KB
testcase_31 AC 195 ms
9,676 KB
testcase_32 AC 194 ms
9,676 KB
testcase_33 AC 313 ms
15,820 KB
testcase_34 AC 290 ms
15,820 KB
testcase_35 AC 286 ms
15,820 KB
testcase_36 AC 287 ms
15,820 KB
testcase_37 AC 313 ms
15,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int main() {
  int N, M;
  std::cin >> N >> M;
  std::vector<int> C(N+1);
  std::vector<std::vector<int>> G(N+1);
  for (int i = 1; i <= N; ++i)
    std::cin >> C[i];
  while (M--) {
    int u, v;
    std::cin >> u >> v;
    if (C[u] == C[v]) {
      G[u].push_back(v);
      G[v].push_back(u);
    }
  }

  std::vector<int> groups(N+1);
  std::vector<bool> done(N+1, false);
  for (int s = 1; s <= N; ++s)
    if (!done[s]) {
      std::vector<int> bfs;
      bfs.reserve(N);
      bfs.push_back(s);
      done[s] = true;
      for (auto u = bfs.begin(); u != bfs.end(); ++u)
        for (const int &v : G[*u])
          if (!done[v])
            bfs.push_back(v), done[v] = true;
      ++groups[C[s]];
    }

  int answer{};
  for (int c = 1; c <= N; ++c)
    if (groups[c] > 1)
      answer += groups[c] - 1;
  std::cout << answer << '\n';
}
0