結果

問題 No.2563 色ごとのグループ
ユーザー あべしあべし
提出日時 2023-12-02 14:51:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 6,225 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 205,328 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2024-09-26 17:33:30
合計ジャッジ時間 4,425 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 5 ms
5,376 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 34 ms
5,632 KB
testcase_25 AC 29 ms
5,760 KB
testcase_26 AC 40 ms
6,528 KB
testcase_27 AC 34 ms
7,424 KB
testcase_28 AC 44 ms
6,784 KB
testcase_29 AC 60 ms
7,424 KB
testcase_30 AC 61 ms
7,552 KB
testcase_31 AC 61 ms
7,552 KB
testcase_32 AC 58 ms
7,552 KB
testcase_33 AC 71 ms
7,424 KB
testcase_34 AC 72 ms
7,424 KB
testcase_35 AC 72 ms
7,552 KB
testcase_36 AC 72 ms
7,552 KB
testcase_37 AC 74 ms
7,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

// macro
#define rep(i, N) for(ll i = 0; i < (ll)(N); ++i)
#define per(i, N) for(ll i = (ll)(N) - 1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define bit(n, k) (((n) >> (k)) & 1)
#define pcnt(n) (bitset<64>(n).count())
#define endl '\n'
#define fi first
#define se second
#define mkpr make_pair
#define mktpl make_tuple
#define eb emplace_back

// input/output
template<class T> istream& operator>>(istream& is, vector<T>& V){
    for(auto& it : V) is >> it;
    return is;
}
template<class T> ostream& operator<<(ostream& os, vector<T>& V){
    for(int i = 0; i < (int)V.size(); i++){
        os << V[i];
        os << (i + 1 != (int)V.size() ? " " : "");
    }
    return os;
}
template<class T1, class T2> istream& operator>>(istream& is, pair<T1, T2>& P){
    is >> P.first >> P.second;
    return is;
}
template<class T1, class T2> ostream& operator<<(ostream& os, pair<T1, T2> P){
    os << P.first << " " << P.second << "";
    return os;
}

// setup
void set_fast_ios(size_t precision){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(precision);
    cerr << fixed << setprecision(precision);
}

// make_vector
template <class T>
vector<T> make_vector(size_t a, T b) {
    return vector<T>(a, b);
}
template <class... Ts>
auto make_vector(size_t a, Ts... ts) {
    return vector<decltype(make_vector(ts...))>(a, make_vector(ts...));
}

// debug
#ifdef LOCAL
#define debug(x) cerr << "line"<< __LINE__ << ": " << #x << " = " << x << '\n'
#define debugln() cerr << "line" << __LINE__ << ": passed.\n"
#else
#define debug(x, ...) void(0)
#define debugln() void(0)
#endif

// type
using ll = long long;
using ull = unsigned long long;
using ld = double;
using i128 = __int128_t;
using State = string::iterator;
using Pair = pair<ll, ll>;
using Tuple = tuple<ll, ll, ll>;
template<class T> using max_heap = priority_queue<T>;
template<class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vec<vec<T>>;
template<class T> using vvvec = vec<vvec<T>>;

// constant
constexpr ll INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr ll MOD998 = 998244353;
constexpr ll MOD107 = (ll)(1e9+7);
constexpr ll NIL = -1;
constexpr ll pm[2] = {1, -1};
constexpr ll dy[4] = {0, 1, 0, -1};
constexpr ll dx[4] = {1, 0, -1, 0};
constexpr ll dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr ll dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};

// function
ll Gcd(ll a, ll b){ return b ? Gcd(b, a % b) : abs(a);}
ll Lcm(ll a, ll b){ return a / Gcd(a, b) * b;}
template<class T> T powi(T x, ll exp){
    return exp > 0 ? (exp & 1 ? x : 1) * powi(x * x, exp >> 1) : x / x;
}
ll modpow(ll x, ll exp, ll mod){
    x %= mod;
    return exp > 0 ? (exp & 1 ? x : 1) * modpow(x * x, exp >> 1, mod) % mod : 1;
}
template<class T> bool chmin(T &a, T b){ return a > b ? (a = b, true) : 0;}
template<class T> bool chmax(T &a, T b){ return a < b ? (a = b, true) : 0;}

// modint
template <int mod> struct modint{
    long long x;
    modint() : x(0) {}
    modint(long long _x){ x = _x % mod; if(x < 0) x += mod; }
    constexpr modint operator-() const { return modint(-x); }
    constexpr modint& operator+=(const modint& a){ if ((x += a.x) >= mod) x -= mod; return *this; }
    constexpr modint& operator-=(const modint& a){ if ((x += mod - a.x) >= mod) x -= mod; return *this; }
    constexpr modint& operator*=(const  modint& a){ (x *= a.x) %= mod; return *this; }
    constexpr modint& operator/=(const modint& a){ return (*this) *= a.mpow(mod-2); }
    constexpr modint operator+(const modint& a) const { return modint(*this) += a; }
    constexpr modint operator-(const modint& a) const { return modint(*this) -= a; }
    constexpr modint operator*(const modint& a) const { return modint(*this) *= a; }
    constexpr modint operator/(const modint& a) const { return modint(*this) /= a; }
    constexpr bool operator==(const modint& a) const { return (*this).x == a.x; }
    constexpr bool operator!=(const modint& a) const { return (*this).x != a.x; }
    constexpr const modint mpow(long long exp) const {
        modint res(1), x = *this;
        for(; exp; exp >>= 1){
            if(exp & 1) res *= x;
            x *= x;
        }
        return res;
    }
    friend istream& operator>>(istream& is, modint& m){ is >> m.x; m = modint<mod>(m.x); return is; }
    friend ostream& operator<<(ostream& os, const modint& m){ os << m.x; return os; }
    bool operator<(const modint& a) const { return (*this).x < a.x; }
};
constexpr ll MOD = MOD998;
using mint = modint<MOD>;

template<class T> class UnionFind{
    vector<int> par, rank;
    vector<T> dat;

    void merge(int p, int u){
        dat[p] += dat[u];
    }

    public:
    UnionFind() = default;

    UnionFind(int N) : par(N), rank(N, 0), dat(N, 1) {
        for(int i = 0; i < N; i++) par[i] = i;
    }

    UnionFind(int N, vector<T>& V) : par(N), rank(N, 0), dat(N) {
        for(int i = 0; i < N; i++) par[i] = i;
        for(int i = 0; i < N; i++) dat[i] = V[i];
    }

    void unite(int x, int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(rank[y] <= rank[x]){
            if(rank[x] == rank[y]) rank[x]++;
            swap(x, y);
        }
        par[x] = y;
        merge(y, x);
    }

    int find(int x){
        if(par[x] == x) return x;
        else{
            dat[x] = dat[par[x] = find(par[x])];
            return par[x];
        }
    }

    const T &operator[](int x){
        return dat[find(x)];
    }

    bool same(int x, int y){
        return find(x) == find(y);
    }
};

int main(){
    set_fast_ios(15);

    int N, M; cin >> N >> M;
    vec<int> C(N); cin >> C;

    UnionFind<int> UF(N);

    const int MAX_N = 300000;
    vec<int> num(MAX_N, 0);
    rep(i, N) num[C[i]]++;
    rep(i, MAX_N){
        if(num[i] != 0) num[i]--;
    }

    rep(i, M){
        int u, v; cin >> u >> v; u--, v--;
        if(C[u] == C[v] && !UF.same(u, v)){
            UF.unite(u, v);
            num[C[u]]--;
        }
    }

    int ans = 0;
    rep(i, MAX_N) ans += num[i];

    cout << ans << endl;
}
0