結果

問題 No.1390 Get together
ユーザー kkktymkkktym
提出日時 2021-02-12 21:49:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 1,048 ms
コンパイル使用メモリ 106,772 KB
実行使用メモリ 16,892 KB
最終ジャッジ日時 2023-09-27 03:38:45
合計ジャッジ時間 5,065 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 144 ms
14,776 KB
testcase_17 AC 112 ms
11,632 KB
testcase_18 AC 150 ms
16,892 KB
testcase_19 AC 162 ms
14,652 KB
testcase_20 AC 144 ms
12,712 KB
testcase_21 AC 145 ms
12,804 KB
testcase_22 AC 149 ms
15,048 KB
testcase_23 AC 154 ms
15,172 KB
testcase_24 AC 149 ms
15,236 KB
testcase_25 AC 148 ms
14,844 KB
testcase_26 AC 151 ms
14,908 KB
testcase_27 AC 151 ms
14,732 KB
testcase_28 AC 151 ms
14,836 KB
testcase_29 AC 157 ms
14,900 KB
testcase_30 AC 155 ms
14,980 KB
testcase_31 AC 152 ms
14,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
struct UFT{
  vector<int> par;//親
  vector<int> rank;//木の深さ
  vector<int> size;//木の大きさ
  int n;
  
  UFT(int _n)
  {
    n = _n;
    par.resize(n);
    rank.assign(n,0);
    size.assign(n,0);
    rep(i,n){
      par[i] = i;
    }
  }
  
  //xの根を返す
  int find(int x)
  {
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
  }

  //x,yを併合
  void unite(int x,int y)
  {
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank[x] < rank[y]){
      par[x] = y;
      size[y] += size[x];
    }
    else{
      par[y] = x;
      size[x] += size[y];
      if(rank[x] == rank[y]) rank[x]++;
    }
  }

  //x,yが同じグループにいるかどうかを返す
  bool same(int x,int y)
  {
    return find(x) == find(y);
  }

  //xの属する木のサイズを探す
  int usize(int x)
  {
    return size[find(x)];
  }
};

int main()
{
  int n,m; cin >> n >> m;
  vvi a(m+1);
  rep(i,n) {
    int b,c; cin >> b >> c;
    a[b].push_back(c);
  }

  UFT uf(n+1);
  rep1(i,m) {
    if(sz(a[i]) >= 2) {
      rep1(j,sz(a[i])-1) uf.unite(a[i][0], a[i][j]);
    }
  }

  vi tab(n+1, 0);
  int res = 0;
  rep1(i,m) {
    if(sz(a[i]) == 0) continue;
    int x = uf.find(a[i][0]);
    if(tab[x] == 0) tab[x]++;
    else res++;
  }
  cout << res << "\n";

  return 0;
}
0