結果
問題 | No.2563 色ごとのグループ |
ユーザー |
|
提出日時 | 2023-12-02 15:04:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 226 ms / 2,000 ms |
コード長 | 587 bytes |
コンパイル時間 | 3,809 ms |
コンパイル使用メモリ | 255,028 KB |
最終ジャッジ日時 | 2025-02-18 04:13:20 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/all>using namespace std;int main(){int N, M;cin >> N >> M;vector<int> C(N);for (int i = 0; i < N; i++){cin >> C[i];C[i]--;}atcoder::dsu uf(N);for (int i = 0; i < M; i++){int u, v;cin >> u >> v;u--;v--;if (C[u] == C[v]){uf.merge(u, v);}}int ans = 0;vector<vector<int>> A(N);for (int i = 0; i < N; i++){A[C[i]].push_back(i);}for (int i = 0; i < N; i++){set<int> st;for (int j : A[i]){st.insert(uf.leader(j));}ans += max(0, (int) st.size() - 1);}cout << ans << endl;}