結果

問題 No.1390 Get together
ユーザー 夜
提出日時 2021-02-12 22:08:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 1,120 bytes
コンパイル時間 1,074 ms
コンパイル使用メモリ 99,152 KB
実行使用メモリ 8,536 KB
最終ジャッジ日時 2023-09-27 04:19:49
合計ジャッジ時間 4,934 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 4 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 4 ms
4,376 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 131 ms
8,536 KB
testcase_17 AC 137 ms
7,356 KB
testcase_18 AC 140 ms
8,320 KB
testcase_19 AC 140 ms
7,004 KB
testcase_20 AC 139 ms
7,140 KB
testcase_21 AC 140 ms
7,208 KB
testcase_22 AC 135 ms
7,348 KB
testcase_23 AC 140 ms
7,072 KB
testcase_24 AC 141 ms
7,200 KB
testcase_25 AC 139 ms
7,000 KB
testcase_26 AC 139 ms
7,196 KB
testcase_27 AC 140 ms
7,068 KB
testcase_28 AC 140 ms
7,044 KB
testcase_29 AC 139 ms
7,072 KB
testcase_30 AC 140 ms
7,048 KB
testcase_31 AC 142 ms
7,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
vector<pair<int, int>> vec;
int par[200020];
int siz[200020];
int bo[200020] = { false };
int root(int x) {
	if (par[x] == x) {
		return x;
	}
	else {
		return par[x] = root(par[x]);
	}
}
int main() {
	int n, m;
	int x, y;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		par[i] = i;
		siz[i] = 1;
	}
	for (int i = 0; i < n; i++) {
		int b, c;
		cin >> b >> c;
		vec.emplace_back(make_pair(c, b--));
	}
	sort(vec.begin(), vec.end());
	for (int i = 1; i < n; i++) {
		if (vec[i - 1].first == vec[i].first) {
			x = root(vec[i-1].second);
			y = root(vec[i].second);
			if (x != y) {
				par[x] = y;
				int mem = siz[x];
				siz[x] += siz[y];
				siz[y] += mem;
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < m; i++) {
		if (!bo[root(i)]) {
			ans += siz[root(i)] - 1;
			bo[root(i)] = true;
		}
	}
	cout << ans << endl;
	return 0;
}
0