結果

問題 No.2563 色ごとのグループ
ユーザー manabeaimanabeai
提出日時 2023-12-02 16:27:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 1,120 bytes
コンパイル時間 3,593 ms
コンパイル使用メモリ 209,452 KB
実行使用メモリ 22,372 KB
最終ジャッジ日時 2023-12-02 16:27:45
合計ジャッジ時間 7,459 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 1 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 1 ms
6,548 KB
testcase_14 AC 3 ms
6,548 KB
testcase_15 AC 3 ms
6,548 KB
testcase_16 AC 2 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 12 ms
6,548 KB
testcase_21 AC 8 ms
6,548 KB
testcase_22 AC 8 ms
6,548 KB
testcase_23 AC 13 ms
6,548 KB
testcase_24 AC 120 ms
11,008 KB
testcase_25 AC 96 ms
10,368 KB
testcase_26 AC 135 ms
13,288 KB
testcase_27 AC 102 ms
13,656 KB
testcase_28 AC 155 ms
14,528 KB
testcase_29 AC 211 ms
18,276 KB
testcase_30 AC 235 ms
18,148 KB
testcase_31 AC 221 ms
18,148 KB
testcase_32 AC 215 ms
18,148 KB
testcase_33 AC 227 ms
22,372 KB
testcase_34 AC 221 ms
22,372 KB
testcase_35 AC 246 ms
22,372 KB
testcase_36 AC 225 ms
22,372 KB
testcase_37 AC 233 ms
22,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define repo(i, n) for (ll i = 1; i <= (ll)(n); ++i)
typedef long long ll;
ll n, m;
ll ans;

void dfs(const vector<vector<ll>> &G, int v, vector<bool> &seen, vector<ll> &c) {
    seen[v] = true;
    for (auto next_v : G[v]) { 
        if (seen[next_v] || c[v] != c[next_v]) continue; 
        dfs(G, next_v,seen,c);
    }
}

int main() {
    cin >> n >> m;
    vector<ll> c(n);
    for (ll i = 0; i < n; ++i) cin >> c[i];

    vector<vector<ll>> G(n);
    vector<bool> seen(n,false);
    
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        --a, --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    vector<ll> count(n,0);
    vector<bool> checkd(n,false);

    for (ll i = 0; i < n; ++i) {
        if (seen[i]) continue;
        else {
            if(!checkd[c[i]-1]) {checkd[c[i]-1] = true; dfs(G,i,seen,c);}
            else {++count[c[i]-1];dfs(G,i,seen,c);}
        }
    }

    ll ans = 0;
    ans = accumulate(count.begin(), count.end(), 0);
    cout << ans << endl;
}
0