結果

問題 No.1390 Get together
ユーザー pionepione
提出日時 2021-02-12 23:09:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 3,798 bytes
コンパイル時間 1,687 ms
コンパイル使用メモリ 174,752 KB
実行使用メモリ 17,860 KB
最終ジャッジ日時 2023-09-27 06:28:21
合計ジャッジ時間 4,893 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 83 ms
15,568 KB
testcase_17 AC 46 ms
12,432 KB
testcase_18 AC 80 ms
17,860 KB
testcase_19 AC 94 ms
15,392 KB
testcase_20 AC 72 ms
13,620 KB
testcase_21 AC 69 ms
13,640 KB
testcase_22 AC 91 ms
16,124 KB
testcase_23 AC 90 ms
16,060 KB
testcase_24 AC 93 ms
16,080 KB
testcase_25 AC 89 ms
15,532 KB
testcase_26 AC 94 ms
15,672 KB
testcase_27 AC 95 ms
15,588 KB
testcase_28 AC 90 ms
15,700 KB
testcase_29 AC 95 ms
15,748 KB
testcase_30 AC 84 ms
15,548 KB
testcase_31 AC 79 ms
15,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define irreps(i, m, n) for (long long i = ((long long)(n)-1); i > (long long)(m); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) (long long)(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;
using ld = long double;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

class UnionFind{
private:
  vector<int> Parent; 
 public:
  UnionFind(int N){
    Parent = vector<int>(N, -1);
  }
  
  int root(int A){
    if(Parent[A] < 0) return A;
    return Parent[A] = root(Parent[A]);
  }
  
  int size(int A){
    return -Parent[root(A)]; 
  }
  
  bool connect(int A, int B){
    A = root(A);
    B = root(B);
    if(A == B){
      return false;
    }
    if(size(A) < size(B)) swap(A, B);
    Parent[A] += Parent[B];
    Parent[B] = A;
    return true;
  }
  
  bool isSame(int A, int B){
    return root(A) == root(B);
  }
};

// 全てのスライムが1か所に集まる
// 同じ箱に居る色は同じグループ
// 同じグループの和-一番個数が多い箱の中の個数
// 箱をグループ化することを考える
// 各箱はsetをもつ
// 同じ箱の要素をUF木でグループ化
// 一通りグループ化したら各箱のidをグループのルートとして割り振る
// 同じidで、複数存在している場合、同じグループの総数-最大の箱の個数になる から和を求めつつ最大を保持 と同時に1個以上かをカウント

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  int n,m; cin>>n>>m;
  
  vc<vc<int>> v(m);
  rep(i,n){
    int b,c; cin>>b>>c; b--,c--;
    v[b].push_back(c);
  }
  
  
  auto uf=UnionFind(n);
  rep(i,m){
    if(v[i].size()==0) continue;
    int fi=v[i][0];
    irep(j,1,v[i].size()){
      uf.connect(fi,v[i][j]);
    }
  }
  
  // rep(i,m) cout<<v[i].size()<<' ';
  // cout<<endl;
  // rep(i,m) {
  //   if(v[i].size()) cout<<uf.root(v[i][0])<<' ';
  //   else cout<<0<<' ';
  // }
  // cout<<endl;
  
  vc<int> id(m,-1);
  vc<int> same_cnt(n);
  vc<int> sum(n);
  vc<int> ma(n);
  rep(i,m){
    if(v[i].size()==0) continue;
    id[i]=uf.root(v[i][0]);
    same_cnt[id[i]]++;
    sum[id[i]]+=v[i].size();
    chmax(ma[id[i]], (int)(v[i].size()));
  }
  
  vc<bool> used(n);
  ll ans=0;
  rep(i,m){
    if(v[i].size()==0) continue;
    int now=id[i];
    if(same_cnt[now]<=1) continue;
    if(used[now]) continue;
    used[now]=1;
    
    ans+=same_cnt[now]-1;
  }
  cout<<ans<<endl;
  
}
0