結果

問題 No.2563 色ごとのグループ
ユーザー Preds1dentPreds1dent
提出日時 2023-12-02 15:21:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 902 bytes
コンパイル時間 3,474 ms
コンパイル使用メモリ 181,252 KB
実行使用メモリ 23,032 KB
最終ジャッジ日時 2023-12-02 15:21:19
合計ジャッジ時間 8,095 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 2 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 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 2 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 3 ms
6,548 KB
testcase_15 AC 4 ms
6,548 KB
testcase_16 AC 4 ms
6,548 KB
testcase_17 AC 3 ms
6,548 KB
testcase_18 AC 3 ms
6,548 KB
testcase_19 AC 6 ms
6,548 KB
testcase_20 AC 16 ms
6,548 KB
testcase_21 AC 10 ms
6,548 KB
testcase_22 AC 8 ms
6,548 KB
testcase_23 AC 13 ms
6,548 KB
testcase_24 AC 123 ms
10,240 KB
testcase_25 AC 107 ms
11,392 KB
testcase_26 AC 152 ms
15,488 KB
testcase_27 AC 155 ms
20,352 KB
testcase_28 AC 184 ms
16,768 KB
testcase_29 AC 245 ms
21,376 KB
testcase_30 AC 239 ms
21,376 KB
testcase_31 AC 243 ms
21,376 KB
testcase_32 AC 239 ms
21,376 KB
testcase_33 AC 271 ms
22,904 KB
testcase_34 AC 249 ms
22,904 KB
testcase_35 AC 249 ms
22,904 KB
testcase_36 AC 265 ms
23,032 KB
testcase_37 AC 256 ms
22,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <atcoder/all>
using namespace std;
using namespace atcoder;

int main() {
  int n, m; cin >> n >> m;
  vector<int> z(n), c(n), cnt(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i]--;
    z[i] = cnt[c[i]]++;
  }
  map<int, vector<pair<int, int>>> mp;
  for (int i = 0; i < m; i++) {
    int u, v; cin >> u >> v; u--, v--;
    if (c[u] == c[v]) {
      mp[c[u]].push_back({u, v});
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int now = 0;
    if (mp[i].size()) {
      dsu d(cnt[i]);
      for (auto [u, v] : mp[i]) {
        int nu = z[u];
        int nv = z[v];
        if (!d.same(nu, nv)) {
          d.merge(nu, nv);
          now++;
        }
      }
    }
    if (cnt[i] > 0) {
      ans += cnt[i] - 1 - now;
    }
  }
  cout << ans << endl;


}

0