結果
| 問題 | 
                            No.1390 Get together
                             | 
                    
| コンテスト | |
| ユーザー | 
                             ocvret_
                         | 
                    
| 提出日時 | 2021-02-12 22:45:42 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 139 ms / 2,000 ms | 
| コード長 | 1,077 bytes | 
| コンパイル時間 | 1,664 ms | 
| コンパイル使用メモリ | 168,668 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-19 23:42:12 | 
| 合計ジャッジ時間 | 5,327 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
#include<bits/stdc++.h>
// #include<atcoder/all>
using namespace std;
// using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007
// 2020/12/06 rechecked
class UnionFind{
  public:
  vector<int> par;
  UnionFind(long long size) : par(size,-1){}
  bool unite(int x,int y){
    x = root(x),y = root(y);
    if(x == y)return false;
    if(par[y] < par[x])swap(x,y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  bool find(int x,int y){
    return root(x) == root(y);
  }
  int root(int x){
    return par[x] < 0 ? x : par[x] = root(par[x]);
  }
  int size(int x){
    return -par[root(x)];
  }
};
int main(){
  
  int n,m;
  cin >> n >> m;
  UnionFind uf(m);
  vector<int> col(n,-1);
  rep(i,n){
    int b,c;cin >> b >> c;
    b--;c--;
    if(col[c] >= 0)uf.unite(col[c],b);
    else col[c] = b;
  }
  int res = 0;
  rep(i,m)if(uf.root(i) == i)res += uf.size(i)-1;
  cout << res << "\n";
  return 0;
}
            
            
            
        
            
ocvret_