結果

問題 No.1390 Get together
ユーザー どららどらら
提出日時 2021-02-12 21:55:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 1,340 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 179,172 KB
実行使用メモリ 30,540 KB
最終ジャッジ日時 2023-09-27 03:51:22
合計ジャッジ時間 9,279 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
22,744 KB
testcase_01 AC 86 ms
22,748 KB
testcase_02 AC 89 ms
22,744 KB
testcase_03 AC 90 ms
22,584 KB
testcase_04 AC 88 ms
22,588 KB
testcase_05 AC 89 ms
22,576 KB
testcase_06 AC 89 ms
22,580 KB
testcase_07 AC 88 ms
22,588 KB
testcase_08 AC 88 ms
22,744 KB
testcase_09 AC 91 ms
22,888 KB
testcase_10 AC 88 ms
22,592 KB
testcase_11 AC 88 ms
22,632 KB
testcase_12 AC 88 ms
22,592 KB
testcase_13 AC 88 ms
22,628 KB
testcase_14 AC 88 ms
22,724 KB
testcase_15 AC 87 ms
22,588 KB
testcase_16 AC 248 ms
22,628 KB
testcase_17 AC 238 ms
28,396 KB
testcase_18 AC 279 ms
22,732 KB
testcase_19 AC 250 ms
28,404 KB
testcase_20 AC 262 ms
30,540 KB
testcase_21 AC 263 ms
30,540 KB
testcase_22 AC 260 ms
26,544 KB
testcase_23 AC 262 ms
26,704 KB
testcase_24 AC 257 ms
26,688 KB
testcase_25 AC 258 ms
28,456 KB
testcase_26 AC 257 ms
28,392 KB
testcase_27 AC 255 ms
28,508 KB
testcase_28 AC 256 ms
28,456 KB
testcase_29 AC 250 ms
28,540 KB
testcase_30 AC 252 ms
28,500 KB
testcase_31 AC 251 ms
28,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

class UnionFind{
  int n;
  vector<int> data;
public:
  UnionFind(int _n):n(_n),data(_n,-1){}
  bool link(int a, int b){
    int ra=find_root(a);
    int rb=find_root(b);
    if(ra!=rb){
      if(data[rb]<data[ra]) swap(ra,rb);
      data[ra]+=data[rb];
      data[rb]=ra;
    }
    return (ra!=rb);
  }
  bool check(int a, int b){
    return (find_root(a)==find_root(b));
  }
  int find_root(int a){
    return ((data[a]<0)?a:(data[a]=find_root(data[a])));
  }
  int size(int a){
    return -data[find_root(a)];
  }
};


set<int> G[200005];

int main(){
  int N, M;
  cin >> N >> M;
  rep(i,N){
    int b, c;
    cin >> b >> c;
    G[c].insert(b);
  }

  UnionFind uf(200005);
  rep(i,200005){
    int prev = -1;
    FOR(it,G[i]){
      if(prev != -1){
        uf.link(prev, *it);
      }
      prev = *it;
    }
  }

  map<int,int> visited;
  ll ret = 0;
  rep(i,200005){
    int r = uf.find_root(i);
    if(visited.count(r) > 0) continue;
    visited[r] = 1;

    ret += uf.size(r) - 1;
  }

  cout << ret << endl;
  
  
  return 0;
}

0