結果

問題 No.1390 Get together
ユーザー kenken714kenken714
提出日時 2021-02-12 23:52:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,424 bytes
コンパイル時間 2,102 ms
コンパイル使用メモリ 185,216 KB
実行使用メモリ 9,416 KB
最終ジャッジ日時 2023-09-27 07:39:50
合計ジャッジ時間 5,970 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 4 ms
4,384 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 5 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 131 ms
8,836 KB
testcase_17 AC 133 ms
8,256 KB
testcase_18 AC 140 ms
9,008 KB
testcase_19 AC 140 ms
8,488 KB
testcase_20 AC 139 ms
9,416 KB
testcase_21 AC 139 ms
8,780 KB
testcase_22 AC 134 ms
8,740 KB
testcase_23 AC 140 ms
8,372 KB
testcase_24 AC 141 ms
8,380 KB
testcase_25 AC 140 ms
8,380 KB
testcase_26 AC 140 ms
8,504 KB
testcase_27 AC 139 ms
8,484 KB
testcase_28 AC 140 ms
8,628 KB
testcase_29 AC 139 ms
8,300 KB
testcase_30 AC 139 ms
8,440 KB
testcase_31 AC 140 ms
8,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};
ll n, m, ans;
bool used[210000];
UnionFind uf(210000);
vector<P> s;
int main(){
    cin >> n >> m;
	REP(i, n){
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        s.push_back(P(b, a));
    }
    sort(ALL(s));
	REP(i, (int)s.size() - 1){
        if (s[i].first == s[i + 1].first) uf.unionSet(s[i].second, s[i + 1].second);
    }
	REP(i, m){
		if(!used[uf.root(i)]){
            used[uf.root(i)] = true;
            ans++;
        }
	}
    cout << m - ans << endl;
}
0