結果

問題 No.1390 Get together
ユーザー ocvret_ocvret_
提出日時 2021-02-12 22:45:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,077 bytes
コンパイル時間 1,482 ms
コンパイル使用メモリ 167,672 KB
実行使用メモリ 4,904 KB
最終ジャッジ日時 2023-09-27 05:37:55
合計ジャッジ時間 4,605 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 100 ms
4,600 KB
testcase_17 AC 105 ms
4,720 KB
testcase_18 AC 111 ms
4,736 KB
testcase_19 AC 109 ms
4,752 KB
testcase_20 AC 111 ms
4,672 KB
testcase_21 AC 108 ms
4,544 KB
testcase_22 AC 104 ms
4,884 KB
testcase_23 AC 110 ms
4,604 KB
testcase_24 AC 110 ms
4,732 KB
testcase_25 AC 110 ms
4,632 KB
testcase_26 AC 110 ms
4,544 KB
testcase_27 AC 110 ms
4,548 KB
testcase_28 AC 109 ms
4,668 KB
testcase_29 AC 110 ms
4,560 KB
testcase_30 AC 109 ms
4,904 KB
testcase_31 AC 110 ms
4,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0