結果
| 問題 | No.2563 色ごとのグループ | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2023-12-04 21:25:12 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 294 ms / 2,000 ms | 
| コード長 | 1,521 bytes | 
| コンパイル時間 | 2,001 ms | 
| コンパイル使用メモリ | 175,444 KB | 
| 実行使用メモリ | 16,000 KB | 
| 最終ジャッジ日時 | 2024-09-26 23:14:29 | 
| 合計ジャッジ時間 | 6,481 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 35 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Ul = unsigned long long;
using cl = const ll;
using VI = vector<int>;
using VS = vector<string>;
using VC = vector<char>;
using VL = vector<ll>;
using VU = vector<Ul>;
using VB = vector<bool>;
using PI = pair<int, int>;
using PL = pair<ll, ll>;
void fix(int a){cout << fixed << setprecision(a);}
template<class t,class u> bool chmax(t&a,u b) {if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b) {if(b<a){a=b;return true;}else return false;}
#define rep(i,n) for(int i=0;i<(int)(n);i++)
int main()
{
    int N,M;
    cin>>N>>M;
    VI C(N),dist(N,-1),count(N,-1);
    vector<VI> G(N);
    rep(i,N) cin>>C[i];
    rep(i,M)
    {
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        if(C[a]==C[b])
        {
            G[a].push_back(b);
            G[b].push_back(a);
        }
        else continue;
    }
    queue<int> que;
    rep(i,N)
    {
        if(dist[i]==-1)
        {
            que.push(i);
            dist[i]=0;
            count[C[i]-1]++;
        }
        while(!que.empty())
        {
            int t=que.front();
            que.pop();
            rep(j,G[t].size())
            {            
                if(dist[G[t][j]]==-1)
                {
                    que.push(G[t][j]);
                    dist[G[t][j]]=0;
                }
            }
        }
    }
    int Ans=0;
    rep(i,N)
    {
        if(count[i]>=0) Ans+=count[i];
    }
    cout<<Ans;
}
            
            
            
        