結果

問題 No.1390 Get together
ユーザー kappybarkappybar
提出日時 2021-02-12 21:54:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 2,282 bytes
コンパイル時間 1,703 ms
コンパイル使用メモリ 176,128 KB
実行使用メモリ 48,180 KB
最終ジャッジ日時 2023-09-27 03:49:37
合計ジャッジ時間 8,212 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 217 ms
36,180 KB
testcase_17 AC 241 ms
36,036 KB
testcase_18 AC 285 ms
42,820 KB
testcase_19 AC 302 ms
45,592 KB
testcase_20 AC 338 ms
44,268 KB
testcase_21 AC 334 ms
44,732 KB
testcase_22 AC 301 ms
43,464 KB
testcase_23 AC 339 ms
48,180 KB
testcase_24 AC 309 ms
43,472 KB
testcase_25 AC 294 ms
45,448 KB
testcase_26 AC 290 ms
45,292 KB
testcase_27 AC 291 ms
45,716 KB
testcase_28 AC 293 ms
45,712 KB
testcase_29 AC 292 ms
45,620 KB
testcase_30 AC 295 ms
45,348 KB
testcase_31 AC 294 ms
45,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;

struct Unionfind{
    vector<int> parents;
    int n;
    Unionfind(int n):n(n),parents(n,-1){ }

    int find(int x){
        if(parents[x] < 0) return x;
        else return parents[x] = find(parents[x]);
    }

    void unite(int x,int y){
        x = find(x);
        y =  find(y);

        if(x==y) return;
        if(parents[x] > parents[y]) swap(x,y);
        parents[x] += parents[y];
        parents[y] = x;
    }  

   int size(int x){
       return -parents[find(x)];
   }

   bool same(int x,int y){
       return find(x)==find(y);
   }

   vector<int> members(int x){
       int root = find(x);
       vector<int> member;
       for(int i=0;i<n;i++){
           if(find(i)==root ){
               member.push_back(i);
           }
       }
      return member;
  }

  int group_cnt(){
      int c = 0;
      rep(i,n){
          if(parents[i] < 0) ++c;
      }
      return c;
  }
};


int main(){
    int n,m;
    cin >> n >> m;
    vector<int> b(n),c(n);
    rep(i,n) cin >> b[i] >> c[i];
    rep(i,n) --b[i], --c[i];
    vector<set<int>> in(m);
    rep(i,n) in[b[i]].insert(c[i]);
    Unionfind uf(n);
    rep(i,m){
        int before = -1;
        for(auto s:in[i]){
            if(before == -1){
                before = s;
                continue;
            }
            uf.unite(before,s);
            before = s;
        }
    }
    vector<set<int>> col(n);
    rep(i,n) col[c[i]].insert(b[i]);
    for(int color = 0; color < n; color  ++){
        if(uf.find(color) == color) continue;
        int par = uf.find(color);
        for(auto s:col[color]){
            col[par].insert(s);
        }
    }
    int ans = 0;
    for(int color = 0; color < n; color  ++){
        if(uf.find(color) != color) continue;
        if((int)col[color].size() == 0) continue;
        ans += (int)col[color].size() - 1; 
    }
    cout << ans << endl;
    
    return 0;
}
0