結果
| 問題 |
No.2563 色ごとのグループ
|
| コンテスト | |
| ユーザー |
pmankirai
|
| 提出日時 | 2023-12-02 16:00:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 881 ms / 2,000 ms |
| コード長 | 4,173 bytes |
| コンパイル時間 | 2,571 ms |
| コンパイル使用メモリ | 219,432 KB |
| 最終ジャッジ日時 | 2025-02-18 05:06:26 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;
void speedUpIO() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
#ifndef ATCODER_DSU_HPP
#define ATCODER_DSU_HPP 1
#include <algorithm>
#include <cassert>
#include <vector>
// namespace atcoder {
// Implement (union by size) + (path compression)
// Reference:
// Zvi Galil and Giuseppe F. Italiano,
// Data structures and algorithms for disjoint set union problems
struct dsu {
public:
dsu() : _n(0) {}
explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}
int merge(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
int x = leader(a), y = leader(b);
if (x == y) return x;
if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
parent_or_size[x] += parent_or_size[y];
parent_or_size[y] = x;
return x;
}
bool same(int a, int b) {
assert(0 <= a && a < _n);
assert(0 <= b && b < _n);
return leader(a) == leader(b);
}
int leader(int a) {
assert(0 <= a && a < _n);
if (parent_or_size[a] < 0) return a;
return parent_or_size[a] = leader(parent_or_size[a]);
}
int size(int a) {
assert(0 <= a && a < _n);
return -parent_or_size[leader(a)];
}
std::vector<std::vector<int>> groups() {
std::vector<int> leader_buf(_n), group_size(_n);
for (int i = 0; i < _n; i++) {
leader_buf[i] = leader(i);
group_size[leader_buf[i]]++;
}
std::vector<std::vector<int>> result(_n);
for (int i = 0; i < _n; i++) {
result[i].reserve(group_size[i]);
}
for (int i = 0; i < _n; i++) {
result[leader_buf[i]].push_back(i);
}
result.erase(std::remove_if(result.begin(), result.end(),
[&](const std::vector<int> &v) {
return v.empty();
}),
result.end());
return result;
}
private:
int _n;
// root node: -1 * component size
// otherwise: parent
std::vector<int> parent_or_size;
};
// } // nsamespace atcoder
#endif // ATCODER_DSU_HPP
/*--------------------------------------------------*/
typedef pair<int, int> P;
const int INF = 1e9;
const ll LINF = 1e18;
const int MX = 100010;
void solve() {
int n, m;
cin >> n >> m;
V<int> c(n);
rep(i, n) cin >> c[i];
VV<int> G(n);
rep(i, m) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].eb(b);
G[b].eb(a);
}
auto f = [&](set<int> &us) -> int {
int m = sz(us);
dsu uf(m);
map<int, int> id;
{
int i = 0;
for (int u : us) id[u] = i++;
}
for (int u : us) {
for (int v : G[u]) {
if (us.count(v)) uf.merge(id[u], id[v]);
}
}
return sz(uf.groups()) - 1;
};
map<int, set<int>> mp;
rep(i, n) mp[c[i]].insert(i);
int ans = 0;
for (auto [ci, us] : mp) {
ans += f(us);
}
cout << ans << "\n";
}
int main() {
speedUpIO();
int t = 1;
// cin >> t;
while (t--) {
solve();
// cout << solve() << "\n";
// cout << (solve() ? "YES" : "NO") << "\n";
// cout << fixed << setprecision(15) << solve() << "\n";
}
return 0;
}
pmankirai