結果

問題 No.1390 Get together
ユーザー どららどらら
提出日時 2021-02-12 21:54:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,340 bytes
コンパイル時間 1,941 ms
コンパイル使用メモリ 178,304 KB
実行使用メモリ 14,504 KB
最終ジャッジ日時 2023-09-27 03:49:11
合計ジャッジ時間 8,063 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
12,696 KB
testcase_01 AC 40 ms
12,700 KB
testcase_02 AC 41 ms
12,784 KB
testcase_03 AC 43 ms
12,948 KB
testcase_04 AC 42 ms
13,136 KB
testcase_05 AC 42 ms
12,956 KB
testcase_06 AC 42 ms
12,964 KB
testcase_07 AC 42 ms
13,068 KB
testcase_08 AC 42 ms
13,116 KB
testcase_09 AC 44 ms
12,964 KB
testcase_10 AC 41 ms
12,724 KB
testcase_11 AC 41 ms
12,888 KB
testcase_12 AC 41 ms
12,784 KB
testcase_13 AC 41 ms
12,704 KB
testcase_14 AC 41 ms
12,748 KB
testcase_15 AC 41 ms
12,900 KB
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
権限があれば一括ダウンロードができます

ソースコード

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[100005];

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

  UnionFind uf(100005);
  rep(i,100005){
    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,100005){
    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