結果
問題 | No.2563 色ごとのグループ |
ユーザー |
![]() |
提出日時 | 2023-12-02 15:15:31 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 123 ms / 2,000 ms |
コード長 | 4,491 bytes |
コンパイル時間 | 3,497 ms |
コンパイル使用メモリ | 236,076 KB |
実行使用メモリ | 33,768 KB |
最終ジャッジ日時 | 2024-09-26 18:20:26 |
合計ジャッジ時間 | 6,225 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
コンパイルメッセージ
main.cpp: In member function 'void UnionFind::rollback()': main.cpp:122:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions] 122 | for (auto [a,x] : hist.back()) a = x; | ^
ソースコード
/*∧∧∩( ゚∀゚)/ キタ━━━━━━(゚∀゚)━━━━━━!!!!⊂ ノ ゆっくり見てってね!!!!!!!!(つ ノo (ノ\ ☆| o(⌒ ⌒ヽ / ☆\ (´⌒ ⌒ ⌒ヾ /('⌒ ; ⌒ ::⌒ )(´ ) ::: ) /☆─ (´⌒;: ::⌒`) :; )*/#include <bits/stdc++.h>#include <atcoder/all>using namespace std;using namespace atcoder;#define elif else if#define rep(i, s, n) for (int i=s; i<n; i++)#define all(a) a.begin(), a.end()// xを2進数にした際にi桁目が0か1か#define bit(x,i)((x>>i)&1)typedef long long ll;typedef long double ld;ld pi = 3.14159265358979;inline void scan(){}template<class Head,class... Tail>inline void scan(Head&head,Tail&... tail){std::cin>>head;scan(tail...);}// print(a, b, c) => cout << a << ' ' << b << ' ' << c << endl;inline void print(){}template<class Head,class... Tail>inline void print(Head&head,Tail&... tail){std::cout<<head<<" ";print(tail...);cout<<endl;}// bool型に対して"Yes", "No" を出力するbool printb(bool f) {if (f) cout << "Yes\n";else cout << "No\n";return f;}//vectorのcout,cintemplate<typename T>std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}template<typename T>std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}// 2地点の距離(long double)long double coor_dist(long double xi, long double yi, long double xj, long double yj) {return sqrt(abs((xi - xj) * (xi - xj)) + abs((yi - yj) * (yi - yj)));}// 回文かどうかの判定bool iskaibun(string s){ll k = s.size();rep(i,0,k/2){if(s[i]!=s[k-1-i]){return false;}}return true;}template<class T> void chmax(T& a, T b) { if (a < b) a = b; }template<class T> void chmin(T& a, T b) { if (a > b) a = b; }#define mod 998244353using mint = modint998244353;int op(int a, int b) {return max(a, b);}int e() {return 0;}int mapping(int f, int x) {return (f == 1e9 ? x : f);}int composition(int f, int g) {return (f == 1e9 ? g : f);}int id() {return 1e9;}#include <bits/stdc++.h>using namespace std;// rollback できる UnionFind木struct upd {int& a; int x;upd(int &a): a(a), x(a) {}};using Hist = vector<upd>;struct UnionFind {int ans;vector<int> d, e;UnionFind(int n=0): ans(0), d(n,-1), e(n,0) {}int root(int x) {if (d[x] < 0) return x;return root(d[x]);}int f(int v) {return min(size(v), e[v]);}void unite(int x, int y) {x = root(x); y = root(y);hist.push_back(Hist());Hist& h = hist.back();h.emplace_back(ans);if (x == y) {h.emplace_back(e[x]);e[x]++;if (e[x] == size(x)) ans++;} else {if (size(x) < size(y)) swap(x,y);h.emplace_back(d[x]);h.emplace_back(d[y]);h.emplace_back(e[x]);ans -= f(x);ans -= f(y);d[x] += d[y];d[y] = x;e[x] += e[y]+1;ans += f(x);}}vector<Hist> hist;void rollback() {for (auto [a,x] : hist.back()) a = x;hist.pop_back();}int size(int x) {return -d[root(x)];}bool same(int u, int v) {return root(u) == root(v);}};signed main() {cin.tie(nullptr);cout << fixed << setprecision(10);ios::sync_with_stdio(false);int n, m; cin >> n >> m;vector<int> c(n); cin >> c;vector<vector<int>> col(n+1);rep(i, 0, n) col[c[i]].push_back(i);UnionFind uf(n);rep(i, 0, m) {int u, v; cin >> u >> v;u--; v--;if(c[u] != c[v]) continue;uf.unite(u, v);}int ans = 0;rep(i, 1, n+1) {rep(j, 0, col[i].size()) {if(uf.same(col[i][j], col[i][0])) continue;uf.unite(col[i][j], col[i][0]);ans++;}}cout << ans << endl;}