結果

問題 No.2563 色ごとのグループ
ユーザー yasunori
提出日時 2023-12-02 14:58:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 2,570 bytes
コンパイル時間 2,683 ms
コンパイル使用メモリ 204,492 KB
最終ジャッジ日時 2025-02-18 04:07:15
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
using namespace std;
template <typename T> using min_priority_queue = priority_queue<T,vector<T>,greater<T>>;
template<typename T>
void printv(vector<T> &v){
bool b = false;
for(auto i : v){
if(b) cout << " ";
else b = true;
cout << i;
}
cout << endl;
}
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b; // ab
return true;
}
return false;
}
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b; // ab
return true;
}
return false;
}
void yn(bool b){
if(b) cout << "Yes" << endl;
else cout << "No" << endl;
}
struct UF {
vector<int> par;
vector<int> ran;
vector<int> sz;
UF(int n){
par.resize(n);
ran.resize(n);
sz.resize(n,1);
for(int i = 0; i < n; i++){
par[i] = i;
}
}
int find(int x){
if(par[x] == x) return x;
int rtn = find(par[x]);
par[x] = rtn;
return rtn;
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(ran[x] < ran[y]){
par[x] = y;
sz[y] += sz[x];
}
else{
par[y] = x;
sz[x] += sz[y];
if(ran[x] == ran[y]){
ran[x]++;
}
}
}
bool same(int x, int y){
return find(x) == find(y);
}
int siz(int x){
return sz[find(x)];
}
};
bool debug;
bool randomInput;
void input(){
if(debug && randomInput){
}
else{
}
return;
}
void calc(){
int N, M;
cin >> N >> M;
vector<int> C(N);
for(int i = 0; i < N; i++){
cin >> C[i];
}
UF U(N);
int cnt = 0;
for(int i = 0; i < M; i++){
int u, v;
cin >> u >> v;
u--; v--;
if(C[u] != C[v]) continue;
if(U.same(u, v)) continue;
U.unite(u, v);
cnt++;
}
set<int> C_set;
for(int i : C) C_set.insert(i);
cout << N - C_set.size() - cnt << endl;
return;
}
int64_t ansSimple;
void calcSimple(){
return;
}
void calc1(){
int t; cin >> t;
for(int i = 0; i < t; i++){
input();
calc();
calcSimple();
}
}
int main(){
debug = 0;
randomInput = 0;
srand(time(NULL));
cout << fixed << setprecision(12);
if(debug){
calc1();
}
else{
input();
calc();
}
return 0;
}
//
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0