結果

問題 No.1390 Get together
ユーザー glretoglreto
提出日時 2021-05-12 17:37:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,852 bytes
コンパイル時間 1,335 ms
コンパイル使用メモリ 121,784 KB
実行使用メモリ 24,660 KB
最終ジャッジ日時 2023-10-24 08:45:16
合計ジャッジ時間 7,880 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 6 ms
4,348 KB
testcase_04 AC 5 ms
4,348 KB
testcase_05 AC 5 ms
4,348 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 6 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 144 ms
15,440 KB
testcase_17 AC 226 ms
24,660 KB
testcase_18 AC 132 ms
12,280 KB
testcase_19 AC 215 ms
21,228 KB
testcase_20 AC 216 ms
23,340 KB
testcase_21 AC 227 ms
23,076 KB
testcase_22 AC 177 ms
18,132 KB
testcase_23 AC 185 ms
18,124 KB
testcase_24 AC 178 ms
18,132 KB
testcase_25 AC 221 ms
21,228 KB
testcase_26 AC 216 ms
21,228 KB
testcase_27 AC 212 ms
21,228 KB
testcase_28 AC 212 ms
21,228 KB
testcase_29 AC 215 ms
21,228 KB
testcase_30 AC 218 ms
21,228 KB
testcase_31 AC 216 ms
21,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
#include<random>
#include<complex>
#include<math.h>
#include<functional>
#include<stack>
#include<queue>
#include<unordered_map>
#include<set>
#include<numeric>
#include <cassert>
using namespace std;
#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long ll;
typedef long double ld;
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; }
const int inf = 0x3fffffff;
const ll INF = 1e18;
struct UnionFind {
    vector<int> par;
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }bool same(int x, int y) { return root(x) == root(y); }
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y]; par[y] = x;
        return 1;
    }int size(int x) { return -par[root(x)]; }
};
int main() {
    int n, m; cin >> n >> m; vector<int> b(n), c(n);
    UnionFind uf(n + m); ll sum = 0; vector<vector<int>> G(n);
    rep(i, n) {
        cin >> b[i] >> c[i], b[i]--, c[i]--;
        G[c[i]].push_back(b[i]);
    }rep(i, n) {
        rep(j, G[i].size() - 1) {
            int now = G[i][j], nx = G[i][j + 1];
            if (now != nx) uf.unite(now, nx);
        }
    }set<int> st;
    rep(i, m) st.insert(uf.root(i));
    for (int i : st) sum += uf.size(i) - 1;
    cout << sum << endl;
}
0