結果

問題 No.1390 Get together
ユーザー bayashikobayashiko
提出日時 2021-02-12 21:39:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 2,253 bytes
コンパイル時間 3,430 ms
コンパイル使用メモリ 221,028 KB
実行使用メモリ 16,492 KB
最終ジャッジ日時 2023-09-27 03:20:02
合計ジャッジ時間 5,745 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
5,316 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 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,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 69 ms
16,304 KB
testcase_17 AC 46 ms
16,240 KB
testcase_18 AC 81 ms
16,300 KB
testcase_19 AC 69 ms
16,492 KB
testcase_20 AC 61 ms
16,236 KB
testcase_21 AC 60 ms
16,208 KB
testcase_22 AC 74 ms
16,168 KB
testcase_23 AC 74 ms
16,276 KB
testcase_24 AC 74 ms
16,232 KB
testcase_25 AC 69 ms
16,164 KB
testcase_26 AC 69 ms
16,372 KB
testcase_27 AC 68 ms
16,368 KB
testcase_28 AC 69 ms
16,236 KB
testcase_29 AC 67 ms
16,236 KB
testcase_30 AC 68 ms
16,236 KB
testcase_31 AC 68 ms
16,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
//#include<atcoder/all>
//using namespace atcoder;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define flc(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define vec vector<lint>
#define nep(x) next_permutation(all(x))
typedef long long lint;
//typedef __int128_t llint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=3e5+5;
//vector<int> bucket[MAX_N/1000];
constexpr int MOD=1000000007;
//constexpr int MOD=998244353;

vector<lint> par; //MAX_N
vector<lint> ufsize; //MAX_N
vector<map<int,int>> group;
 
void init(lint x){
    par.resize(x);
    ufsize.resize(x);
    group.resize(x);
    rep(i,x) par[i]=i,ufsize[i]=1;
}
 
lint root(lint x){
    if(par[x]==x) return x;
    else return par[x]=root(par[x]);
}
 
bool same(lint x,lint y){
    return root(x)==root(y);
}
 
void unite(lint x,lint y){
    x=root(x);
    y=root(y);
    if(x==y) return;
    if(ufsize[x]>ufsize[y]){
        par[y]=x;
        for(const auto &mp:group[y]) group[x][mp.fi]+=mp.se;
    }
    else{
        par[x]=y;
        for(const auto &mp:group[x]) group[y][mp.fi]+=mp.se;
    }
    lint s=ufsize[x]+ufsize[y];
    ufsize[x]=s,ufsize[y]=s;
}
 
lint size(lint x){
    return ufsize[root(x)];
}

int main(void){
    int N,M;
    cin >> N >> M;
    init(M);
    int plc[N];
    rep(i,N) plc[i]=-1;
    rep(i,N){
        int b,c;
        cin >> b >> c;
        b--,c--;
        if(plc[c]==-1) plc[c]=b;
        else unite(plc[c],b);
    }
    lint ans=0;
    rep(i,M) if(root(i)==i) ans+=size(i)-1;
    cout << ans << endl;
}
0