結果

問題 No.1390 Get together
ユーザー umezoumezo
提出日時 2021-02-12 22:33:03
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,249 bytes
コンパイル時間 2,410 ms
コンパイル使用メモリ 207,544 KB
実行使用メモリ 16,632 KB
最終ジャッジ日時 2023-09-27 05:14:09
合計ジャッジ時間 6,687 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 114 ms
13,200 KB
testcase_17 AC 154 ms
16,472 KB
testcase_18 AC 124 ms
13,336 KB
testcase_19 AC 168 ms
16,632 KB
testcase_20 AC 163 ms
16,460 KB
testcase_21 AC 163 ms
16,404 KB
testcase_22 AC 143 ms
15,408 KB
testcase_23 AC 149 ms
15,092 KB
testcase_24 AC 148 ms
15,052 KB
testcase_25 AC 165 ms
16,296 KB
testcase_26 AC 170 ms
16,288 KB
testcase_27 AC 167 ms
16,408 KB
testcase_28 AC 165 ms
16,396 KB
testcase_29 AC 167 ms
16,396 KB
testcase_30 AC 166 ms
16,404 KB
testcase_31 AC 167 ms
16,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

class Dis{
  public:
  vector<ll> rank,p,siz;
  
  Dis(int s){
    rank.resize(s,0);
    p.resize(s,0);
    siz.resize(s,1);
    rep(i,s) makeSet(i);
  }
  
  void makeSet(int x){
    p[x]=x;
    rank[x]=0;
  }
  
  bool same(int x,int y){
    return findSet(x)==findSet(y);
  }
  
  void unite(int x,int y){
    if(same(x,y)) return;
    link(findSet(x),findSet(y));
  }
  
  void link(int x,int y){
    if(rank[x]>rank[y]){
      p[y]=x;
      siz[x]+=siz[y];
    }
    else{
      p[x]=y;
      siz[y]+=siz[x];
      if(rank[x]==rank[y]) rank[y]++;
    }
  }
  
  int findSet(int x){
    if(x != p[x]) p[x]=findSet(p[x]);
    return p[x];
  }
  
  int size(int x){
    return siz[findSet(x)];
  }
};


int main(){
  int n,m;
  cin>>n>>m;
  
  vector<int> A[n];
  rep(i,n){
    int b,c;
    cin>>b>>c;
    b--,c--;
    A[c].push_back(b);
  }
  
  Dis ds=Dis(m);
  rep(i,n){
    int t=A[i].size();
    if(t<2) continue;
    int tmp=A[i][0];
    for(int j=1;j<t;j++) ds.unite(tmp,A[i][j]);
  }
  int cnt=0;
  rep(i,m){
    if(ds.findSet(i)==i) cnt++;
  }
  cout<<m-cnt<<endl;
  
  return 0;
}
0